<?php
require_once('common.php');
require_once('head.php');

$uid=is_numeric($_GET['uid'])?$_GET['uid']:'0';
if($_GET['do']=='del'){
	$db->query("delete from ".C('DB_PREFIX')."users where uid='$uid'");
	echo "<script language='javascript'>alert('删除成功！');</script>";
}elseif($_GET['do']=='daili'){
	if($uid && $row=$db->get_row("select daili from ".C('DB_PREFIX')."users where uid='$uid'")){
		if($row['daili']){
			$db->query("update ".C('DB_PREFIX')."users set daili=0 where uid='{$uid}'");
			echo "<script language='javascript'>alert('取消代理成功！');</script>";
		}else{
			$db->query("update ".C('DB_PREFIX')."users set daili=1 where uid='{$uid}'");
			echo "<script language='javascript'>alert('设为代理成功！');</script>";
		}
	}
}elseif($_GET['do']=='fuzhan'){
	if($uid && $row=$db->get_row("select fuzhan from ".C('DB_PREFIX')."users where uid='$uid'")){
		if($row['fuzhan']){
			$db->query("update ".C('DB_PREFIX')."users set fuzhan=0 where uid='{$uid}'");
			echo "<script language='javascript'>alert('取消副站长成功！');</script>";
		}else{
			$db->query("update ".C('DB_PREFIX')."users set fuzhan=1 where uid='{$uid}'");
			echo "<script language='javascript'>alert('设为副站长成功！');</script>";
		}
	}
}
$p=is_numeric($_GET['p'])?$_GET['p']:'1';
$pp=$p+8;
$pagesize=10;
$start=($p-1)*$pagesize;

if($_GET['do']=='search' && $s=safestr($_GET['s'])){
	$pagedo='seach';
	$users=$db->get_results("select * from ".C('DB_PREFIX')."users where uid='{$s}' or user like'%{$s}%' or mail like'%{$s}%' or phone like'%{$s}%' order by (case when uid='{$s}' then 8 else 0 end)+(case when user like '%{$s}%' then 3 else 0 end)+(case when mail like '%{$s}%' then 2 else 0 end)+(case when phone like '%{$s}%' then 1 else 0 end) desc limit 20");
}else{
	$pages=ceil(get_count('users','1=1','uid')/$pagesize);
	$users=$db->get_results("select * from ".C('DB_PREFIX')."users order by uid desc limit $start,$pagesize");
}
if($pp>$pages) $pp=$pages;
if($p==1){
	$prev=1;
}else{
	$prev=$p-1;
}
if($p==$pages){
	$next=$p;
}else{
	$next=$p+1;
}
?>
<!DOCTYPE html>
<title>用户管理-<?=C('webname')?> </title>
<body>
<!--main content start-->
  <section id="container" class="">
      <section id="main-content">
          <section class="wrapper">
		  <form action="?" class="navbar-right" method="GET">
				<input type="hidden" name="do" value="search">
			</form>
			<h3 class="page-header">用户列表</h3>
			<div class="table-responsive">
				<table class="table table-striped">
				<thead>
				<tr>
					<th>#UID</th>
					<th>用户名</th>
					<th>邮箱</th>
					<th>QQ</th>
					<th>手机</th>
					<th>注册时间</th>
					<th>注册IP</th>
					<th>最后登录</th>
					<th>操作</th>
				</tr>
				</thead>
				<tbody>
				<?php if($users){foreach($users as $user){?>
				<tr>
					<td><?=$user[uid]?></td>
					<td><?=$user[user]?></td>
					<td><?=$user[mail]?></td>
					<td><?=$user[qq]?></td>
					<td><?=$user[phone]?></td>
					<td><?=$user[regtime]?></td>
					<td><?=$user[regip]?>[<?=$user['city']?>]</td>
					<td><?=$user[lasttime]?></td>
					<td><a href="?do=del&p=<?=$p?>&uid=<?=$user[uid]?>" class="btn btn-danger" onClick="if(!confirm('确认删除？')){return false;}">删除</a>&nbsp;<a href="uset.php?xz=update&uid=<?=$user[uid]?>" class="btn btn-success">修改</a></td>
				</tr>
				<?php }}?>
				</tbody>
				</table>
			</div>
			<? if($pagedo!='seach'){?>
			<div class="row" style="text-align:center;">
				<ul class="pagination pagination-lg">
					<li <?php if($p==1){echo'class="disabled"';}?>><a href="?p=1">首页</a></li>
					<li <?php if($prev==$p){echo'class="disabled"';}?>><a href="?p=<?=$prev?>">&laquo;</a></li>
					<?php for($i=$p;$i<=$pp;$i++){?>
					<li <?php if($i==$p){echo'class="active"';}?>><a href="?p=<?=$i?>"><?=$i?></a></li>
					<?php }?>
					<li <?php if($next==$p){echo'class="disabled"';}?>><a href="?p=<?=$next?>">&raquo;</a></li>
					<li <?php if($p==$pages){echo'class="disabled"';}?>><a href="?p=<?=$pages?>">末页</a></li>
				</ul>
			</div>
			<?php } ?>
			
<?php
require_once('foot.php');
?>
  </body>
</html>
