<?php
namespace Exam\Controller;
use Common\Controller\HomebaseController;
class TikuController extends HomebaseController {
	function _initialize() {
	  parent::_initialize();
	  if(C('tikuIsOpen')==0){
			$this->error('模块题库未开启');
		}
	  $this->check_login();
	  $this->coursetype_obj = D("Common/coursetype");
	  $this->shiti_obj = D("Common/Exam_shiti");
	  $this->label_obj = D("Common/Label");
	}
	function index(){
		$this->display();
	}
	function zhishidian(){
		$toptype=$this->coursetype_obj->where('parent=0')->order('listorder asc')->select();
		$topid=intval(I("get.topid"));
		$childid= intval(I("get.childid"));
		$thirdid= intval(I("get.thirdid"));
		$labelid= intval(I("get.labelid"));
		$labelid= intval(I("get.labelid"));
		$filter['cs_price']= I("get.cs_price");
		$filter['stu_numbers']= I("get.stu_numbers");
		if(!empty($topid)){
			if(!empty($childid)){
				if(!empty($thirdid)){
					if(!empty($labelid)){
						$map=array("ty_id"=>$thirdid,"labelid"=>$labelid);
						$childtype=$this->coursetype_obj->where('parent='.$topid)->order('listorder asc')->select();
						$thirdtype=$this->coursetype_obj->where('parent='.$childid)->order('listorder asc')->select();
						$label=$this->label_obj->where('c_id='.$thirdid)->order('listorder asc')->select();	
						//$topid不空，$childid不空，$thirdid不空,$labelid不空
					}else{
						$map=array("ty_id"=>$thirdid);
						$childtype=$this->coursetype_obj->where('parent='.$topid)->order('listorder asc')->select();
						$thirdtype=$this->coursetype_obj->where('parent='.$childid)->order('listorder asc')->select();
						$label=$this->label_obj->where('c_id='.$thirdid)->order('listorder asc')->select();	
						//$topid不空，$childid不空，$thirdid不空 $labelid 空
					}
				}else{
					$childtype=$this->coursetype_obj->where('parent='.$topid)->order('listorder asc')->select();
					$thirdtype=$this->coursetype_obj->where('parent='.$childid)->order('listorder asc')->select();
					$label=$this->label_obj->where('c_id='.$childid)->order('listorder asc')->select();
					if(!empty($thirdtype)){
						foreach($thirdtype as $n=> $val){
								$child1Ids[] = $thirdtype[$n]['term_id'];
						}	
						$map=array("ty_id"=>array(in,$child1Ids));
					}else{
						$map=array("ty_id"=>$childid);
					}
					//$topid不空，$childid不空，$thirdid空
			  }
			}else{
				$child1=$this->coursetype_obj->where('parent='.$topid)->order('listorder asc')->select();
				$label=$this->label_obj->where('c_id='.$topid)->order('listorder asc')->select();
				$childtype=$child1;
				if(!empty( $child1)){
						foreach($child1 as $n=> $val){
							$child1Ids[] = $child1[$n]['term_id'];
						}
						$map1['parent']= array('in',$child1Ids);
					    $child2=$this->coursetype_obj->where($map1)->order('listorder asc')->select();
						foreach($child2 as $n=> $val){
							$child2Ids[] = $child2[$n]['term_id'];
				 		}
				 		if(!empty($child2Ids)){
								$childs= array_merge($child1Ids,$child2Ids);
						}else{
								$childs=$child1Ids;
						}	
						$map=array("ty_id"=>array(in,$childs));
				}else{
					  $map=array("ty_id"=>$topid);
				}
				//$topid不空，$childid空
			}
			
		}else{
			$map=array();
				//$topid空
		}
		dump($map);
		$count=$this->shiti_obj->where($map)->count();
		$page = $this->page($count, 16);
		$shitilist=$this->shiti_obj->limit($page->firstRow . ',' . $page->listRows)->order($order)->where($map)->select();
		$this->assign("topid",$topid);
		$this->assign("childid",$childid);
		$this->assign("thirdid",$thirdid);
		$this->assign("labelid",$labelid);
		$this->assign("toptype",$toptype);
		$this->assign("childtype",$childtype);
		$this->assign("thirdtype",$thirdtype);
		$this->assign("label",$label);
		$this->assign("shitilist",$shitilist);
		$this->assign("name",'试题中心');
		//$this->assign("Page", $page->show('Admin'));
	    $this->display();
	}									

}