<?php


namespace admin\controller;

/**
 * 分类控制器
 * @author huajie <banhuajie@163.com>
 */
class Attr extends Admin {

    
	//需要修改
	private $model="Home/Attr"; //模型名称  /********注意修改模型名称********/
	private $pk="id"; //主键名称  
	private $keyword="name"; //  
	

	// 管理列表
	public function index($type_id){

		$m=D($this->model);
		$where=array();
		$where['type_id']=$type_id;
		$order="sort asc,".$this->pk." desc";
		if(IS_AJAX){
			$list=$m->order($order)->where($where)->select();
			foreach($list as $k=>&$v){
				$v['attr_value']=D('Home/AttrValue')->order('sort')->where("attr_id=".$v['id'])->select();
			}
			$this->ajaxReturn($list);
		}else{
			$keyword=I($this->keyword,false);
			if($keyword!==false){
				$where[$this->keyword]=array('like',"%{$keyword}%");	
			}
			$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('index');	
		}
		
		
		
    }
	
	
	
	//添加
	public function add($type_id){
		$father_info= D('Home/AttrType')->where(array('id'=>$type_id))->find();
		$this->father_info=$father_info;
		$this->father_group = preg_split('/[;\r\n]+/s', $father_info['group'],-1,PREG_SPLIT_NO_EMPTY);
		$m = D($this->model);
		if(IS_POST){
			//数据准备
			$m->create();
			$m->add();
			$this->redirect('index',array('type_id'=>$type_id));
		}else{
			$this->display('index');
		}
	}
	
	//修改
	public function mod($id){
		$m = D($this->model);
		if(IS_POST){
			//数据准备
			$m->create();
			$m->save();
			$this->redirect('index',array('type_id'=>I('type_id')));
		}else{
			$where[$this->pk]=$id;
			$modval = $m->where($where)->find();
			$this->modval =$modval;
			$father_info= D('Home/AttrType')->where(array('id'=>$modval['type_id']))->find();
			$this->father_info=$father_info;
			$this->father_group = preg_split('/[;\r\n]+/s', $father_info['group'],-1,PREG_SPLIT_NO_EMPTY);
			$this->display('index');
		}
	}
	
	//删除我的推广信息
	public function del($ids){
		//数据准备
		$type_id = $_GET['type_id'];
		$m = D($this->model);
		$where[$this->pk]=array('in',$ids);
		$m->where($where)->delete();
		$this->redirect('index',array('type_id'=>$type_id));
	}
	
	//修改显示
	function old($goods_id,$type_id){
		$m=D('Home/Attr');
		$where['type_id']=$type_id;
		$list=$m->where($where)->order('sort')->select();
		$m=D('Home/AttrGoods');
		$where=array();
		foreach($list as $k=>&$v){
			$where['attr_id']=$v['id'];
			$where['goods_id']=$goods_id;
			$v['value']=$m->where($where)->order('id')->select();
			$v['attr_value']=D('Home/AttrValue')->order('sort')->where(array("attr_id"=>$v['id']))->select();
		}
		$this->ajaxReturn($list);
	}
}
