<?php
require_once(dirname(__FILE__)."/admin_inc.php");
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
viewHead("管理员管理"."-" .$menuList[3][0]);
$errorInfo="信息填写不完整，请检查" ; $errorPwd="两次密码不一致";
$action = getForm("action", "get");
switch ($action) {
	case "del" : delmanager();break;
	case "delall" : delAll();break;
	case "edit" : main();edit();break;
	case "save" : saveEdit();break;
	case "add" : addManager();break;
	default : main();add();break;
} 
viewFoot();

function main() {
	global $menuList;
	$id = getForm("id","get");
?>
<div id="append_parent"></div>
<div class="container" id="cpcontainer">
<!--当前导航-->
<script type="text/JavaScript">if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<?php echo $menuList[8][0];?>&nbsp;&raquo;&nbsp;管理员管理';</script>
<form  method="post" name="managerform">
<table class="tb">
<tr class="thead"><th colspan="7">管理员管理</th></tr>
    <TR align="center">
      <TD>ID</TD>
      <TD>用户名</TD>
       <TD>最近登录时间</TD>
       <TD>最近登陆IP</TD>
       <TD>管理员级别</TD>
       <TD>状态</TD>
      <TD>操作</TD>
    </TR>
    <?php
	$query = DB::query("SELECT m_id,m_username,m_loginip,m_logintime,m_level,m_state FROM ".DB::table('manager')." ORDER BY m_logintime DESC");
    while($row = DB::fetch($query)) {
    ?>
     <TR<?php if($id==$row['m_id']) echo " class=\"editlast\"";?> align="center">
      <TD><input type="checkbox" value="<?=$row['m_id']?>" name="m_id[]"   class="checkbox" /><?=$row['m_id']?></TD>
      <TD><?=$row['m_username']?></TD>
      <TD><?=isCurrentDay($row['m_logintime'])?></TD>
      <TD><?=$row['m_loginip']?></TD>
      <TD><?=getManagerLevel($row['m_level'])?></TD>
      <TD><?= getManagerState($row['m_state'])?></TD>
      <TD><a href="?action=edit&id=<?=$row['m_id']?>">编辑</a>&nbsp;&nbsp;<a onclick="if(confirm('确定要删除吗')){return true;}else{return false;}"  href="?action=del&id=<?=$row['m_id']?>">删除</a></TD>
    </TR>
   <?php
    }
  ?>
          <tr><td colspan="7">全选<input type="checkbox" name="chkall" id="chkall" class="checkbox" onclick="checkAll(this.checked,'input','m_id[]')" />反选<input type="checkbox" name="chkothers" id="chkothers" class="checkbox" onclick="checkOthers('input','m_id[]')" /><input type="submit" value="批量删除" onclick="if(confirm('确定要批量删除吗')){managerform.action='?action=delall';}else{return false}" class="btn"  />&nbsp;&nbsp;</td></tr>
</TABLE>
</form> 
<?php
}

function add() {
?>
<form action="?action=add" method="post" >
<table class="tb mt20">
<tr class="thead"><th colspan="2">添加管理员</th></tr>
    <TR>
      <TD vAlign=center width="20%" >用户名：</TD>
      <TD ><input type="text" size="30" name="m_username" id="m_username"  onBlur="isExistUsername('')"/><span id="checkmanagername" style="color:#FF0000"><font color="red">＊</font></span></TD>
    </TR>
    <TR>
      <TD >密码：</TD>
      <TD ><input type="password" size="30" name="m_pwd" /><font color="red">＊</font></TD>
    </TR>
   
   <TR>
      <TD >密码确认：</TD>
      <TD ><input type="password" size="30" name="m_pwd2" /><font color="red">＊</font></TD>
    </TR>
    <TR>
      <TD >管理员级别：</TD>
      <TD ><label><input type="radio" size="20" name="m_level"  value="0" checked class="radio"/>系统管理员(<font color="red">拥有全部权限</font>)</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input type="radio" size="20" name="m_level" value="1" class="radio" />网站编辑(<font color="red">只拥有数据管理权限</font>)</label><font color="red">＊</font></TD>
    </TR>
    
    <TR align="center"  >
    <TD align="left" ></TD>
      <TD align="left" ><input type="submit" value="添加管理员" class="btn" />
      &nbsp;<input type="button" value="返   回"  class="btn" onClick="javascript:history.go(-1)" /></TD>
    </TR>
</td></tr></table>
</form>
<?php
}

function edit() {
	$id = getForm("id","get");
	$row = DB::fetch_first("SELECT * FROM ".DB::table('manager')." WHERE m_id=$id");
?>
<form action="?action=save&id=<?=$id?>" method="post" name="editmanager" >
<table class="tb mt20">
<tr class="thead"><th colspan="2">编辑管理员</th></tr>
    <TR>
      <TD vAlign=center width="20%" >用户名：</TD>
      <TD ><input type="text" size="30" name="m_username"   value="<?=$row['m_username']?>" onBlur="isExistUsername('<?=$id?>')"/><span id="checkmanagername" style="color:#FF0000"><font color="red">＊</font></span></TD>
    </TR>
    <TR>
      <TD >密码：</TD>
      <TD ><input type="password" size="30" name="m_pwd"   value="<?=$row['m_pwd']?>" /><font color="red">＊</font></TD>
    </TR>
   
   <TR>
      <TD >密码确认：</TD>
      <TD ><input type="password" size="30" name="m_pwd2" value="<?=$row['m_pwd']?>" /><font color="red">＊</font></TD>
    </TR>
    <TR>
      <TD >管理员级别：</TD>
      <TD ><label><input type="radio" size="20" name="m_level" class="radio"  value="0" <?php if($row['m_level'] == 0){?> checked <?php }?>/>系统管理员(<font color="red">拥有全部权限</font>)</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input type="radio" class="radio" size="20" name="m_level" value="1"  <?php if($row['m_level'] == 1){?> checked <?php }?> />网站编辑(<font color="red">只拥有数据管理权限</font>)</label><font color="red">＊</font></TD>
    </TR>
     <TR>
      <TD >是否锁定</TD>
      <TD ><label><input type="radio" class="radio" size="20" name="m_state"  value="1" <?php if($row['m_state'] == 1){?> checked <?php }?>/>激活</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input type="radio" class="radio" size="20" name="m_state" value="0"  <?php if($row['m_state'] == 0){?> checked <?php }?> />锁定</label><font color="red">＊</font></TD>
    </TR>
    <TR align="center"  >
    <TD align="left" ></TD>
      <TD align="left" ><input type="submit" value="修改管理员" name="m_eidtsubmit" class="btn" id="m_eidtsubmit" />
      &nbsp;<input type="button" value="返   回"  class="btn" onClick="javascript:history.go(-1)" /></TD>
    </TR>
</td></tr></table>
</form>
<script>$("m_eidtsubmit").focus()</script>
<?php
}

function saveEdit() {
    global $errorInfo,$errorPwd;
	$m_username = getForm("m_username","post");$m_pwd = getForm("m_pwd","post");$m_pwd2 = getForm("m_pwd2","post");$m_level = getForm("m_level","post");$m_state=getForm("m_state","post");$id=getForm("id","get");
	if (empty($m_username) || empty($m_pwd)  || empty($m_pwd2))  die($errorInfo);
	if ($m_pwd != $m_pwd2) die($errorPwd);
	if (is_null($m_level)) $m_level = 0;
	if (is_null($m_state)) $m_state = 1;
	$num = DB::result_first("SELECT count(*) FROM ".DB::table('manager')." WHERE `m_id`<>'$id' AND m_username='$m_username'");
	if ($num > 0) die("已经存在此管理员，请更换名称");
	$vrow = DB::fetch_first("SELECT * FROM ".DB::table('manager')." WHERE m_id='$id'");
	if (is_array($vrow)) {
	    if ($m_pwd<>$row['m_pwd']){
	        DB::update('manager', array('m_pwd' => md5($m_pwd)),array('m_id' => $id));
		}
		DB::update('manager', array('m_username' => $m_username,'m_level' => $m_level,'m_state' => $m_state),array('m_id' => $id));
	}
	alertMsg("编辑成功", "admin_manager.php?id=$id");
}


function addManager() {
	global $errorInfo,$errorPwd;
	$m_username = getForm("m_username","post");$m_pwd = getForm("m_pwd","post");$m_pwd2 = getForm("m_pwd2","post");$m_level = getForm("m_level","post");
	if (empty($m_username) || empty($m_pwd)  || empty($m_pwd2))  die($errorInfo);
	if ($m_pwd != $m_pwd2) die($errorPwd);
	if (is_null($m_level)) $m_level=0;
	$num = DB::result_first("SELECT count(*) FROM ".DB::table('manager')." WHERE `m_username`='$m_username'");
	if ($num > 0) die("已经存在此管理员，请更换名称");
	$agerarr = array(
	'm_username' => $m_username,
	'm_pwd' => $m_pwd,
	'm_level' => $m_level,
	'm_state' => 1,
	);
	DB::insert('manager', $agerarr);
	alertMsg("添加成功", "admin_manager.php"); 
}

function delmanager() {
	$id = getForm("id","get");
	$row = DB::fetch_first("SELECT m_username FROM ".DB::table('manager')." WHERE m_id=$id");
	if ($row['m_username'] == getcookie("m_username")) {
		alertMsg("不能删除自身","admin_manager.php?id=$id");
	} else {
		DB::delete('manager', array('m_id' => $id));
		alertMsg("", "admin_manager.php");
	}
}

function delAll() {
	$ids = getForm("m_id","post");
	if (empty($ids)){ alertMsg("", "admin_manager.php");die("");}
	foreach ($ids as $id) {
		$row = DB::fetch_first("SELECT `m_username` FROM ".DB::table('manager')." WHERE `m_id`=".$id);
		if ($row['m_username'] != getcookie("m_username")) {
			DB::delete('manager', array('m_id' => $id));
		}
	}
	alertMsg("", "admin_manager.php");
}

function getManagerLevel($id) {
	if (is_null($id)) return "未知";
	if ($id == 0) return "系统管理员";
	if ($id == 1) return "网站编辑";
}

function getManagerState($id) {
	if (is_null($id)) return "未知";
	if ($id == 0) return "锁定";
	if ($id == 1) return "激活";
}
?>
