<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Pro_sort extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	function __construct()
	{
		parent::__construct();
		$this -> load_lang('adminsys/pro_sort');
		$this -> load('admin'); 
		$this -> admin -> online_admin();
		$this -> load_db();
		$this -> load('helper');
		$this -> load_model('pro_sort');
	}
	
	function info_list(  )
	{
		$this -> load('page',$this->lang['page']);
		$this -> page -> set_page_size(1000);

		//列表字段显示
		$show_arr = array('prs_id' => array('th'=>0,'td'=>''),'prs_name'=> array('th'=>0,'td'=>''),'prs_count'=> array('th'=>0,'td'=>''),'prs_enabled'=> array('th'=>0,'td'=>''),'prs_list' => array('th'=>0,'td'=>'') );
		$field_show = $this -> helper -> get_th_list( $show_arr , $list_val , $list_type , $lang_arr = $this->lang['field_list'] );

		
		$sql_query = " SELECT prs_id,prs_name,prs_count, prs_parent_id, prs_enabled,prs_list FROM  ".$this -> db -> prefix."pro_sort  WHERE 1 GROUP BY prs_id ORDER BY prs_list,`prs_id` LIMIT  ". $this -> page ->get_limit_val();
		
		$infosm   = $this -> db -> select_info($sql_query);
		$info 	  = $this -> helper -> grade_info_sort( $infosm , $key_id = 'prs_id', $parent_id = 'prs_parent_id', $grade_val = 6 );

		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."pro_sort  WHERE 1  ORDER BY `prs_id` ";
		$infocount  = $this -> db -> select_one($sql_query);
		
		$page_str 	= $this -> page -> get_admin_page(  $infocount["CNUM"] ,'info_list');
		$this -> view -> assign('field_show',$field_show);
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('pro_sort.tpl');
	}

	
	function add( )
	{

		
		$asInfo = $this -> pro_sort_mod -> get_sort();
		$info 	= $this -> helper -> grade_info_sort( $asInfo , $key_id = 'prs_id', $parent_id = 'prs_parent_id', $grade_val = 4 );
		
		//var_dump($info);
		$this -> view -> assign('edit_submit', $this-> lang['add']);
		$this -> view -> assign('txt_title', $this-> lang['add_title']);
		$this -> view -> assign('asInfo',$info);
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=add_info");
		$this -> view -> display('pro_sort_edit.tpl');
		
	}
	
	function num_count( $start_id = 0 )
	{
		//核算数量，是重新核算分类数量 //应该重新编写子分类级别问题
		if($this -> input -> get["start_id"]!='')
		{
			$start_id  = $this -> input -> get["start_id"];
		}

			$sql_query = " SELECT prs_id  FROM  ".$this -> db -> prefix."pro_sort  WHERE prs_id > '$start_id' GROUP BY prs_id  ORDER BY `prs_id`  LIMIT  0,1 ";
 
			$infoOne = $this -> db -> select_one( $sql_query );
			$whereadd = '';
			$sinid  = '';
			//是否存在子分类
			if($infoOne["prs_id"] != '')
			{
				$sql_query = " SELECT prs_id  FROM  ".$this -> db -> prefix."pro_sort  WHERE prs_parent_id = ".$infoOne["prs_id"]." GROUP BY prs_id  ORDER BY `prs_id` ";
				$sortInfo = $this -> db -> select_info( $sql_query );
				
				
				$sinid = $infoOne["prs_id"];
				if($sortInfo!='')
				{
					foreach( $sortInfo as $skey => $sval )
					{
						$sinid .= ','.$sval["prs_id"];
					}
					$whereadd .= " AND  prs_id IN ( ".$sinid." ) ";
					
				}else{
					$whereadd .= " AND  prs_id IN ( ".$infoOne["prs_id"]." ) ";
				}
				
				$sql_querycount = " SELECT count( pro_id ) as NUMCOUNT   FROM  ".$this -> db -> prefix."product WHERE pro_delete = 0   $whereadd  ";
				$infocount = $this -> db -> select_one( $sql_querycount );
				
				$sqlupdate = " UPDATE ".$this -> db -> prefix."pro_sort SET prs_count = '".$infocount["NUMCOUNT"]."' WHERE prs_id = '".$infoOne["prs_id"]."'";
				$this -> db -> query_sql( $sqlupdate );
					
				$this -> view ->  assign('info_title',$_LANG['bd_numing']);
				$this -> view ->  assign('URL_TIME',0);
				
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=numcount&start_id=".$infoOne["prs_id"]);
				$this -> view ->  display('suggestion.tpl');	
				
				
			}else{
			
				$this -> view ->  assign('info_title',$_LANG['bd_numsusse']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=info_list");
				$this -> view ->  display('suggestion.tpl');	
			}
			//查询数量

	}
	
	
	
	
	function add_info()
	{
		$prs_nameval 		= $this -> input ->  req["prs_name"];
		$prs_parent_idval 	= $this -> input ->  req["prs_parent_id"];
		$prs_enabledval 	= $this -> input ->  req["prs_enabled"];
		$prs_listval 		= $this -> input ->  req["prs_list"];

		if($prs_nameval !='' and $prs_enabledval !='' )
		{
			//同级不能同名，不同级可以同名
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."pro_sort  WHERE prs_name = '".$prs_nameval."' and prs_parent_id ='".$prs_parent_idval."' ORDER BY `prs_id` "; 
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".$this -> db -> prefix."pro_sort` (`prs_name` , `prs_parent_id` , `prs_enabled` , `prs_list` ) VALUES (  '$prs_nameval', '$prs_parent_idval', '$prs_enabledval', '$prs_listval')";
				$this -> db -> query_sql($insertip);
				
				$this -> view ->  assign('info_title',$this->lang['add_success']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=info_list");
				$this -> view ->  display('suggestion.tpl');
				
			}else{
				
				$this -> view ->  assign('info_title',$this->lang['err_info_exist']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=add");
				$this -> view ->  display('suggestion.tpl');
				
			}
			
		}else{
			
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=add");
			$this -> view ->  display('suggestion.tpl');	
				
		}

	}
	
	function delete( )
	{
		$idval = $this -> input -> get["id"];
		
		if(!$idval){
			$indel = implode(',', $delArray);
		}else{
			$indel = $idval;
		}
		//要保证不存在子分类，保证不存在该分类产品。
		$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."pro_sort  WHERE  prs_parent_id in  ( $indel ) ORDER BY `prs_id` "; 
		$info = $this -> db -> select_one($sql_query);
		if( $info["CNUM"] > 0 ){
			$this -> view ->  assign('info_title',$this->lang['err_son_info_exist']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=info_list");
			$this -> view ->  display('suggestion.tpl');
		}else{
			$pro_sql = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."product  WHERE  prs_id in  ( $indel ) ORDER BY `prs_id` "; 
			$pro_info = $this -> db -> select_one($pro_sql);
			if($pro_info["CNUM"] > 0){
				$this -> view ->  assign('info_title',$this->lang['err_pro_info_exist']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=info_list");
				$this -> view ->  display('suggestion.tpl');
			}
		}
		//exit();
		
		$deletesql ="DELETE FROM `".$this -> db -> prefix."pro_sort` WHERE `prs_id` in ( $indel ) ";
		$this -> db -> query_sql($deletesql);
		
		
		$this -> view ->  assign('info_title',$this->lang['del_success']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function info()
	{
		
		
		$idval = $this -> input -> get["id"];
		
		if(!$idval){
			$infoid = $infoArray[0];
		}else{
			$infoid = $idval;
		}
		
		
		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."pro_sort  WHERE prs_id = '".$infoid."' ORDER BY `prs_id` ";
		$info = $this -> db -> select_one($sql_query);
		
		//可选择的一级分类 
		$asInfo = $this -> pro_sort_mod -> get_sort();
		
		$parent_info = $this -> helper -> grade_info_sort( $asInfo , $key_id = 'prs_id', $parent_id = 'prs_parent_id', $grade_val = 4 );
		
		$this -> view -> assign('asInfo',$parent_info);
		
		$this -> view -> assign('edit_submit', $this-> lang['edit']);
		$this -> view -> assign('txt_title', $this-> lang['edit_title']);
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('pro_sort_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$prs_idval 			= $this -> input ->  req["prs_id"];
		$prs_nameval 		= $this -> input ->  req["prs_name"];
		$prs_parent_idval 	= $this -> input ->  req["prs_parent_id"];
		$prs_enabledval 	= $this -> input ->  req["prs_enabled"];
		$prs_listval 		= $this -> input ->  req["prs_list"];
		
		if($prs_idval !='' and $prs_nameval !='' )
		{

			$updatesql = "UPDATE `".$this -> db -> prefix."pro_sort` SET  prs_name = '".$prs_nameval."' , prs_parent_id = '".$prs_parent_idval."',prs_enabled = '".$prs_enabledval."' , prs_list = '".$prs_listval."'   WHERE  prs_id = '".$prs_idval."' ";
			
			$this -> db -> query_sql( $updatesql );
			
			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=info_list");
			$this -> view ->  display('suggestion.tpl');
				
		}else{
		
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_sort&act=info&id=".$prs_idval);
			$this -> view ->  display('suggestion.tpl');
		}
		
	}

	
		
}
