<?php
	$acl=$_SESSION["acl"];
	if (strlen($acl)<1 || !strpos($acl,"<23>")) {	//没有权限
		ob_clean();
		header("Location: accesserror.html");
		exit;
	}
?>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无组别用户列表</title>
<base target="_self">

<body background="bkg.bmp" style="background-attachment: fixed; background-position-y:130;">

<center>
<?php
   $timestr=strftime("%Y-%m-%d %H:%M:%S");
   $db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
   mysql_select_db(get_cfg_var("SIPDB"),$db);
   $gpid=trim($_REQUEST["gpid"]);
   $ispostback=$_REQUEST["ispostback"];                      //是否为回送
   if ($ispostback=="y") {
         if ($_REQUEST["count"]!="") {
            $count=intval($_REQUEST["count"]);                 //当前页显示的无组别用户数
            for($i=1;$i<=$count;$i++) {
               $add=$_REQUEST["add".strval($i)];
               if ($add=="add") {                              //添加组成员
                  $userid=$_REQUEST["userid".strval($i)];
				  echo "userid=".$userid;
                  $rs=mysql_query("select loginname from users where userid='".$userid."'",$db);
                  if ($row = mysql_fetch_array($rs)) {
                     $loginname=$row["loginname"];
                     mysql_free_result($rs);
                     mysql_query("update users set gpid=".$gpid." where userid='".$userid."'",$db);
                     echo($loginname."添加成功<br>");
                     mysql_query("insert into operation values('".$_SESSION["operator"]."','".$timestr."','添加组别成员,组号：".$gpid.",用户ID：".$userid."')",$db );
				  }
                  else {
                     mysql_free_result($rs);
                  }
               }
            }
         }
   }
   $str=$_REQUEST["pageno"];                        //页号
   if ($str=="") {
      $str="1";
   }
	$page_no=intval($str);			//当前显示页号，因为可能需要多页显示
	$pagecount=20;		//每页显示多少条记录
	$count=0;
	$allpage=0;
    $sqlstr= "select count(userid) from users where gpid=0 and checked='n'"; 
	$rs = mysql_query( $sqlstr,$db );
	//计算总记录数，计算共有多少页
	if ($row = mysql_fetch_array($rs)) {			//有记录
		$count=$row[0];
	}
	mysql_free_result($rs);
	if ($count<1) {
		echo("<p align='center'>没有无组别用户</p>");
	}
	else {
		$allpage=(int)($count/$pagecount);
		if ($allpage*$pagecount<$count) {		//不是整除
			$allpage=$allpage+1;
		}
		if ($page_no>$allpage) {
			echo("页号超出记录范围!");
		}
		else {
		    $sqlstr= "select userid,loginname,username,userdept from users where gpid=0 and checked='n' order by userid";  
			$rs = mysql_query( $sqlstr,$db );
			if ($row = mysql_fetch_array($rs)) {			//有记录
				$n=1;
				while ($row && $n<=($page_no-1)*$pagecount) {		//移动行号直到当前页对应起始行号位置
					$row = mysql_fetch_array($rs);
					$n=$n+1;
				}
				if ($row) {
					 echo("<form name='form1'  method='POST' action='nogpuser.php'>");
					echo("<font size='3'><b>无组别用户列表</b></font>");
					echo("<table border='1' bordercolor='#008080' cellspacing='0' cellpadding='0'>");
					echo("<tr>");
					echo("    <td align='center'>序号</td>");
					echo("    <td align='center'>添加到该组</td>"); 
					echo("    <td align='center'>用户ID</td>"); 
					echo("    <td align='center'>用户姓名</td>"); 
					echo("    <td align='center'>用户部门</td>"); 
					echo("</tr>"); 
					$k=0;
					while ($row && $n<=$page_no*$pagecount) {		//显示当前页记录
						  $k=$k+1;
						  echo("<tr><td align='center'>".strval($n)."</td>");
						  echo("<td align='center'><input type=checkbox name=add".strval($k)." value=add></td>");
						  echo("<td align='center'><a href='queryuser_content.php?userid=".$row[0]."' target='_blank'>".$row[1]."</a></td>");
						  echo("<input type=hidden name=userid".strval($k)." value=".$row[0].">");
						  echo("<td align='center'>".$row[2]."&nbsp;</td>"); 
						  echo("<td align='center'>".$row[3]."</td>"); 
						  echo("</tr>");  
						  $row = mysql_fetch_array($rs);
						  $n=$n+1;
				   }
				   echo("</table>");
					echo("<br><input type=submit name=b1 value=添加><br>");
					echo("<input type=hidden name=count value=".strval($k).">");
					echo("<input type=hidden name=gpid value=".$gpid.">");
					echo("<input type=hidden name=ispostback value='y'>");
					echo("<input type=hidden name=pageno value=".strval($page_no).">");
					echo("</form >"); 
					echo("<form method=GET action=nogpuser.php><input type=hidden name=gpid value=".$gpid.">");
					echo("第 ".strval($page_no)." 页，共 ".strval($allpage)." 页。");
					if ($page_no>1) {			//有前一页
					   echo("<a href=nogpuser.php?gpid=".$gpid."&pageno=".strval($page_no-1).">前一页</a>");
					}
					if ($page_no<$allpage) {	//有后一页
					   echo("&nbsp;<a href=nogpuser.php?gpid=".$gpid."&pageno=".strval($page_no+1).">后一页</a>");
					}
					if ($allpage>2) {	//超过2页
						echo(" 到<input type=text size=3 name=pageno maxlength=3>页");
					}
					echo("</form>");
				}
				else {
					echo("页号已经超出记录范围!");
				}
			}
			else {
				echo("查询范围内没有上网记录!");
			}
			mysql_free_result($rs);
		}
	}
  mysql_close($db); 
 ?>
</center>
</body>

</html>
