<?php
require 'inc.php';
$obj_tab_admin=new cls_tab_sys_admin();

if($get_act=="del"){
	$arr_return=$obj_tab_admin->on_del($get_id);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="del_sel"){
	$arr_return=$obj_tab_admin->on_del($get_adminid);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
switch($get_act)
{	
	case 'save':
	{
		$arr_fields=array();
		$arr_fields["admin_id"]=$get_id;
		$arr_fields["admin_user_id"]=$get_admin_user_id;
		$arr_fields["admin_group_id"]=$get_admin_group_id;
		$arr_fields["admin_name"]=$get_admin_name;
		if(isset($get_admin_pwd)&&$get_admin_pwd!="") $arr_fields["admin_pwd"]=$get_admin_pwd;
		$arr_fields["admin_tel"]=$get_admin_tel;
		$arr_fields["admin_address"]=$get_admin_address;
		$arr_fields["admin_attribute"]="";
		if(isset($get_admin_attribute)) $arr_fields["admin_attribute"]=implode(",",$get_admin_attribute);
		$arr_msg=$obj_tab_admin->on_save($arr_fields);
		if($arr_msg["err"]==""){
			$get_id=$arr_msg["id"];
			$arr_msg["act_info"]="成功注册管理员！";
			$arr_msg["act_act"]="<a href=\"javascript:frmExecute('add');\">添加</a> <a href=\"javascript:frmExecute('');\">管理</a>";

		}
		include 'tpl/_msg.tpl.php';
	    break;
	}
	case 'add':
	{
		$get_id=0;
		$page_admin_attribute=$obj_tab_admin->get_checkbox_html("attribute","admin_attribute");
		$page_group_list=me_get_admin_group(0,0);
		include "tpl/admin.add.tpl.php";
		break;
	}
	case 'edit':
	{
		$arr_admin=array();
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."sys_admin a left join ".DB_PRE."sys_user b on a.admin_user_id=b.user_id where admin_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$page_admin_attribute=$obj_tab_admin->get_checkbox_html("attribute","admin_attribute",$obj_rs["admin_attribute"]);
			$page_group_list=me_get_admin_group(0,0,$obj_rs["admin_group_id"]);
			$arr_admin = $obj_rs;
		}
		include 'tpl/admin.edit.tpl.php';
		break;
	}
	default:
	{
		//分页
		$str_where="";
		$arr_admin=array();
		$obj_rs   = $obj_db->get_one("SELECT count(*) as num FROM ".DB_PRE."sys_admin".$str_where);
		$int_total=$obj_rs["num"];
		$int_pages= ceil($int_total/$int_pagesize);
		$get_page = min($int_pages, $get_page);
		$get_page = max($get_page, 1);
		$page_split_page=get_pages($int_total, $get_page, $int_pagesize);
		$int_offset = $int_pagesize*($get_page-1);
		$str_limit = " limit ".$int_offset.",".$int_pagesize;
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."sys_admin a left join ".DB_PRE."sys_user b on a.admin_user_id=b.user_id left join ".DB_PRE."sys_admin_group c on a.admin_group_id=c.group_id".$str_where.$str_limit);
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["admin_addtime"] = format_date($obj_rs["admin_addtime"]);
			$obj_rs["admin_updatetime"] = format_date($obj_rs["admin_updatetime"]);
			$arr_admin[] = $obj_rs;
		}
		include "tpl/admin.tpl.php";

	}
}

function me_get_admin_group($msg_pid,$msg_layer=0,$msg_val=0){
	global $obj_db;
	$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."sys_admin_group where group_pid='".$msg_pid."'");
	$str_li="";
	$str_sp_x="　　";
	$str_sp="";
	for($i=0;$i<$msg_layer;$i++){
		$str_sp.=$str_sp_x;
	}
	while($obj_rs = $obj_db->fetch_array($obj_result))
	{
		$str_sel="";
		if($obj_rs["group_id"]==$msg_val) $str_sel=" selected";
		$str_li.="<option value='".$obj_rs["group_id"]."'".$str_sel.">".$str_sp.$obj_rs["group_name"]."</option>";
		$str_li.=me_get_admin_group($obj_rs["group_id"],$msg_layer+1,$msg_val);
	}
	return $str_li;
}

?>
