<?php
	!defined('DIRCMS_MANAGE') && exit('Access Denied!');
	if($_roleid!=1 && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');
	$action=empty($action)?'manage':trim($action);

	$c=new cache();
	include(DIRCMS_ROOT.'include/member.class.php');
	$member=new member();
	switch($action)
	{
		case 'manage':
			if($_GET['roleid'])
			{
				$__roleid=intval($_GET['roleid']);
				$result=$db->fetch_all("SELECT a.*,b.* FROM ".DB_PRE."member a LEFT JOIN ".DB_PRE."admin b ON a.id=b.memberid WHERE a.groupid=1 AND b.roleid={$__roleid}");
			}
			else
			{
				$result=$db->fetch_all("SELECT a.*,b.* FROM ".DB_PRE."member a LEFT JOIN ".DB_PRE."admin b ON a.id=b.memberid WHERE a.groupid=1");
			}
			unset($__roleid);
			if($result)foreach($result as $k => $r)
			{
				$t_roleid=explode(',',$r['roleid']);
				$role=array();
				if($t_roleid)foreach($t_roleid as $v_roleid)
				{
					$_role=cache_read('role'.$v_roleid.'.cache.php',DIRCMS_ROOT.'data/cache/');
					$role[]=$_role['name'];
				}
				$result[$k]['role']=$result[$k]['memberid']==ADMIN_FOUNDERS?'网站创始人':implode(',',$role);
			}
			include admin_tlp('admin_manage');			
			break;
		case 'add':
			if(isset($do_submit)) 
			{
				if(!$member->is_username($newadmin['username']))
				{
					showmsg('该会员名不可用，请先添加!',ADMIN_FILE.'?file=admin&action=add');
				}
				else
				{
					$r=$db->fetch_one("SELECT memberid FROM ".DB_PRE."admin WHERE username='{$data}'");
					if($r)showmsg('该管理员已存在!',ADMIN_FILE.'?file=admin&action=add');
					else
					{
						$newadmin['memberid']=$member->get_userid($newadmin['username']);					
						if(!$newadmin['roleid'])showmsg('请选择所属角色!',ADMIN_FILE.'?file=admin&action=add');
						if(!$newadmin['memberid'])showmsg('该会员名不存在，请先添加!',ADMIN_FILE.'?file=admin&action=add');
						$newadmin['allowmultilogin']=intval($newadmin['allowmultilogin']);
						$newadmin['roleid']=intval($newadmin['roleid']);
						$newadmin['categorypriv']=$categorypriv?implode(',',$categorypriv):'';
						$id=$db->insert(DB_PRE.'admin',$newadmin);
						$db->update(DB_PRE.'member',array('groupid'=>1),'id='.$newadmin['memberid']);
						$db->update(DB_PRE.'member_cache',array('groupid'=>1),'id='.$newadmin['memberid']);
						$c->cache_adminrole();
						showmsg('管理员添加成功!',ADMIN_FILE.'?file=admin&action=manage');
					}
				}
			}
			$r=cache_read('role.cache.php',DIRCMS_ROOT.'data/cache/');
			$roleselect='';
			if($r)foreach($r as $_r)
			{
				$roleselect.='<input type="radio" class="radio" value="'.$_r['id'].'" name="newadmin[roleid]" />'.$_r['name'].'&nbsp;&nbsp;';
			}
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=$select=new select();
			$s->getOption(0,DB_PRE.'category','categoryname','isshow=1');
			$catid_select='<select multiple="multiple" size="15" name="categorypriv[]" style="width:140px">';
			$catid_select.=$s->select;
			$catid_select.='</select>';
			include admin_tlp('admin_add');
			break;
		case 'edit':
			if(isset($do_submit)) 
			{	
				$newadmin['allowmultilogin']=intval($newadmin['allowmultilogin']);
				$newadmin['roleid']=intval($newadmin['roleid']);
				if(!$newadmin['roleid'])showmsg('请选择所属角色!');
				if($categorypriv)$newadmin['categorypriv']=implode(',',$categorypriv);
				$db->update(DB_PRE.'admin',$newadmin,'memberid='.$memberid);
				$c->cache_adminrole();
				showmsg('管理员编辑成功!',ADMIN_FILE.'?file=admin&action=manage');
			}
			$r=$db->fetch_one("SELECT * FROM ".DB_PRE."admin WHERE memberid=".$id);
			@extract($r);
			$r=cache_read('role.cache.php',DIRCMS_ROOT.'data/cache/');
			$roleselect='';
			if($r)foreach($r as $_r)
			{
				$ischecked=$roleid==$_r['id']?' checked="checked"':'';
				$roleselect.='<input type="radio" class="radio" '.$ischecked.' value="'.$_r['id'].'" name="newadmin[roleid]" />'.$_r['name'].'&nbsp;&nbsp;';
			}
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=$select=new select();
			$s->getOption(0,DB_PRE.'category','categoryname','isshow=1');
			$catid_select='<select multiple="multiple" size="15" name="categorypriv[]" id="categorypriv" style="width:140px">';
			$catid_select.=$s->select;
			$catid_select.='</select>';
			include admin_tlp('admin_edit');
			break;
		case 'lock':
			if($id==ADMIN_FOUNDERS)showmsg('网站创始人不能被禁用!',ADMIN_FILE.'?file=admin&action=manage');
			if($db->query("UPDATE ".DB_PRE."admin SET ".DB_PRE."admin.disabled=".intval($disabled)." WHERE ".DB_PRE."admin.memberid=".intval($id),true))
			{
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=admin&action=manage');
			}
			else 
			{
				showmsg($LANG['MANAGE_ERROR'],ADMIN_FILE.'?file=admin&action=manage');
			}
			break;
		case 'allowmultilogin':
			if($db->query("UPDATE ".DB_PRE."admin SET ".DB_PRE."admin.allowmultilogin=".intval($allowmultilogin)." WHERE ".DB_PRE."admin.memberid=".intval($id),true))
			{
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=admin&action=manage');
			}
			else 
			{
				showmsg($LANG['MANAGE_ERROR'],ADMIN_FILE.'?file=admin&action=manage');
			}
			break;
		case 'setroleid':
			if($id==ADMIN_FOUNDERS)showmsg('不能网站创始人进行此操作!',ADMIN_FILE.'?file=admin&action=manage');
			if($db->query("UPDATE ".DB_PRE."admin SET ".DB_PRE."admin.roleid=".intval($_GET['roleid'])." WHERE ".DB_PRE."admin.memberid=".intval($id),true))
			{
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=admin&action=manage');
			}
			else 
			{
				showmsg($LANG['MANAGE_ERROR'],ADMIN_FILE.'?file=admin&action=manage');
			}
			break;
		case 'delete':
			if($id==ADMIN_FOUNDERS)showmsg('网站创始人不能被撤销!',ADMIN_FILE.'?file=admin&action=manage');
			$db->mysql_delete(DB_PRE.'admin',intval($id),'memberid');
			$db->update(DB_PRE.'member',array('groupid'=>4),'id='.intval($id));
			cache_delete('admin'.$id.'_role.inc.php',DIRCMS_ROOT.'data/cache_admin/');
			cache_delete('admin'.$id.'_categorypriv.inc.php',DIRCMS_ROOT.'data/cache_admin/');
			$c->cache_adminrole();
			showmsg('管理员撤销成功!',ADMIN_FILE.'?file=admin&action=manage');
			break;
		case 'checkusername':		
			if((!$member->is_username($data) || !$member->get_userid($data)) && $data)exit('yes');
			else
			{
				$r=$db->fetch_one("SELECT memberid FROM ".DB_PRE."admin WHERE username='{$data}'");
				if($r)exit('yes');
				else exit('no');
			}
			break;
	}
?>