<?php
namespace app\admin\controller;
use think\App;
use think\facade\Session;
use think\facade\View;
use think\facade\Request;
 
class label extends Base{

	//Label首页
    public function index(){
		$tdata=Request::param();
		$info['field']=$tdata['field'];
		
		View::assign(['info'=>$info]);

		return View::fetch();
    }

	//Label add
    public function add(){
		$tdata=Request::param();
		
		$list=\app\admin\model\Label::where('field',$tdata['field'])->order('sort asc')->select()->toArray();
		$list=label_list($list);
		if(isset($tdata['pid'])){
			$pid=$tdata['pid'];
		}else{
			$pid='';	
		}

		View::assign(['field'=>$tdata['field'],'list'=>$list,'pid'=>$pid]);
		return View::fetch();
    }

    public function create(){
		IS_AJAX(Request::isAjax());
		$tdata=Request::param();
		if(trim($tdata['name']=='')){
			$data =['type' =>1,'msg' =>'请填写名称'];
			return json($data);
		}
		
		$map['field']=$tdata['field'];
		$map['pid']=$tdata['pid'];
		$sort=\app\admin\model\Label::where($map)->order('sort desc')->limit(1)->select();
		if($sort->isEmpty()){
			$sort=1;
		}else{
			$sort=$sort[0]['sort']+1;
		}
		
		//插入数据
		$tdata['sort']=$sort;
		$label_id=\app\admin\model\Label::strict(false)->insertGetId($tdata);
		
		$data =['type' =>2,'msg' =>'添加成功'];
		return json($data);
    }


    public function edit(){
		$tdata=Request::param();
		
		//父级list
		$map[]=['field','=',$tdata['field']];
		$map[]=['id','<>',$tdata['id']];
		$list=\app\admin\model\Label::where([$map])->order('sort asc')->select()->toArray();
		$list=label_list($list);
	
		$info=\app\admin\model\Label::where('id',$tdata['id'])->find()->toArray();
		
		View::assign(['list'=>$list,'info'=>$info]);
		return View::fetch();
    }

    public function update(){
		IS_AJAX(Request::isAjax());
		$tdata=Request::param();
		if(trim($tdata['name']=='')){
			$data =['type' =>1,'msg' =>'请填写名称'];
			return json($data);
		}
		$images_id=\app\admin\model\Label::strict(false)->update($tdata);
		
		$data =['type' =>2,'msg' =>'修改成功'];
		return json($data);
	}

	//列表数据		
    public function list_data(){
		IS_AJAX(Request::isAjax());
		$tdata=Request::param();
				
		$list=\app\admin\model\Label::where('field',$tdata['field'])->order('sort asc')->select()->toArray();
		$list=label_list($list);

		$data['code']=0;
		$data['data']=$list;
		return json($data);
    }

	
}
