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

	
	function info_list()
	{
		$this -> load('page',$this->lang['page']);


		$keywordval 	= $this -> input ->  post["keyword"];
		$prs_idval 		= $this -> input ->  post["prs_id"];
		$page_num_val 	= $this -> input ->  get["page_num"];
		$listval  		= $this -> input ->  get["listval"];
		$listtype 		= $this -> input ->  get["listtype"];

		$orderbyval = "";
		if($listval!='' and $listtype =='DESC' )
		{

			$orderbyval = $listval ."  DESC ";
			$listhtml = '';	//反向排序参数
			
		}elseif($listval!='' )
		{
			$orderbyval = $listval;
			$listhtml = 'DESC';


		}else{

			$orderbyval = " prt_id DESC ";	
		}
		//列表字段显示
		$show_arr = array('prt_id' => 1,'prt_name'=>1,'prt_enabled'=>0,'prt_desc'=>0);
		$field_show = $this -> helper -> get_th_list( $show_arr , $list_val , $list_type , $lang_arr = $this->lang['field_list'] );
		
		
		$sql_query = " SELECT prt_id, prt_name,prt_enabled, prt_desc FROM  ".$this -> db -> prefix."pro_type  WHERE 1  GROUP BY prt_id ORDER BY $orderbyval  LIMIT ". $this -> page ->get_limit_val();
		
		$info = $this -> db -> select_info($sql_query);
		
		$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."pro_type  WHERE 1  ORDER BY `prt_id`  ";
		
		$infocount = $this -> db -> select_one($sql_query);
		
		$page_str = $this -> page ->get_admin_page( $infocount["CNUM"] );
		
		$this -> view -> assign('field_show',$field_show);
		$this -> view -> assign('listval',$listval);
		$this -> view -> assign('listtype',$listtype);

		
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('pro_type.tpl');
		
	}
	
	function add()
	{
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24

		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=pro_type&act=add_info");
		$this -> view -> assign( 'edit_submit', $this-> lang['add_submit']);
		$this -> view -> assign( 'txt_title', $this-> lang['add_title']);
		$this -> view -> display('pro_type_edit.tpl');
	}
	
	function add_info()
	{

		$prt_nameval 			= $this -> input ->  post["prt_name"];
		$prt_descval 			= $this -> input ->  post["prt_desc"];
		$prt_enabledval			= $this -> input ->  post["prt_enabled"];


		if($prt_nameval !='' and $prt_descval !='' )
		{
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."pro_type  WHERE prt_name = '".$prt_nameval."' ";
			$info 	  = $this -> db -> select_one($sql_query);
			
			if( $info["CNUM"] < 1 )
			{				

				$insertsql ="INSERT INTO `".$this -> db -> prefix."pro_type` (`prt_name` ,`prt_desc`,`prt_enabled`) 
		VALUES (  '$prt_nameval',  '$prt_descval', '$prt_enabledval')";
				
				$this -> db -> query_sql($insertsql);
				
				$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_type&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_type&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_type&act=add");
			$this -> view ->  display('suggestion.tpl');		}
			//var_dump($_REQUEST);
	}
	
	function del()
	{
		$infoid = $this -> input ->  get["id"];
			
		$pro_sql = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."product  WHERE  prt_id in  ( $infoid ) ORDER BY `prt_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');
		}else{
			$deletesql ="DELETE FROM  `".$this -> db -> prefix."pro_type` WHERE  `prt_id` in ( $infoid ) ";
			$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_type&act=info_list");
			$this -> view ->  display('suggestion.tpl');
		}
	}
 
	
	
	function info()
	{
		$idval = $this -> input ->  get["id"];
		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."pro_type  WHERE prt_id = '".$idval."' ORDER BY `prt_id` ";
		$info = $this -> db -> select_one($sql_query);
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=pro_type&act=edit_info");
		
		$this -> view -> assign( 'edit_submit', $this-> lang['edit']);
		$this -> view -> assign( 'txt_title', $this-> lang['edit_title']);
		
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('pro_type_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
		$prt_idval 				= $this -> input ->  post["prt_id"];
		$prt_nameval 			= $this -> input ->  post["prt_name"];
		$prt_descval  			= $this -> input ->  post["prt_desc"];
		$prt_enabledval  		= $this -> input ->  post["prt_enabled"];

		if($prt_idval !='' and $prt_nameval !='' )
		{

			$updatesql = "UPDATE `".$this -> db -> prefix."pro_type` SET  prt_name = '".$prt_nameval."' , prt_desc = '".$prt_descval."' , prt_enabled = '".$prt_enabledval."' WHERE  prt_id = '".$prt_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_type&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_type&act=info&id=".$prt_idval);
			$this -> view ->  display('suggestion.tpl');
			
		}
		
	}
	
	function attribute_list()
	{
		$this -> load('page',$this->lang['page']);

		$prt_idval 		= $this -> input ->  get["prt_id"];
		$page_num_val 	= $this -> input ->  get["page_num"];
		


		if( $prt_idval != '')
		{
			//列表字段显示
			$show_arr = array('pra_id' => 1,'pra_name'=>1,'prt_name'=>0,'pra_input_type'=>0,'pra_values'=>0,'pra_list'=>0);
			$field_show = $this -> helper -> get_th_list( $show_arr , $list_val , $list_type , $lang_arr = $this->lang['field_attribute_list'] );
			
			
			$sql_query = " SELECT prt_name  FROM  ".$this -> db -> prefix."pro_type  WHERE prt_id = '".$prt_idval."' ORDER BY `prt_id` ";
			$prt_info = $this -> db -> select_one($sql_query);
			
			$sql_query = " SELECT pra_id, pra_name, pra_type,pra_input_type,pra_values,pra_index,pra_list FROM  ".$this -> db -> prefix."pro_attribute  WHERE 1 AND prt_id = $prt_idval GROUP BY pra_id ORDER BY pra_id ASC  LIMIT  ". $this -> page ->get_limit_val();
			
			$pra_info = $this -> db -> select_info($sql_query);
			
			
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."pro_attribute  WHERE 1 AND prt_id = $prt_idval   ORDER BY `pra_id`  ";
			$infocount = $this -> db -> select_one($sql_query);
			
			$page_str = $this -> page ->get_admin_page( $infocount["CNUM"] );
			
			$this -> view -> assign('field_show',$field_show);
			$this -> view -> assign( 'txt_title', $this-> lang['attribute_title']);
			$this -> view -> assign( 'edit_title', $this-> lang['attribute_list_title']);
			
			
			$this -> view -> assign('listval',$listval);
			$this -> view -> assign('listtype',$listtype);
			
			$this -> view -> assign('prt_id',$prt_idval);
			$this -> view -> assign('page_str',$page_str);
			$this -> view -> assign('prt_info',$prt_info);
			$this -> view -> assign('info',$pra_info);
			
			$this -> view -> display('attribute_list.tpl');
		}
	}


	function attribute_add()
	{
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$prt_idval 	= $this -> input -> get["prt_id"];
		$prt_info 	= $this -> all_pro_type();
		
		$this -> view -> assign( 'txt_title', $this-> lang['attribute_title']);
		$this -> view -> assign( 'edit_title', $this-> lang['attribute_add_title']);
		$this -> view -> assign( 'edit_submit', $this-> lang['add_submit']);
		
		$this -> view -> assign('prt_info',$prt_info);
		$this -> view -> assign('prt_id',$prt_idval);
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=pro_type&act=attribute_add_info");
		$this -> view -> display('attribute_edit.tpl');
	}
	
	function all_pro_type()
	{
		$sql_query = " SELECT prt_id , prt_name  FROM  ".$this -> db -> prefix."pro_type  WHERE 1 ORDER BY `prt_id` ";
		return $info = $this -> db -> select_info($sql_query);	
	}
	
	function attribute_add_info()
	{

		$pra_nameval 			= $this -> input ->  post["pra_name"];
		$prt_idval 				= $this -> input ->  req["prt_id"];
		$pra_input_typeval		= $this -> input ->  post["pra_input_type"];
		$pra_valuesval 			= $this -> input ->  post["pra_values"];
		$pra_indexval 			= $this -> input ->  post["pra_index"];
		$pra_is_linkval			= $this -> input ->  post["pra_is_link"];
		$pra_typeval 			= $this -> input ->  post["pra_type"];
		$pra_listval 			= $this -> input ->  post["pra_list"];


		if($pra_nameval !='' and $prt_idval !=''  and $pra_input_typeval !='' and $pra_indexval !=''  and $pra_typeval !=''  and $pra_listval !='' )
		{
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."pro_attribute  WHERE pra_name = '".$pra_nameval."' and prt_id =  '".$prt_idval."'" ;
			$info 	  = $this -> db -> select_one($sql_query);
			
			if( $info["CNUM"] < 1 )
			{				

				$insertsql ="INSERT INTO `".$this -> db -> prefix."pro_attribute` (`pra_name` ,`prt_id`,`pra_input_type`,`pra_values`,`pra_index`,`pra_is_link`,`pra_type`,`pra_list`) 
		VALUES (  '$pra_nameval',  '$prt_idval', '$pra_input_typeval',  '$pra_valuesval', '$pra_indexval',  '$pra_is_linkval', '$pra_typeval',  '$pra_listval')";
				
				$this -> db -> query_sql($insertsql);
				
				$this -> view ->  assign('info_title',$this->lang['add_success']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				if($prt_idval !=''){
					$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=proattribute_list_type&prt_id=".$prt_idval);
				}else{
					$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_type&act=attribute_list");
				}
				
				
				$this -> view ->  display('suggestion.tpl');	
			}else{
				
				$this -> view ->  assign('info_title',$this->lang['err_info_exist']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				if($prt_idval !=''){
					$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_type&act=add&prt_id=".$prt_idval);
				}else{
					$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_type&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_type&act=attribute_add&prt_id=".$prt_idval);
			$this -> view ->  display('suggestion.tpl');		}
			//var_dump($_REQUEST);
	}
	
	function attribute_delete()
	{
		$infoid = $this -> input ->  get["id"];
		$prt_idval 		= $this -> input ->  get["prt_id"];
		$pro_sql = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."pro_att_value  WHERE  pra_id in  ( $infoid ) ORDER BY `pra_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);
			if($prt_idval!=''){
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_type&act=attribute_list&prt_id=$prt_idval");
			}else{
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_type&act=attribute_list");
			}
			$this -> view ->  display('suggestion.tpl');
		}else{
		
			$deletesql ="DELETE FROM  `".$this -> db -> prefix."pro_attribute` WHERE  `pra_id` in ( $infoid ) ";
			$this -> db -> query_sql($deletesql);
			
			$this -> view ->  assign('info_title',$this->lang['del_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			if($prt_idval!=''){
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_type&act=attribute_list&prt_id=$prt_idval");
			}else{
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_type&act=attribute_list");
			}
			$this -> view ->  display('suggestion.tpl');
		}
	}
	
	function attribute_info()
	{
		$idval 		= $this -> input ->  get["id"];
		$prt_idval 	= $this -> input ->  get["prt_id"];

		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."pro_attribute  WHERE pra_id = '".$idval."' ORDER BY `prt_id` ";
		$info = $this -> db -> select_one($sql_query);
		if($prt_idval==''){
			$prt_idval = $info['prt_id'];
		}
		
		$prt_info 		= $this -> all_pro_type();
		
		$this -> view -> assign('prt_info',$prt_info);
		$this -> view ->  assign('prt_id',$prt_idval);
		$this -> view -> assign( 'txt_title', $this-> lang['attribute_title']);
		$this -> view -> assign( 'edit_title', $this-> lang['attribute_edit_title']);
		$this -> view -> assign( 'edit_submit', $this-> lang['edit']);
	
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=pro_type&act=attribute_edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('attribute_edit.tpl');
	
	}
	
	function attribute_edit_info( )
	{	
		
		$pra_idval 				= $this -> input ->  post["pra_id"];
		$prt_idval 				= $this -> input ->  post["prt_id"];
		$pra_nameval 			= $this -> input ->  post["pra_name"];
		$pra_input_typeval		= $this -> input ->  post["pra_input_type"];
		
		$pra_valuesval 			= $this -> input ->  post["pra_values"];
		$pra_indexval 			= $this -> input ->  post["pra_index"];
		$pra_is_linkval			= $this -> input ->  post["pra_is_link"];
		
		$pra_typeval 			= $this -> input ->  post["pra_type"];
		$pra_listval 			= $this -> input ->  post["pra_list"];

		//$pra_nameval !='' and $prt_idval !=''  and $pra_input_typeval !=''  and $pra_valuesval !='' and $pra_indexval !=''  and $pra_typeval !=''  and $pra_listval !='' 
		if( $pra_nameval !='' and $prt_idval !=''  and $pra_input_typeval !=''  and $pra_valuesval !='' and $pra_indexval !=''  and $pra_typeval !=''  and $pra_listval !='' )
		{

			$updatesql = "UPDATE `".$this -> db -> prefix."pro_attribute` SET  prt_id = '".$prt_idval."' , pra_name = '".$pra_nameval."' , pra_input_type = '".$pra_input_typeval."' , pra_values  = '".$pra_valuesval."' , pra_index = '".$pra_indexval."' , pra_is_link = '".$pra_is_linkval."' , pra_type = '".$pra_typeval."' , pra_list = '".$pra_listval."'  WHERE  prt_id = '".$prt_idval."' ";
			
			$this -> db -> query_sql( $updatesql );
			
			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			if($prt_idval!=''){
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_type&act=attribute_list&prt_id=$prt_idval");
			}else{
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=pro_type&act=attribute_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_type&act=attribute_info&id=".$prt_idval);
			$this -> view ->  display('suggestion.tpl');
			
		}
	}


	
}

