<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Admins extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	var $master_act 	= 'admins';
	var $info_prefix 	= 'adm_';
	private $power_arr = array();
	function __construct()
	{
		parent::__construct();	
		$this -> load_lang('adminsys/'.$this -> master_act);
		$this -> load_lang('link');
		
		$this -> view -> assign('master_act', $this -> master_act);
		$this -> view -> assign('info_prefix',	$this -> info_prefix);

		$this -> load_model('admin'); 
		$this -> power_arr = $this -> admin_mod -> get_power( $this -> master_act );
		
		$this -> load_db();
		$this -> load('helper');     //加载辅助类
		$this -> load_model('sort'); //加载分类模型
		
	}


	function info_list(  )
	{
		
		
		//var_dump($pow_arr);
		
		$keyword 	= $this -> input -> req["keyword"];
		$list_val   = $this -> input -> req["list_val"];
		$list_type  = $this -> input -> req["list_type"];

		$table_arr['where_add'] = '';

		if($keyword!='')
		{ 	
			$table_arr['where_add'] .= " AND ( adm_user LIKE  '%".$keyword."%' )";  
		}
		

		$table_arr['order_by']  = 'adm_id';
		if($list_val!='' and $list_type =='DESC' )
		{
			$table_arr['order_by'] = $list_val ."  DESC ";
		}elseif($list_val!='' and $list_type =='ASC')
		{
			$table_arr['order_by'] = $list_val." ASC";
		}else{
			$table_arr['order_by'] =  'adm_id';
		}

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

		//列表字段显示
		$def_list_arr = array('adm_id' => array('th'=>1,'td'=>'id'),'adm_user'=> array('th'=>0,'td'=>''),'adm_email'=> array('th'=>1,'td'=>''),'adm_mobile'=> array('th'=>1,'td'=>''),'adm_add_time'=> array('th'=>1,'td'=>'time'),'adm_login_count'=>array('th'=>1,'td'=>''),'adm_login_time'=>array('th'=>1,'td'=>'time'),'adm_enabled'=>array('th'=>1,'td'=>'arr'));
		$show_arr = $this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , $def_list_arr ,'info_list');
		
		
		$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['def_where'] = ' 1 ';
		$table_arr['group_by']  = '';
		$table_arr['limit_val'] = $this -> page ->get_limit_val();

		$re_sql   = $this -> helper -> get_list_sql( $table_arr );

		
		$info 	  = $this -> db -> select_info( $re_sql['sql_list'] );
		
		$key_arr['adm_enabled'] = $this->lang['enabled_arr'];


		$opt = array('item_list'=> $this->lang['item_list'], 'edit'=> $this->lang['edit'],'delete'=> $this->lang['delete']);
		
		$count 	  = $this -> db -> select_one( $re_sql['sql_count'] );

		$page_str = $this -> page ->get_admin_page( $count["CNUM"] );
		
		$this -> view -> assign('keyword',		$keyword);
		$this -> view -> assign('disabled_adm_user',	EMPLOYEE_BASEUSER);
		$this -> view -> assign('list_val',		$list_val);
		$this -> view -> assign('list_type',	$list_type);
		
		$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('td_rows',		$info);
		
		$this -> view -> display('admin_list.tpl');
		
	}

 

	function delete( )
	{
		$this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , '' ,'delete' );
		
		$infoid = $this -> input ->  get["id"];
		
		
		$insert_sql = " DELETE FROM `".$this -> db -> prefix.$this -> master_act."` WHERE `adm_id` in ( $infoid ) and adm_user <> '".EMPLOYEE_BASEUSER."' ";

		$this -> db -> query_sql($insert_sql);
		$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 show( )
	{
		$info_id = $this -> input ->  get["id"];
		$upshow = " UPDATE `".$this -> db -> prefix.$this -> master_act."` SET adm_enabled = 1  WHERE `adm_id` in ( $info_id ) ";
		$this -> db -> query_sql($upshow);
		$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=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}


	function hide( )
	{
		
		$info_id = $this -> input ->  get["id"];
		
		$up_hide = " UPDATE `".$this -> db -> prefix.$this -> master_act."` SET adm_enabled = 0  WHERE  `adm_id` in ( $info_id ) ";
		$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=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	
	function info()
	{
		$act_type 	= $this -> input -> get["act_type"];
		
		//$show_arr = $this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , $def_info_arr ,'info_list'  );
		
		if($act_type=='edit'){
			
			$this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , $show_arr ,'edit_info' );

			
			$id_val = $this -> input ->  get["id"];
			
			$sql_query = " SELECT *  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE adm_id = '".$id_val."' ORDER BY `adm_id` ";
			$info = $this -> db -> select_one($sql_query);

			$this -> view -> assign('txt_title',  $this->lang['edit_title']);
			$this -> view -> assign('edit_submit', $this->lang['edit']);
			$this -> view -> assign('info',$info);
			$this -> view -> assign('act_type',$act_type);
			$this -> view -> assign('sort_select_str',$sort_select_str);
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_info&act_type=edit");
			
			
		}else{
			$this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , $show_arr ,'add_info' );
			
			$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_title']);

			$this -> view -> assign('edit_submit', $this-> lang['add']);
			
		}

		$this -> view ->  display('admin_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$adm_id 		= $this -> input ->  funpost("adm_id");
		$adm_user 		= $this -> input ->  funpost("adm_user");
		$adm_email 		= $this -> input ->  funpost("adm_email");
	    $adm_mobile 	= $this -> input ->  funpost("adm_mobile");
		$adm_pwd 		= $this -> input ->  funpost("adm_pwd");
		$re_adm_pwd 	= $this -> input ->  funpost("re_adm_pwd");
		$adm_enabled 	= $this -> input ->  funpost("adm_enabled");
		
		$act_type 		= $this -> input ->  req["act_type"];

		if( $act_type=='edit' )
		{

		
			if( $adm_email !='' and $adm_mobile !=''  and $adm_id !='')
			{
				if($adm_pwd==''){
					$update = "UPDATE `".$this -> db -> prefix.$this -> master_act."` SET adm_email = '".$adm_email."' , adm_mobile = '".$adm_mobile."' ,  adm_enabled = '".$adm_enabled."'  WHERE  adm_id = '".$adm_id."' ";
					
				}else{
				
					$update = "UPDATE `".$this -> db -> prefix.$this -> master_act."` SET adm_email = '".$adm_email."' , adm_mobile = '".$adm_mobile."' , adm_pwd = '".md5($adm_pwd)."',  adm_enabled = '".$adm_enabled."'  WHERE  adm_id = '".$adm_id."' ";
				}
				$this -> db -> query_sql( $update );
				
				$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=".$adm_id);
				$this -> view ->  display('suggestion.tpl');	
			}
		

		}else{
			
				if( $adm_user !='' and $adm_email !='' and $adm_pwd !='')
				{
					$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE adm_user = '".$adm_user."' ORDER BY `adm_id` ";
					$info = $this -> db -> select_one($sql_query);
					
					if( $info["LNUM"] > 0 )
					{
						$this -> view ->  assign('info_title',$this->lang['err_user_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{
						$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE adm_email = '".$adm_email."' ORDER BY `adm_id` ";
						$info = $this -> db -> select_one($sql_query);
						
						if( $info["LNUM"] > 0 )
						{
							$this -> view ->  assign('info_title',$this->lang['err_email_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{
							
							$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE adm_mobile = '".$adm_mobile."' ORDER BY `adm_id` ";
							$info = $this -> db -> select_one($sql_query);
							
							if( $info["LNUM"] > 0 )
							{
								$this -> view ->  assign('info_title',$this->lang['err_mobile_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{
								//可注册
								$adm_add_time = time();
								
								$insert_sql =" INSERT INTO `".$this -> db -> prefix.$this -> master_act."` (`adm_user` , `adm_email` ,  `adm_mobile` ,`adm_pwd`  , `adm_enabled`,`adm_add_time` ) VALUES (  '$adm_user', '$adm_email', '$adm_mobile','".md5($adm_pwd)."', '$adm_enabled','$adm_add_time')";
								
								$this -> db -> query_sql($insert_sql);
								$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_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');
				}
		}
		
	}




	
	function item_list()
	{	
		$this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , $show_arr ,'edit_info' );
		$id_val = $this -> input ->  get["id"];

		$sql_query = " SELECT adm_id ,adm_user,adm_email FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE adm_id = '".$id_val."' ORDER BY `adm_id` ";
		$info 	   = $this -> db -> select_one($sql_query);


		//查询权限
		$sql_power = " SELECT adm_id ,admp_item,admp_action FROM  ".$this -> db -> prefix."adm_power  WHERE adm_id = '".$id_val."' ";
		$info_power  = $this -> db -> select_info($sql_power);
		$all_power = array();
		foreach($info_power as $pkey => $pval)
		{
			$item_arr[] = $pval['admp_item'];
			$all_power[$pval['admp_item']] = json_decode($pval['admp_action']);
 
			foreach((array)$all_power[$pval['admp_item']] as $tkey => $tval)
			{
 
				if(is_array($tval)){
 
				
					$item_arr_two[$pval['admp_item']][] = $tkey;
					foreach((array)$tval as $ttkey => $ttval)
					{
						$item_arr_three[$pval['admp_item']][$tkey][] =  $ttval;	
					}
				}else{
					$item_arr_two[$pval['admp_item']][] = $tval;
					//echo $tval;	
				}
			}
		}
 
		$power_arr = $this->lang['power_arr'];
		foreach((array)$power_arr as $arr_key => $arr_val )
		{
			if(!empty($item_arr)){
				if(in_array($arr_key,$item_arr)){
					$power_arr[$arr_key]['is_select'] = true;
					
					foreach((array) $arr_val['m']  as $two_key => $two_val )
					{
						//echo $two_key;
						if(!empty($item_arr_two[$arr_key])){
							
							if(in_array($two_key,$item_arr_two[$arr_key])){
								$power_arr[$arr_key]['m'][$two_key]['is_select'] = true;
								
	
									foreach((array) $two_val['arr']  as  $three_key => $three_val )
									{
										//var_dump($item_arr_three[$arr_key][$two_key]);
										if(!empty($item_arr_three[$arr_key][$two_key])){
											if(in_array($three_key,$item_arr_three[$arr_key][$two_key])){
												$power_arr[$arr_key]['m'][$two_key]['arr'][$three_key] = true;
											}else{
												$power_arr[$arr_key]['m'][$two_key]['arr'][$three_key] = false;	
											}
										}
										
									}
								
								
							}else{
								$power_arr[$arr_key]['m'][$two_key]['is_select'] = false;	
							}
						}
						
					}				
					
					
				}else{
					$power_arr[$arr_key]['is_select'] = false;	
				}
			}
			//echo '<br>';
		}
		
		//var_dump( $power_arr );
		
		//exit();
		$this -> view -> assign('power_arr',  $power_arr);
 		$this -> view -> assign('txt_title',  $this->lang['edit_title']);
		$this -> view -> assign('edit_submit',  $this->lang['edit']);
		
		$this -> view ->  assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_item");
		
		$this -> view ->  assign('colspan_val',	count($show_arr)+1);
		$this -> view ->  assign('field_show', $field_show);
		$this -> view ->  assign('id', $id_val);
		$this -> view ->  assign('info',$info);
		$this -> view ->  assign('info_item',$info_item);
		$this -> view ->  display('admin_power_list.tpl');
		
	}
	
	private function edit_adm_power( $type ='edit', $sm_info = array() )
	{
				if($type =='edit'){
					$update_sql = " UPDATE `".$this -> db -> prefix."adm_power` SET  admp_form = '".$sm_info['admp_form']."' , admp_action='".$sm_info['admp_action']."' WHERE  adm_id = ".$sm_info['adm_id']." and  admp_item = '".$sm_info['admp_item']."' ";
				}else{
					$update_sql = "INSERT INTO `".$this -> db -> prefix."adm_power` (`adm_id` ,`admp_form` , `admp_item` , `admp_action` )
 VALUES ( '".$sm_info['adm_id']."', '".$sm_info['admp_form']."', '".$sm_info['admp_item']."', '".$sm_info['admp_action']."') ";
				}
				$this -> db -> query_sql( $update_sql );	
	}

	function edit_item()
	{	
	
	
		$id_val 		= $this -> input ->  funpost("id");	
		$menu 			= $this -> input ->  funpost('menu');
		$menu_item 		= $this -> input ->  funpost('menu_item');
 
		//var_dump($menu_item);
		
		//exit();
		
		$sql_query = " SELECT admp_item FROM  ".$this -> db -> prefix."adm_power  WHERE adm_id = '".$id_val."' ORDER BY `adm_id` ";
		$info 	   = $this -> db -> select_info($sql_query);
		
		$newinfo = array_column($info, 'admp_item');
		
		//合并数组
		
		
		$del_sql = " DELETE FROM  ".$this -> db -> prefix."adm_power  WHERE adm_id = '".$id_val."' ";
		$this -> db -> query_sql( $del_sql );
		

 
		$all_menu = array();
 
		if(!empty($menu_item)){
				$all_menu = array_merge_recursive($menu_item, $all_menu);
		}
		
		//echo '<br><br>';
		//var_dump($menu_item);
		
		//exit();
		
		foreach((array)$all_menu as $mkey => $mval)
		{
			$add_info = array();
			
			$add_info['adm_id'] 	= $id_val ;
			//$add_info['admp_form'] 	= $id_val ;
			$add_info['admp_item']	 = $mkey ;
			//exit();
			$add_info['admp_action'] = json_encode($mval) ;

			//if($mval)
			if(empty($info))
			{
				//echo 'add';
				$this -> edit_adm_power('add',$add_info);
			}elseif(in_array($mval,$newinfo)){
				//echo 'edit';
				///var_dump($add_info);
				///exit();
				
				$this -> edit_adm_power('edit',$add_info);
			}else{
				$this -> edit_adm_power('add',$add_info);
				//echo 'add';	
			}
		}


		$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=item_list&id=".$id_val);
		$this -> view ->  display('suggestion.tpl');
		
 

		//exit();
		/*
		$voi_idval 		= $this -> input ->  funpost("voi_id");
		$voi_scoreval 	= $this -> input ->  funpost("voi_score");
		$voi_nameval 	= $this -> input ->  funpost("voi_name");

		$act_type 	= $this -> input -> get["act_type"];
		
 
			if($voi_idval !='' and  $voi_nameval !='' )
			{
				$voi_up_time_val =  time();
				
				$updatesql = " UPDATE `".$this -> db -> prefix."admin_item` SET  voi_name = '".$voi_nameval."' , voi_score = '".$voi_scoreval."' WHERE  voi_id = '".$voi_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=".$this -> master_act."&act=item_list&id=".$id_val);
				$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=item_list&id=".$id_val);
				$this -> view ->  display('suggestion.tpl');	
			}
			*/
	 

	}



	function pwd()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24	
		//var_dump($_SESSION);
		//$this -> view ->  assign('title',$this->lang['pwd_edit_title']);
		
		$this -> view ->  assign('use_name',$_SESSION["admin"]["adm_user"]);
		$this -> view ->  assign('act_url',ADMIN_WEBURL."/?load=admins&act=act_pwd");
		$this -> view ->  display('pwd_edit.tpl');	
		//exit();
	}


	
	function act_pwd()
	{

		
		$adm_old_pwd 	= $this -> input ->  post["adm_old_pwd"];
		$adm_pwd		= $this -> input ->  post["adm_pwd"];
		$re_adm_pwd 	= $this -> input ->  post["re_adm_pwd"];
		
		//echo md5('123456');
		//exit();
		$use_id 		= $_SESSION["admin"]["adm_id"];
		
		if($adm_old_pwd =='' or $adm_pwd =='' or $re_adm_pwd =='' ){
			//修改有误!
			$this -> view ->  assign('info_title',$this->lang['err_not_empty']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/?load=admins&act=pwd");	
			$this -> view ->  display('suggestion.tpl');
			
		}else{

			if( $adm_pwd <> $re_adm_pwd ){
				//修改有误!
				$this -> view ->  assign('info_title',$this->lang['err_re_adm_pwd']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/?load=admins&act=pwd");	
				$this -> view ->  display('suggestion.tpl');

			}else{ 
				$sql = " SELECT adm_id  FROM ".$this -> db -> prefix."admins WHERE adm_id= '".$use_id."' AND  adm_pwd = '".md5($adm_old_pwd)."' LIMIT 1 ";
				$admin = $this -> db -> select_one( $sql );
			
				if($admin["adm_id"]!='')
				{
		
					//修改成功！
					$sqlup = " UPDATE ".$this -> db -> prefix."admins  set  adm_pwd = '".md5($adm_pwd)."'  WHERE  adm_id= ".$use_id;
					$this -> db -> query_sql($sqlup);
					$this -> view ->  assign('info_title',$this->lang['edit_success']);
					$this -> view ->  assign('URL_TIME',URL_TIME);
					$this -> view ->  assign('get_url',ADMIN_WEBURL."/?load=admins&act=pwd");
					$this -> view ->  display('suggestion.tpl');
					//exit();
				}else{
					//修改有误!
					$this -> view ->  assign('info_title',$this->lang['err_old_pwd']);
					$this -> view ->  assign('URL_TIME',URL_TIME);
					$this -> view ->  assign('get_url',ADMIN_WEBURL."/?load=admins&act=pwd");	
					$this -> view ->  display('suggestion.tpl');
					
				}
			}
		}
		//exit();
	}	


}

