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

    public function index(){
		return View::fetch();
    }
	
    public function list_data(){
		IS_AJAX(Request::isAjax());
		$list=\app\admin\model\CateType::order('id asc')->select()->toArray();
		$data['code']=0;
		$data['data']=$list;
		return json($data);
    }

    public function add(){
		return View::fetch();
    }
	
    public function edit(){
		$tdata=Request::param();
		$info=\app\admin\model\CateType::find($tdata['id']);
		View::assign('info',$info);
		return View::fetch();
    }

    public function insert(){
		$tdata=Request::param();
		if(trim($tdata['table'])==''){
			$data =['type' =>2,'msg' =>'扩展表不能为空'];
			return json($data);
		}

		$table_list=\app\admin\model\TableField::table_list();
		$table_name="tq_article_".$tdata['table'];
		$table=trimall($table_name);
		$inarray=inarray($table,$table_list);
		if($inarray==1){
			$data =['type' =>2,'msg' =>'扩展表已存在'];
			return json($data);
		}

		$add_table=\app\admin\model\TableField::table_add($table);//插入表
		$id_auto=\app\admin\model\TableField::id_auto();//id重置
		
		$id=\app\admin\model\CateType::insert($tdata);
		$data =['type' =>1,'msg' =>'操作成功'];
		return json($data);
    }

    public function update(){
		$tdata=Request::param();
		$id=\app\admin\model\CateType::update($tdata);
		$data =['type' =>1,'msg' =>'操作成功'];
		return json($data);
    }


	public function is_delete(){
		IS_AJAX(Request::isAjax());
		$tdata=Request::param();		

		//删除表
		$table_delete=\app\admin\model\TableField::table_delete($tdata['id'],$tdata['table']);
		//删除表扩展字段信息
		$table="tq_article_".$tdata['table'];
		$table_field_delete=\app\admin\model\TableField::where('pid',$table)->delete();
		
		//删除数据
		$id=\app\admin\model\CateType::where('id',$tdata['id'])->delete();
		$data =['type' =>1,'msg' =>'删除成功'];
		return json($data);
	}
	
}
