<?php
header("Content-type:text/html;charset=utf-8");  
session_start();
$session_admin = isset($_SESSION['admin']) ? $_SESSION['admin'] : ''; 
if ($session_admin == '') {
	die("Can not access");
}
require_once("../common.php");
checkadminrights($session_admin, 'rooms');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet" href="../css/base.css" />
<link rel="stylesheet" type="text/css" href="flexigrid/css/flexigrid.pack.css" />
<script type="text/javascript" src="../js/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="flexigrid/js/flexigrid.pack.js"></script>
<style type="text/css">
#searchbar ul { margin:0;padding:0;list-style:none;display:inline; }
#searchbar li { float:left;margin:0;padding:10px 10px 10px 0; }
</style>
<script type="text/javascript">
function chkform() {
	return true;
}
function setInnerText(obj, text) {
	if (document.all) {
		obj.innerText = text;
	} else {
		obj.textContent = text;
	}	
}
function onsearchtypechange() {
	var searchtype = document.getElementById('searchtype');
	var searchparam_title = document.getElementById('searchparam_title');
	setInnerText(searchparam_title, '');
	switch (searchtype.value) {
		case '1': setInnerText(searchparam_title, '讨论组ID:');break;
		case '2': setInnerText(searchparam_title, '讨论组名称:');break;
	}//switch
}
</script>
</head>
<body>

<?php
require_once("../common.php");

$ownerusertype = isset($_GET['ownerusertype']) ? $_GET['ownerusertype'] : '-1';
if ($ownerusertype == '') $ownerusertype = '-1';
$searchtype = isset($_GET['searchtype']) ? $_GET['searchtype'] : '0';
if ($searchtype == '') $searchtype = '0';
$searchparam = '';
if ($searchtype == '1') {
	$param_roomid = isset($_GET['searchparam']) ? $_GET['searchparam'] : '0'; 
	if ($param_roomid == '') $param_roomid = '0';
	if (strval($param_roomid) > 0) $searchparam = $param_roomid;
} else if ($searchtype == '2') {
	$param_roomname = isset($_GET['searchparam']) ? urldecode($_GET['searchparam']) : ''; 
	$searchparam = $param_roomname;
}
$searchmode = ($searchparam != '');
if (!$searchmode) $searchtype = '0';
$pagesize = '10';
$pageindex = isset($_GET['pageindex']) ? $_GET['pageindex'] : '1'; 
if ($pageindex == '') $pageindex = '1';

$selected1 = '';
$selected2 = '';
$selected3 = '';

if ($ownerusertype == '-1') {
	$selected1 = ' class="selected"';
} else if ($ownerusertype == '1') {
	$selected2 = ' class="selected"';
} else if ($ownerusertype == '0') {
	$selected3 = ' class="selected"';
}

$maxpageindex = $pageindex;

$curpageurl = 'rooms.php';
$firstpageurl = 'rooms.php';
$prevpageurl = 'rooms.php';
$nextpageurl = 'rooms.php';
$lastpageurl = 'rooms.php';

$methodname = 'Admin.SearchRooms';
$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
    '<ip>' . getip() . '</ip>' . 
	'<ownerusertype>' . $ownerusertype . '</ownerusertype>' . 
	'<searchtype>' . $searchtype . '</searchtype>' . 
	'<param_roomid>' . $param_roomid . '</param_roomid>' . 
	'<param_roomname>' . $param_roomname . '</param_roomname>' . 
	'<pagesize>' . $pagesize . '</pagesize>' . 
	'<pageindex>' . $pageindex . '</pageindex>' . 
	'</request>';
$responseinfo = '';
$responsexml = null;
$errorcode = ERRORCODE_UNKNOWN;

$cmd = new COM("OpenCom.Command");
$methodname = iconv('UTF-8', 'GBK//IGNORE', $methodname);
$requestinfo = iconv('UTF-8', 'GBK//IGNORE', $requestinfo);
if ($cmd->Execute($methodname, $requestinfo, $responseinfo, 5000)) {
	$responseinfo = iconv('GBK', 'UTF-8//IGNORE', $responseinfo);
	$responsexml = simplexml_load_string($responseinfo);
	$errorcode = $responsexml->errorcode;
}
$cmd = null;

switch($errorcode) {
	case ERRORCODE_OK: 
		$maxpageindex = $responsexml->maxpageindex;	
		if ($pageindex > $maxpageindex) $pageindex = $maxpageindex;

		$baseurl = 'rooms.php?ownerusertype=' . $ownerusertype . '&searchtype=' . $searchtype . '&searchparam=' . urlencode($searchparam);

		//curpageurl
		$curpageurl = $baseurl . '&pageindex=' . $pageindex;

		//firstpageurl
		$newpageindex = 1;
		$firstpageurl = $baseurl . '&pageindex=' . $newpageindex;

		//prevpageurl
		$newpageindex = $pageindex - 1;
		if ($newpageindex < 1) $newpageindex = 1;
		$prevpageurl = $baseurl . '&pageindex=' . $newpageindex;

		//nextpageurl
		$newpageindex = $pageindex + 1;
		if ($newpageindex > $maxpageindex) $newpageindex = $maxpageindex;
		$nextpageurl = $baseurl . '&pageindex=' . $newpageindex;

		//lastpageurl
		$newpageindex = $maxpageindex;
		$lastpageurl = $baseurl . '&pageindex=' . $newpageindex;
echo<<<EOT
	<div class="rightbar">
		<div id="searchbar" style="float:left;margin:0;padding:0;height:45px;text-align:left;font-size:13px;color:#000000;">
		<form action="rooms.php" method="get" id="form" onSubmit="return chkform();">
		<ul>
			<li>范围:&nbsp;<select id="ownerusertype" name="ownerusertype">
				<option value="-1"
EOT;
	if($ownerusertype==-1) echo(" selected ");
echo<<<EOT
>全部讨论组</option>
				<option value="1"
EOT;
	if($ownerusertype==1) echo(" selected ");
echo<<<EOT
>内部讨论组</option>
				<option value="0"
EOT;
	if($ownerusertype==0) echo(" selected ");
echo<<<EOT
>外部讨论组</option>
				</select>
			</li>
			<li>查找项:&nbsp;<select id="searchtype" name="searchtype" onchange="onsearchtypechange();">
				<option value="1"
EOT;
	if($searchtype==1) echo(" selected ");
echo<<<EOT
>讨论组ID</option>
				<option value="2"
EOT;
	if($searchtype==2) echo(" selected ");
echo<<<EOT
>讨论组名称</option>
				</select>
			</li>
			<li>
				<span id="searchparam_title">
EOT;
				switch($searchtype) {
					case 1: echo '讨论组ID:';break;
					case 2: echo '讨论组名称:';break;
					default: echo '讨论组ID:';
				}//switch
echo<<<EOT
</span>&nbsp;<input style="width:160px;" type="text" name="searchparam" value="{$searchparam}"/>
			</li>
			<li><input class="cssbtn" type="submit" value="查找"></li>
EOT;
			if ($searchmode == true) {
echo<<<EOT
			<li><div style="margin:0;padding:10px 0 0 0;"><a href="rooms.php?ownerusertype={$ownerusertype}">清除查找</a></div></li>
EOT;
			}//if
echo<<<EOT
		</ul>
		</form>
		</div>
	</div>

	<table class="grid1">
		<thead>
			<tr>
				<th width="48"></th>
				<th width="62">群ID</th>
				<th width="100">群名称</th>
				<th width="62">成员数</th>
				<th width="62">管理员数</th>
				<th width="100">设置</th>
				<th width="62">拥有者</th>
				<th width="62">创建者</th>
				<th width="100">创建时间</th>
				<th width="100">操作</th>
			</tr>
		</thead>	
        <tbody>
EOT;
		$itemindex = 0;
		foreach ($responsexml->rooms->room as $room) {
			$itemindex += 1;
			$itembgcolor = '#f2f2f2';
			if (($itemindex % 2) == 0) $itembgcolor = '#e9e9e9';

			$attrs = $room->attributes();

			$introductionstr = '简介:无';
			if ($attrs->introduction != '') {
			if (strlen($attrs->introduction) >= 33)  
				$introductionstr = '简介:' . substr($attrs->introduction, 0, 33) . '...';
			}

			$ownerusertypestr = '外部用户';
			if ($attrs->ownerusertype == 1) $ownerusertypestr = '内部用户';

			$joinsettingstr = '允许任何人';
			if ($attrs->joinsetting == 1) $joinsettingstr = '需要验证';
			if ($attrs->joinsetting == 2) $joinsettingstr = '不允许加入';

			//subpageurlparams
			$subpageurlparams = 'ownerusertype=' . $ownerusertype . '&searchtype=' . $searchtype . 
				'&searchparam=' . urlencode($searchparam) . '&pageindex=' . $pageindex .
				'&roomid=' . $attrs->roomid . '&roomname=' . urlencode($attrs->roomname);
echo<<<EOT
		<tr>
			<td><img width="40px" height="40px" style="" src="../images/room.gif"/></td>
			<td style="padding:12px 0 0 0;">$attrs->roomid</td>
			<td style="padding:12px 0 0 0;">$attrs->roomname</td>
			<td style="padding:12px 0 0 0;">$attrs->membercount</td>
			<td style="padding:12px 0 0 0;">$attrs->managercount</td>
			<td style="padding:12px 0 0 0;">$joinsettingstr</td>
			<td style="padding:12px 0 0 0;">$attrs->ownerid</td>
			<td style="padding:12px 0 0 0;">$attrs->creatorid</td>
			<td style="padding:12px 0 0 0;">$attrs->createtime</td>
			<td style="padding:12px 0 0 0;text-align:center">
				<a href="rooms_remove.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/remove.gif" title="删除" /></a>
			</td>
		</tr>
EOT;
		}//foreach
		if ($itemindex == 0) {
echo<<<EOT
		<tr>
			<td style="padding:12px 0 0 0;">null</td>
			<td style="padding:12px 0 0 0;">null</td>
			<td style="padding:12px 0 0 0;">null</td>
			<td style="padding:12px 0 0 0;">null</td>
			<td style="padding:12px 0 0 0;">null</td>
			<td style="padding:12px 0 0 0;">null</td>
			<td style="padding:12px 0 0 0;">null</td>
			<td style="padding:12px 0 0 0;">null</td>
			<td style="padding:12px 0 0 0;">null</td>
			<td style="padding:12px 0 0 0;">null</td>
		</tr>
EOT;
		}//if

echo<<<EOT
		</tbody>
	</table>
EOT;
	
		//当前是第X/X页,本页有X条记录
echo<<<EOT
		<div class="statusbar">
			<div style="float:left;margin:0;padding:0;width:200px;text-align:left;">
EOT;
		//第一页，上一页(((
		if ($pageindex == 1) {
echo<<<EOT
			<div style="float:left;margin:0;padding:8px 0 0 0;width:44px;text-align:center;"><span style="color:#808080">第一页</span></div>
			<div style="float:left;margin:0;padding:8px 0 0 0;width:44px;text-align:center;"><span style="color:#808080">上一页</span></div>
EOT;
		} else {
echo<<<EOT
			<div style="float:left;margin:0;padding:8px 0 0 0;width:44px;text-align:center;"><a href="{$firstpageurl}">第一页</a></div>
			<div style="float:left;margin:0;padding:8px 0 0 0;width:44px;text-align:center;"><a href="{$prevpageurl}">上一页</a></div>
EOT;
		}//)))第一页，上一页
		//下一页，最后页(((
		if ($pageindex == $maxpageindex) {
echo<<<EOT
			<div style="float:left;margin:0;padding:8px 0 0 0;width:44px;text-align:center;"><span style="color:#808080">下一页</span></div>
			<div style="float:left;margin:0;padding:8px 0 0 0;width:44px;text-align:center;"><span style="color:#808080">最后页</span></div>
EOT;
		} else {
echo<<<EOT
			<div style="float:left;margin:0;padding:8px 0 0 0;width:44px;text-align:center;"><a href="{$nextpageurl}">下一页</a></div>
			<div style="float:left;margin:0;padding:8px 0 0 0;width:44px;text-align:center;"><a href="{$lastpageurl}">最后页</a></div>
EOT;
		}//)))下一页，最后页
echo<<<EOT
			</div>
			<div style="float:left;margin:0;padding:8px 0 0 0;text-align:left;">当前是第{$pageindex}/{$maxpageindex}页,本页有{$itemindex}条记录.</div>
		</div>

	<script type="text/javascript">
	$(document).ready(function(){  
		var w = document.documentElement.clientWidth;
		var h = document.documentElement.clientHeight - 110;
		$('.grid1').flexigrid({height:h,singleSelect:true,resizable:false,showToggleBtn:false});
	}); 
	</script>
EOT;
		break;
	default:
echo<<<EOT
	<div class="msgbox">
		<div class="msgbox_text">
			<img width="40px" height="40px" align="absmiddle" src="../images/warning.gif" />&nbsp;讨论组列表获取失败！
		</div>
	</div>
EOT;
}
?>

</body>
</html>
