<?php


namespace admin\controller;

/**
 * 分类控制器
 * @author huajie <banhuajie@163.com>
 */
class Type extends Admin {
	//需要修改
	private $pk="id"; //主键名称  
	private $keyword="name";
	
    
	function _initialize(){
		parent::_initialize();
		$this->table=I('table'); //模型名称
		$this->id="id"; //主键名称
		$this->typename=I('typename','地区');
		$this->order=I('order','id');
		$this->orderway=I('orderway','desc');
		
		
	}
	

	
	/****
	*列表
	*****/
	function index(){
		//数据表初始化
		//M()->query("DROP TABLE IF EXISTS `".C('DB_PREFIX')."$this->table`");
		M()->execute("CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."$this->table` (
		    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
			`name` varchar(255) NOT NULL DEFAULT '',
			`reid` int(10) unsigned NOT NULL DEFAULT '0',
			`reids` varchar(255) NOT NULL DEFAULT '',
			`disorder` int(10) unsigned DEFAULT NULL,
			PRIMARY KEY (`id`)
		) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=".C("DB_CHARSET"));
		
		
		//变量，模型准备
		$m=D($this->table);
		$order="disorder asc,id desc";
		$keyword=I($this->keyword,false);
		if($keyword!==false){
			$where[$this->keyword]=array('like',"%{$keyword}%");	
		}
		$reid=I("reid",0);
		//逻辑处理
		if($_REQUEST['reid']=="top"){
			$where['reid']=0; 
		}else if($reid){
			$where['reid']=$reid;
		}
		$count=$m->where($where)->count();
		
		$page       = new \source\Page($count,20); //3.2
		$nowPage = I("p",1);
		$this->page  = $page->show();
		$this->list=$m->order($order)->where($where)->page($nowPage.','.$page->listRows)->select();
		$this->display();
		
		
	}


	/****
	*列表
	*****/
	function lists(){
		//数据表初始化
		//M()->query("DROP TABLE IF EXISTS `".C('DB_PREFIX')."$this->table`");
		M()->execute("CREATE TABLE IF NOT EXISTS `".C('DB_PREFIX')."$this->table` (
		    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
			`name` varchar(255) NOT NULL DEFAULT '',
			`reid` int(10) unsigned NOT NULL DEFAULT '0',
			`disorder` int(10) unsigned DEFAULT NULL,
			PRIMARY KEY (`id`)
		) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=".C("DB_CHARSET"));
		
		
		//变量，模型准备
		$m=D($this->table);
		$order="disorder asc,id desc";
		//搜索
		$name=I('name',false);
		if($name!==false){
			$where['name']=array('like',"%{$name}%");	
		}
		$reid=I("reid",0);
		if($reid){
			$where['reid']=$reid;
		}
		$count=$m->where($where)->count();
		
		$page       = new \source\Page($count,20); //3.2
		$nowPage = I("p",1);
		$this->page  = $page->show();
		$list=$m->order($order)->where($where)->page($nowPage.','.$page->listRows)->select();
		$this->list=json_encode($list);
		$this->display();
		
		
	}
	

	
	/****
	*添加
	*****/
	public function add(){
		$table=I('table');
		$m = M($table);
		if(IS_POST){
			//数据准备
			$m->create();
			$m->add();
			S('type_'.$_REQUEST['table'],null);
			$this->redirect('index',array('table'=>$this->table,'typename'=>$this->typename));
		}else{
			$this->display('index',array('table'=>$this->table,'typename'=>$this->typename));	
		}
	}
	
	
	/****
	*修改
	*****/
	public function mod($id){
		$table=$_REQUEST['table'];
		$m = M($table);
		if(IS_POST){
			//数据准备
			$data=$m->create();
			$m->save();
			S('type_'.$table,null);
			if(IS_AJAX){
			    $this->success('更新成功');
			}
			$this->redirect('index',array('table'=>$this->table,'typename'=>$this->typename,'p'=>I('p',1)));
		}else{
			$where[$this->pk]=$id;
			$this->modval = $m->where($where)->find();
			$this->display('index',array('table'=>$this->table,'typename'=>$this->typename,'p'=>I('p',1)));
		}
	}
	
	/****
	*删除
	*****/
	public function del($ids){
		//数据准备
		$m=D($this->table);
		$where[$this->pk]=array('in',$ids);
		$m->where($where)->delete();
		S('type_'.$this->table,null);
		if(IS_AJAX){
			    $this->success('删除成功');
			}
		$this->redirect('index',array('table'=>$this->table,'typename'=>$this->typename,'p'=>I('p',1)));
	}

}
