<?php
namespace app\admin\controller;

use think\facade\Session;
use app\admin\BaseController;
use app\common\model\Navigation as NavigationModel;

class Navigation extends BaseController
{
	
	public function index()
	{
		return view('set/navigationlist');
	}
	
	public function navigationForm()
	{
		$id = input('id/d, 0');
		$navigation = NavigationModel::findOrEmpty($id);
		return view('set/navigationform',[
			'navigation' => $navigation
		]);
	}
	
	
	public function listNavigation()
	{
		$limit = input('limit');
		$page = input('page');
		$map = array();
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$pagetab = input('pagetab/d');
		if (!empty($pagetab)){
			$map[] = ['pagetab', '=', $pagetab];
		}
		$stype = input('stype/d');
		if (!empty($stype)){
			$map[] = ['stype', '=', $stype];
		}
		$list = NavigationModel::where($map)->order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->append(['ntype_text'])->select();
		$count = NavigationModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	
	public function fieldNavigation()
	{
		$id = input('id/d');
		$navigation = NavigationModel::find($id);
		if ($navigation==null){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "分类信息不存在"
			)));
		}else{
			$navigation->save([
				input('field/s')  => input('value')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	
	public function editNavigation()
	{
		$id = input('id/d');
		$data = array(
			'pagetab'   => input('pagetab/s'),
			'title' 	=> input('title/s'),
			'tilpic' 	=> input('tilpic/s'),
			'ntype' 	=> input('ntype/d', 1),
			'tilurl' 	=> input('tilurl/s'),
			'priority'  => input('priority/d', 0)
		);
		if (empty($id)){
			NavigationModel::create($data);
		}else{
			$data['id'] = $id;
			NavigationModel::update($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delNavigation()
	{
		$idarr = input('idarr/a');
		NavigationModel::whereIn('id',$idarr)->delete();
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}

}