<?php


namespace admin\controller;

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

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

	// 管理列表
	public function index($attr_id){
		
		$row_attr= D('Home/Attr')->where(array('id'=>$attr_id))->find();
		$this->row_attr=$row_attr;
		
		$m=D($this->model);
		$where=array();
		$where['attr_id']=$attr_id;
		$keyword=I($this->keyword,false);
		if($keyword!==false){
			$where[$this->keyword]=array('like',"%{$keyword}%");	
		}
		$order=$this->pk." desc";
		$count=$m->where($where)->count();
		$page       = new \source\Page($count,20); //3.2
		$nowPage = I("p",1);
		$this->type_id=$type_id;
		$this->page  = $page->show();
		$this->list=$m->order($order)->where($where)->page($nowPage.','.$page->listRows)->select();
		$this->display('index');
		
		
    }
	
	
	
	//添加
	public function add($attr_id,$type_id){
		$row_attr= D('Home/Attr')->where(array('id'=>$attr_id))->find();
		$this->row_attr=$row_attr;
		$m = D($this->model);
		if(IS_POST){
			//数据准备
			$m->create();
			$m->add();
			$this->redirect('index',array('type_id'=>$type_id,'attr_id'=>$attr_id));
		}else{
			$this->display('index');
		}
	}
	
	//修改
	public function mod($id){
		$row_attr= D('Home/Attr')->where(array('id'=>I('attr_id')))->find();
		$this->row_attr=$row_attr;
		$m = D($this->model);
		if(IS_POST){
			//数据准备
			$m->create();
			$m->save();
			$this->redirect('index',array('type_id'=>I('type_id'),'attr_id'=>I('attr_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){
		//数据准备
		$m = D($this->model);
		$where[$this->pk]=array('in',$ids);
		$m->where($where)->delete();
		$this->redirect('index',array('type_id'=>I('type_id'),'attr_id'=>I('attr_id')));
	}
}
