<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Dept extends HC_Controller
{
	//ע�����빹�캯��,ʹ���˹��캯�����ع����� parent::__construct();
	var $master_act = 'dept';
	var $info_prefix = 'dep_';
	function __construct()
	{
		parent::__construct();
		$this->load_lang('adminsys/dept');
		$this -> load('admin'); 
		$this -> admin -> online_admin();
		$this -> load_db();
		$this -> load('helper');
		$this -> view -> assign('master_act', $this -> master_act);

	}
	
	function info_list()
	{
 
			$this -> load('page',$this->lang['page']);
			$this -> page -> set_page_size(1000);
			
			//�б��ֶ���ʾ
			$show_arr = array('dep_id' => array('th'=>0,'td'=>''),'dep_name'=> array('th'=>0,'td'=>'') ,'dep_exttel'=> array('th'=>0,'td'=>''),'dep_enabled'=> array('th'=>0,'td'=>''),'dep_list'=> array('th'=>0,'td'=>''));
			$field_show = $this -> helper -> get_th_list( $show_arr , $list_val , $list_type , $lang_arr = $this->lang['field_list'] );
	
			$table_arr['table']		  = $this -> master_act;
			$table_arr['prefix'] 	  = $this -> db -> prefix;
			$table_arr['field_arr']   = array_keys($show_arr);
			$table_arr['field_arr'][] = 'dep_parent_id'; //�����һ������ID
			$table_arr['def_where']   = ' 1 ';
			$table_arr['group_by']    = '';
			$table_arr['order_by']    = 'dep_id';
			$table_arr['limit_val']   = $this -> page ->get_limit_val();
			
			$re_sql   = $this -> helper -> get_list_sql( $table_arr );
			//var_dump( $re_sql );
			$info_sm   = $this -> db -> select_info( $re_sql['sql_list'] );
			
			$info 	  = $this -> helper -> grade_info_sort( $info_sm , $key_id = 'dep_id', $parent_id = 'dep_parent_id', $grade_val = 6 );
			
			//var_dump($info);
			
			$count  = $this -> db -> select_one( $re_sql['sql_count']  );
			
			$page_str 	= $this -> page -> get_admin_page(  $count["CNUM"] ,'info_list');
			
			$this -> view -> assign('add_info',	 	$add_info);
			$this -> view -> assign('master',	 	$this -> master_act);
			$this -> view -> assign('master_title',	$this->lang['title_arr'][$this -> master_act]);
			$this -> view -> assign('colspan_val',	count($show_arr)+1);
			$this -> view -> assign('field_show',	$field_show);
			$this -> view -> assign('page_str',		$page_str);
			$this -> view -> assign('info',			$info);
			$this -> view -> display('dept_list.tpl');
			

	}

	function numcount( $startsid = 0 )
	{
		//���������������º����������
		if($this -> input ->  req["startsid"]!='')
		{
			$startsid  = $this -> input ->  req["startsid"];
		}

			$sql_query = " SELECT dep_id  FROM  ".$this -> db -> prefix."dow_sort  WHERE dep_id > '$startsid' GROUP BY dep_id  ORDER BY `dep_id`  LIMIT  0,1 ";
 
			$infoOne = $this -> db -> select_one( $sql_query );
			$whereadd = '';
			$sinid  = '';
			//�Ƿ�����ӷ���
			if($infoOne["dep_id"] != '')
			{
				$sql_query = " SELECT dep_id  FROM  ".$this -> db -> prefix."dow_sort  WHERE dep_parent_id = ".$infoOne["dep_id"]." GROUP BY dep_id  ORDER BY `dep_id` ";
				$sortInfo = $this -> db -> select_info( $sql_query );
				
				
				$sinid = $infoOne["dep_id"];
				if($sortInfo!='')
				{
					foreach( $sortInfo as $skey => $sval )
					{
						$sinid .= ','.$sval["dep_id"];
					}
					$whereadd .= " AND  dep_id IN ( ".$sinid." ) ";
					
				}else{
					$whereadd .= " AND  dep_id IN ( ".$infoOne["dep_id"]." ) ";
				}
				
				$sql_querycount = " SELECT count( dep_id ) as NUMCOUNT   FROM  ".$this -> db -> prefix."download WHERE dep_delete = 0   $whereadd  ";
				$infocount = $this -> db -> select_one( $sql_querycount );
				
				$sqlupdate = " UPDATE ".$this -> db -> prefix."dow_sort SET dep_count = '".$infocount["NUMCOUNT"]."' WHERE dep_id = '".$infoOne["dep_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=".$this -> master_act."&act=numcount&startsid=".$infoOne["dep_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=".$this -> master_act."&act=info_list");
				$this -> view ->  display('suggestion.tpl');	
			}
			//��ѯ����

	}
 
	function del( )
	{
		$idval = $this -> input ->  get["id"];
		
		if(!$idval){
			$indel = implode(',', $delArray);
		}else{
			$indel = $idval;
		}
		
		$deletesql ="DELETE FROM `".$this -> db -> prefix.$this -> master_act."` WHERE `dep_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=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function get_dept()
	{
		$sql_query = " SELECT dep_id, dep_name,dep_parent_id  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE 1 and dep_enabled = 1 GROUP BY dep_id ORDER BY `dep_list` limit 1000 ";
		return $infosm = $this -> db -> select_info($sql_query);
	}
 
	function info()
	{
		$act_type 	= $this -> input -> get["act_type"];
		
		if($act_type=='edit'){
			
			$id_val = $this -> input ->  get["id"];

			$sql_query = " SELECT *  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE dep_id = '".$id_val."' ORDER BY `dep_id` ";
			$info = $this -> db -> select_one($sql_query);
			
			//��ѡ���һ������ 
			$asInfo 	= $this -> get_dept();
			$sminfo 	= $this -> helper -> grade_info_sort( $asInfo , $key_id = 'dep_id', $parent_id = 'dep_parent_id', $grade_val = 4 );
			//var_dump($asInfo);
			
			$this -> view -> assign('txt_title',$this->lang['add_dep_title']);
			$this -> view -> assign('edit_submit', $this-> lang['edit']);
			$this -> view -> assign('info',$info);
			$this -> view -> assign('act_type',$act_type);
			$this -> view -> assign('sminfo',$sminfo);
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_info&act_type=edit");
			
			
		}else{
		

			$asInfo 	= $this -> get_dept();
			$sminfo 	= $this -> helper -> grade_info_sort( $asInfo , $key_id = 'dep_id', $parent_id = 'dep_parent_id', $grade_val = 4 );
			
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_info&act_type=add");
			
			$this -> view -> assign('act_type',$act_type);
			$this -> view ->  assign('txt_title',$this->lang['add_dep_title']);
			$this -> view -> assign('edit_submit', $this-> lang['add']);
			
		}

		$this -> view -> assign('master_title',	$this->lang['title_arr'][$this -> master_act]);
		$this -> view -> assign('txt_title', $this-> lang['add_title']);
		$this -> view -> assign('sminfo',$sminfo);
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('dept_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$dep_idval 			= $this -> input ->  funpost("dep_id");
		$dep_nameval 		= $this -> input ->  funpost("dep_name");
		$dep_parent_idval 	= $this -> input ->  funpost("dep_parent_id");
		$dep_exttel 		= $this -> input ->  funpost("dep_exttel");
		$dep_remark 		= $this -> input ->  funpost("dep_remark");
		$dep_enabledval 	= $this -> input ->  funpost("dep_enabled");
		$dep_listval 		= $this -> input ->  funpost("dep_list");
		$act_type 			= $this -> input ->  funpost("act_type");
		
		if($act_type=='edit')
		{
			if($dep_idval !='' and $dep_nameval !='' )
			{
	
				$update_sql = "UPDATE `".$this -> db -> prefix.$this -> master_act."` SET  dep_name = '".$dep_nameval."' , dep_parent_id = '".$dep_parent_idval."',dep_enabled = '".$dep_enabledval."' , dep_exttel = '".$dep_exttel."' ,dep_remark = '".$dep_remark."' ,  dep_list = '".$dep_listval."'   WHERE  dep_id = '".$dep_idval."' ";
				
				$this -> db -> query_sql( $update_sql );
				
				$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=".$this -> master_act."&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=".$this -> master_act."&act=info&act_type=edit&id=".$dep_idval);
				$this -> view ->  display('suggestion.tpl');
			}
			
		//��ӷ���
		}else{
			
			if($dep_nameval !='' and $dep_enabledval !='' and $dep_listval !='')
			{
				//ͬ������ͬ������ͬ������ͬ��
				$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE dep_name = '".$dep_nameval."' and dep_parent_id ='".$dep_parent_idval."' ORDER BY `dep_id` "; 
				$info = $this -> db -> select_one($sql_query);
				
				if( $info["CNUM"] < 1 )
				{
	
					$insertip ="INSERT INTO `".$this -> db -> prefix.$this -> master_act."` (`dep_name` , `dep_parent_id` , `dep_exttel` ,`dep_remark` ,`dep_enabled` , `dep_list` ) VALUES (  '$dep_nameval', '$dep_parent_idval','$dep_exttel','$dep_remark', '$dep_enabledval', '$dep_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=".$this -> master_act."&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=".$this -> master_act."&act=info&act_type=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=".$this -> master_act."&act=info&act_type=add");
				$this -> view ->  display('suggestion.tpl');	
					
			}
			
		}
		
	}
		
}
