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

	
	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' )
		{
			//$endArray["listtype"] = 'DESC';
			//$endArray["listval"]  = $listval;
			$orderbyval = $listval ."  DESC ";
			$listhtml = '';	//反向排序参数
			
		}elseif($listval!='' )
		{
			$orderbyval = $listval;
			$listhtml = 'DESC';
			
			//$endArray["listtype"] = '';
			//$endArray["listval"]  = $listval;

		}else{
			//$endArray["listtype"] = 'DESC';
			//$endArray["listval"]  = 'art_uptime';
			$orderbyval = " bra_add_time DESC ";	
		}
		//列表字段显示
		$show_arr = array('bra_id' => 1,'bra_name'=>1,'bra_logo'=>0,'bra_list'=>1,'bra_add_time'=>1,'bra_enabled'=>1);
		$field_show = $this -> helper -> get_th_list( $show_arr , $list_val , $list_type , $lang_arr = $this->lang['field_list'] );


		$sql_query = " SELECT bra_id, bra_name, bra_logo, bra_url, bra_list ,bra_add_time,bra_enabled FROM  ".$this -> db -> prefix."brand  WHERE 1 GROUP BY bra_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."brand  WHERE 1  ORDER BY `bra_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('brand.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=brand&act=add_info");
		$this -> view -> assign( 'edit_submit', $this-> lang['add_submit']);
		$this -> view -> assign( 'txt_title', $this-> lang['add_title']);
		$this -> view -> display('brand_edit.tpl');
		
	}
	
	function add_info()
	{

		$bra_nameval 			= $this -> input ->  post["bra_name"];
		$bra_logoval  			= $this -> input ->  post["bra_logo"];
		$bra_urlval  			= $this -> input ->  post["bra_url"];	
		$bra_listval 			= $this -> input ->  post["bra_list"];
		$bra_descval 			= $this -> input ->  post["bra_desc"];
		$bra_enabledval			= $this -> input ->  post["bra_enabled"];


		if($bra_nameval !=''  )
		{
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."brand  WHERE bra_name = '".$bra_nameval."' and  bra_list = '".$bra_listval."' ";
			$info 	  = $this -> db -> select_one($sql_query);
			
			if( $info["CNUM"] < 1 )
			{				

				$bra_add_timeval = date("Y-m-d H:i:s");  	//time();
				$insertsql ="INSERT INTO `".$this -> db -> prefix."brand` (`bra_name` , `bra_logo` , `bra_url` , `bra_list` , `bra_desc`,`bra_add_time` ,`bra_enabled`) 
		VALUES (  '$bra_nameval', '$bra_logoval', '$bra_urlval', '$bra_listval', '$bra_descval', '$bra_add_timeval','$bra_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=brand&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=brand&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=brand&act=add");
			$this -> view ->  display('suggestion.tpl');		}
			//var_dump($_REQUEST);
	}
	
	function delete( )
	{
		$infoid = $this -> input ->  get["id"];
			
		$pro_sql = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."product  WHERE  bra_id in  ( $infoid ) ORDER BY `bra_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."brand` WHERE  `bra_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=brand&act=info_list");
			$this -> view ->  display('suggestion.tpl');
		}
	}
 
	
	
	function info()
	{
		$idval = $this -> input ->  get["id"];

		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."brand  WHERE bra_id = '".$idval."' ORDER BY `bra_id` ";
		$info = $this -> db -> select_one($sql_query);
		
		$this -> view -> assign( 'edit_submit', $this-> lang['edit_submit']);
		$this -> view -> assign( 'txt_title', $this-> lang['edit_title']);
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=brand&act=edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('brand_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
		$bra_idval 				= $this -> input ->  post["bra_id"];
		$bra_nameval 			= $this -> input ->  post["bra_name"];
		$bra_logoval  			= $this -> input ->  post["bra_logo"];
		$bra_urlval  			= $this -> input ->  post["bra_url"];	
		$bra_listval 			= $this -> input ->  post["bra_list"];
		$bra_descval  			= $this -> input ->  post["bra_desc"];
		$bra_enabledval  		= $this -> input ->  post["bra_enabled"];

		if($bra_idval !='' and $bra_nameval !='' )
		{

			$updatesql = "UPDATE `".$this -> db -> prefix."brand` SET  bra_name = '".$bra_nameval."' , bra_logo = '".$bra_logoval."'  , bra_url = '".$bra_urlval."' , bra_list = '".$bra_listval."' , bra_desc = '".$bra_descval."' , bra_enabled = '".$bra_enabledval."' WHERE  bra_id = '".$bra_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=brand&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=brand&act=info&id=".$bra_idval);
			$this -> view ->  display('suggestion.tpl');
			
		}
		
	}
	
	function show()
	{
		//var_dump($this -> input);
		//exit();
		$infoid = $this -> input ->  get["id"];
		$up_show = " UPDATE `".$this -> db -> prefix."brand` SET bra_enabled = 1  WHERE  `bra_id` in ( $infoid ) ";
		$this -> db -> query_sql($up_show);
		
		$this -> view ->  assign('info_title',$_LANG['bd_showsusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=brand&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function hide()
	{		
		$infoid = $this -> input ->  get["id"];
		$up_hide = " UPDATE `".$this -> db -> prefix."brand` SET bra_enabled = 0  WHERE `bra_id` in ( $infoid ) ";
		$this -> db -> query_sql($up_hide);
		
		$this -> view ->  assign('info_title',$_LANG['bd_hidesusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=brand&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
		
}

