<?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, 'accounts');
?>
<!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 onaccountsectionschange() {
	var accountsections = document.getElementById('accountsections');
	var beginuserid = '0'; 
	var enduserid = '0';
	var section = accountsections.value;
	if (section != 'all') {
		beginuserid = section.substring(0, section.indexOf('-')); 
		enduserid = section.substring(section.indexOf('-')+1); 
	}
	if ((beginuserid == '0') && (enduserid == '0')) {
		location.href = 'accounts.php';
	} else {
		location.href = 'accounts.php?beginuserid=' + beginuserid + '&enduserid=' + enduserid;
	}
}
function chkform() {
	return true;
}
</script>
</head>
<body>

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

$accounttype = 0;//外部用户 
$beginuserid = isset($_GET['beginuserid']) ? $_GET['beginuserid'] : '0'; 
$enduserid = isset($_GET['enduserid']) ? $_GET['enduserid'] : '0'; 
$pagesize = '10'; 
$pageindex = isset($_GET['pageindex']) ? $_GET['pageindex'] : '1'; 

if ($beginuserid == '') $beginuserid = '0';
if ($enduserid == '') $enduserid = '0';

$searchmode = false;

$beginuseridstr = '';
if ($beginuserid != '0') {
	$beginuseridstr = $beginuserid;
	$searchmode = true;
}

$enduseridstr = '';
if ($enduserid != '0') {
	$enduseridstr = $enduserid;
	$searchmode = true;
}

$maxpageindex = $pageindex;

$firstpageurl = 'accounts.php';
$prevpageurl = 'accounts.php';
$nextpageurl = 'accounts.php';
$lastpageurl = 'accounts.php';

/* GetAccountSections */

$AccountSectionsHtml = '';

$accounttype = 0; //外部帐号

$methodname = 'Admin.GetAccountSections';
$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
    '<ip>' . getip() . '</ip>' . 
	'<accounttype>' . $accounttype . '</accounttype>' . 
	'</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: 
		 $AccountSectionsHtml = '所属号段:&nbsp;<select id="accountsections" name="accountsections" onchange="onaccountsectionschange();" >' .
			'<option value="all">全部号段</option>';
		foreach ($responsexml->accountsections->item as $item) {
			$section = $item->beginuserid . '-' . $item->enduserid;
			$AccountSectionsHtml .= '<option value="' . $section . '"';
			if (($item->beginuserid == $beginuserid) and ($item->enduserid == $enduserid)) $AccountSectionsHtml .= ' selected';
			$AccountSectionsHtml .= '>'. $section . '</option>';;
		}//foreach
        $AccountSectionsHtml .= '</select>';
		break;
	default:
}//switch

/* SearchAccounts */

$methodname = 'Admin.SearchAccounts';
$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
    '<ip>' . getip() . '</ip>' . 
	'<accounttype>' . $accounttype . '</accounttype>' . 
	'<beginuserid>' . $beginuserid . '</beginuserid>' . 
	'<enduserid>' . $enduserid . '</enduserid>' . 
	'<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;	
		
		//firstpageurl
		$newpageindex = 1;
		$firstpageurl = 'accounts.php?beginuserid=' . $beginuserid . '&enduserid=' . $enduserid . '&pageindex=' . $newpageindex;

		//prevpageurl
		$newpageindex = $pageindex - 1;
		if ($newpageindex < 1) $newpageindex = 1;
		$prevpageurl = 'accounts.php?beginuserid=' . $beginuserid . '&enduserid=' . $enduserid . '&pageindex=' . $newpageindex;

		//nextpageurl
		$newpageindex = $pageindex + 1;
		if ($newpageindex > $maxpageindex) $newpageindex = $maxpageindex;
		$nextpageurl = 'accounts.php?beginuserid=' . $beginuserid . '&enduserid=' . $enduserid . '&pageindex=' . $newpageindex;

		//lastpageurl
		$newpageindex = $maxpageindex;
		$lastpageurl = 'accounts.php?beginuserid=' . $beginuserid . '&enduserid=' . $enduserid . '&pageindex=' . $newpageindex;

		//removeurlparams1
		$removeurlparams1 = 'beginuserid=' . $beginuserid . '&enduserid=' . $enduserid . '&removebeginuserid=' . $beginuserid . '&removeenduserid=' . $enduserid;

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="accounts.php" method="get" id="form" onSubmit="return chkform();">
		<ul>
	        <li>$AccountSectionsHtml</li>
			<li><input style="width:80px;" type="text" name="beginuserid" value="{$beginuseridstr}">&nbsp;到&nbsp;<input style="width:80px;" type="text" name="enduserid" value="{$enduseridstr}"></li>
			<li><input class="cssbtn" type="submit" value="查找"></li>
			<li><input class="cssbtn" type="button" value="添加" onclick="window.location='accounts_add.php'" /></li>
EOT;
			if ($searchmode == true) {
echo<<<EOT
			<li><input class="cssbtn" type="button" value="删除号段" onclick="location.href='accounts_remove.php?{$removeurlparams1}'"></li>
			<li><div style="margin:0;padding:10px 0 0 0;"><a href="accounts.php">清除查找</a></div></li>
EOT;
			}//if
echo<<<EOT
		</ul>
		</form>
		</div>
	</div>

	<table class="grid1">
		<thead>
			<tr>
				<th width="48"></th>
				<th width="100">发布的帐号</th>
				<th width="120">所属号段</th>
				<th width="80">当前状态</th>
				<th width="100">注册IP地址</th>
				<th width="100">注册时间</th>
				<th width="100">操作</th>
			</tr>
		</thead>	
        <tbody>
EOT;
		$itemindex = 0;
		foreach ($responsexml->accounts->item as $item) {
			$itemindex += 1;
			$itembgcolor = '#f2f2f2';
			if (($itemindex % 2) == 0) $itembgcolor = '#e9e9e9';

			//removeurlparams2
			$removeurlparams2 = 'beginuserid=' . $beginuserid . '&enduserid=' . $enduserid . '&removebeginuserid=' . $item->userid . '&removeenduserid=' . $item->userid;

			$registed = '<span style="color:#808080">未注册</span>';
			if ($item->registed == 1) $registed = '<span style="color:#2fb900">已注册</span>';

			$registertime = '<span style="color:#808080">无</span>';
			if ($item->registertime != '') $registertime = $item->registertime;

			$registeripaddr = '<span style="color:#808080">无</span>';
			if ($item->registeripaddr != '') $registeripaddr = $item->registeripaddr;
				
echo<<<EOT
		<tr>
			<td>
EOT;
		if ($item->registed == 1) {
echo<<<EOT
				<img width="40px" height="40px" style="" src="../images/man.gif"/>	
EOT;
		} else {
echo<<<EOT
				<img width="40px" height="40px" style="" src="../images/man_gray.gif"/>	
EOT;
		}
echo<<<EOT
			</td>
			<td style="padding:12px 0 0 0;">$item->userid</td>
			<td style="padding:12px 0 0 0;">$item->section</td>
			<td style="padding:12px 0 0 0;">$registed</td>
			<td style="padding:12px 0 0 0;">$registeripaddr</td>
			<td style="padding:12px 0 0 0;">$registertime</td>
			<td style="padding:12px 0 0 0;text-align:center">
				<a href="accounts_remove.php?{$removeurlparams2}"><img width="16px" height="16px" align="absmiddle" src="../images/remove.gif" title="删除" /></a>
			</td>
		</div>
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>
		</tr>
EOT;
		}//if

echo<<<EOT
		</tbody>
	</table>
EOT;
	
		//当前是第X/X页,本页有X条记录
echo<<<EOT
		<div class="statusbar">
			<div style="float:left;width:200px;">
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_btns">
			<img width="40px" height="40px" align="absmiddle" src="../images/warning.gif" />&nbsp;帐号列表获取失败！
		</div>
	</div>
EOT;
}
?>

</body>
</html>
