<?php
class ProductAction extends HomeAction{
    public function _initialize() {
        parent::_initialize();
    }
	
	public function index(){
		$name = $this->getActionName();
        $action = D($name);
        $ary_get['pageall'] = $this->_get('pageall', 'htmlspecialchars', 10);
        $count = $action->where($where)->count();
        $obj_page = $this->_Page($count, $ary_get['pageall']);
        $page = $obj_page->newshow();
        $ary_data = $action->where($where)->limit($obj_page->firstRow, $obj_page->listRows)->order(array('order'=>'desc'))->select();
        $this->assign("data", $ary_data);
        $this->assign("page", $page);
        $this->assign("filter",$ary_get);
		$this->getCategory();
		$catlist=D('category')->where(array('model'=>$this->getActionName()))->select();
		$this->assign('catid',$catid);
		$this->assign("catlist", $catlist);			
        $this->display();		
	}	
	
	public function getCategory(){
		$name=$this->getActionName();
		$action=D('Category');	
		$Category=$action->where(array('model'=>$name))->order('`order` desc,create_time desc')->select();
		$this->assign('Category',$Category);
	}
    
    public function show(){
		$id=abs(intval($this->_request('id')));
		if(!$id){
			$this->error("非法访问");
		}
		$datas = D($this->modulename)->where(array('id'=>$id))->find();
		$this->assign("datas",$datas);
		
		$catarr=D('category')->where(array('id'=>$datas['cid']))->find();
		$this->assign("catarr",$catarr);		
		
		$catlist=D('category')->where(array('model'=>$this->getActionName()))->select();
		$this->assign('catid',$catid);
		$this->assign("catlist", $catlist);			
		
		$this->display();
    }
	
	public function lists(){
		$name = $this->getActionName();
        $action = D($name);
        $ary_get['pageall'] = $this->_get('pageall', 'htmlspecialchars', 10);
		
		$catid=abs(intval($this->_request('catid')));
		$where=array();
		if($catid){
			$wherearr=array();
			$childcid_arr=D('category')->where(array('pid'=>$catid))->select();
			if($childcid_arr){
					foreach($childcid_arr  as $key=>$one){
						$wherearr[]=$one['id'];	
					}
				$wherearr[]=$catid;
				$wherestr=implode(',',$wherearr);
				$where="cid in($wherestr)";
			}else{
				$where['cid']=$catid;
			}			
			$catarr=D('category')->where(array('id'=>$catid))->find();
			$this->assign("catarr",$catarr);	
			if($catarr['pid']){
				$catarr_parr=D('category')->where(array('id'=>$catarr['pid']))->find();
				$this->assign("catarr_parr",$catarr_parr);
			}				
		}
        $count = $action->where($where)->count();
        $obj_page = $this->_Page($count, $ary_get['pageall']);
        $page = $obj_page->newshow();
        $ary_data = $action->where($where)->limit($obj_page->firstRow, $obj_page->listRows)->order(array('order'=>'desc'))->select();
		
		$catlist=D('category')->where(array('model'=>$this->getActionName()))->select();
		$this->assign('catid',$catid);
		$this->assign("catlist", $catlist);			
		
        $this->assign("data", $ary_data);
        $this->assign("page", $page);
        $this->assign("filter",$ary_get);
		$this->getCategory();
        $this->display('index');		
	}
    
}