<?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, 'publicusers');
?>
<!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 0 0; }
</style>
<script type="text/javascript">
function onfaceurlerror(img) {
	img.src = "../images/man_gray.gif";
}
var jspost = function(action, values) {
    var id = Math.random();
    document.write('<form id="post' + id + '" name="post'+ id +'" action="' + action + '" method="post">');
    for (var key in values) {
        document.write('<input type="hidden" name="' + key + '" value="' + values[key] + '" />');
    }
    document.write('</form>');    
    document.getElementById('post' + id).submit();
}
//jsPost('b.html', {
//    'username': 'zhangsan',
//    'password': '123'
//});
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;
		case '3': setInnerText(searchparam_title, '昵称:');break;
		case '4': setInnerText(searchparam_title, '真实姓名:');break;
	}
}
</script>
</head>
<body>

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

function getfaceurl($useritem) {
	if ($useritem->facefile != '') {
		if (($useritem->loginstatus == 0) or ($useritem->loginstatus == 4)) {
			if ($useritem->sex == 2) {
				return "../images/women_gray.gif";
			} else {
				return "../images/man_gray.gif";
			}
		} else {
			return "../../faces/" . $useritem->facefile;
		}
	} else {
		if ($useritem->sex == 2) {
			if (($useritem->loginstatus == 0) or ($useritem->loginstatus == 4)) {
				return "../images/women_gray.gif";
			} else {
				return "../images/women.gif";
			}
		} else {
			if (($useritem->loginstatus == 0) or ($useritem->loginstatus == 4)) {
				return "../images/man_gray.gif";
			} else {
				return "../images/man.gif";
			}
		}
	}
}

function getsexstr($useritem) {
	if ($useritem->sex == 1) {
		return '男';
	} else if ($useritem->sex == 2) {
		return '女';
	} else {
		return '未填写';	
	}
}

function getagestr($useritem) {
	if ($useritem->age >= 0) {
		return $useritem->age . '岁';
	} else {
		return '未填写';	
	}
}

function getclientipaddrstr($useritem) {
	if ($useritem->clientipaddr != '') {
		return $useritem->clientipaddr;
	} else {
		return '未登录';	
	}
}

$searchtype = isset($_GET['searchtype']) ? $_GET['searchtype'] : '1';

$beginuserid = '-1';
$enduserid = '-1';
$nickname = ''; 
$actualname = ''; 

$searchparam = '';

if ($searchtype == '1') {
	$beginuserid = isset($_GET['searchparam']) ? $_GET['searchparam'] : '-1'; 
	if ($beginuserid == '') $beginuserid = '-1';
	$enduserid = $beginuserid;
	if ($beginuserid > 0) $searchparam = $beginuserid;
} else if ($searchtype == '2') {
	$username = isset($_GET['searchparam']) ? urldecode($_GET['searchparam']) : ''; 
	$searchparam = $username;
} else if ($searchtype == '3') {
	$nickname = isset($_GET['searchparam']) ? urldecode($_GET['searchparam']) : ''; 
	$searchparam = $nickname;
} else if ($searchtype == '4') {
	$actualname = isset($_GET['searchparam']) ? urldecode($_GET['searchparam']) : ''; 
	$searchparam = $actualname;
}

$sex = isset($_GET['sex']) ? $_GET['sex'] : '-1'; 
if ($sex == '') $sex = '-1';
$age = isset($_GET['age']) ? $_GET['age'] : '-1'; 
if ($age == '') $age = '-1';
$online = isset($_GET['online']) ? $_GET['online'] : '-1'; 
if ($online == '') $online = '-1';

$pagesize = '10'; 

$pageindex = isset($_GET['pageindex']) ? $_GET['pageindex'] : '1'; 
if ($pageindex == '') $pageindex = '1';

$maxpageindex = $pageindex;

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

$searchmode = ($searchparam != '') || ($sex != '-1') || ($age != '-1') || ($online != '-1');

$methodname = 'Admin.SearchPublicUsers';
$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
    '<ip>' . getip() . '</ip>' . 
	'<beginuserid>' . $beginuserid . '</beginuserid>' . 
	'<enduserid>' . $enduserid . '</enduserid>' . 
	'<username>' . $username . '</username>' . 
	'<nickname>' . $nickname . '</nickname>' . 
	'<actualname>' . $actualname . '</actualname>' . 
	'<sex>' . $sex . '</sex>' . 
	'<age>' . $age . '</age>' . 
	'<online>' . $online . '</online>' . 
	'<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 = 'publicusers.php?searchtype=' . $searchtype . '&searchparam=' . urlencode($searchparam) . 
			'&sex=' . $sex . '&age=' . $age . '&online=' . $online;
		
		//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="publicusers.php" method="get" id="form" onSubmit="return chkform();">
		<ul>
			<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>
				<option value="3"
EOT;
	if($searchtype==3) echo(" selected ");
echo<<<EOT
>昵称</option>
				<option value="4"
EOT;
	if($searchtype==4) echo(" selected ");
echo<<<EOT
>真实姓名</option>
				</select>
			</li>
			<li>
				<span id="searchparam_title">
EOT;
				switch($searchtype) {
					case 1: echo '用户ID:';break;
					case 2: echo '用户名:';break;
					case 3: echo '昵称:';break;
					case 4: echo '真实姓名:';break;
					default: echo '用户ID:';
				}//switch
echo<<<EOT
</span>&nbsp;<input style="width:100px;" type="text" name="searchparam" value="{$searchparam}"/>
			</li>
			<li>性别:&nbsp;
				<select name="sex">
				<option value="-1" 
EOT;
	if($sex==-1) echo(" selected ");
echo<<<EOT
>不限</option>
				<option value="1"
EOT;
	if($sex==1) echo(" selected ");
echo<<<EOT
>男</option>
				<option value="2"
EOT;
	if($sex==2) echo(" selected ");
echo<<<EOT
>女</option>
				</select>	
			</li>
			<li>年龄:&nbsp;
				<select name="age">
				<option value="-1" 
EOT;
	if($age==-1) echo(" selected ");
echo<<<EOT
>不限</option>
				<option value="1"
EOT;
	if($age==1) echo(" selected ");
echo<<<EOT
>0岁~18岁</option>
				<option value="2"
EOT;
	if($age==2) echo(" selected ");
echo<<<EOT
>19岁~24岁</option>
				<option value="3"
EOT;
	if($age==3) echo(" selected ");
echo<<<EOT
>25岁~30岁</option>
				<option value="4" 
EOT;
	if($age==4) echo(" selected ");
echo<<<EOT
>31岁~40岁</option>
				<option value="5"
EOT;
	if($age==5) echo(" selected ");
echo<<<EOT
>41岁~50岁</option>
				<option value="6"
EOT;
	if($age==6) echo(" selected ");
echo<<<EOT
>51以上</option>
				</select>	
			</li>
			<li><input type="checkbox" class="checkbox" name="online" value="1" 
EOT;
	if($online==1) echo(" checked ");
echo<<<EOT
/>当前在线</li>
			<li><input class="cssbtn" type="submit" value="查找"></li>
			<li><input class="cssbtn" type="button" value="添加" onclick="window.location='publicusers_add.php'" /></li>
EOT;
			if ($searchmode == true) {
echo<<<EOT
			<li><div style="margin:0;padding:10px 0 0 0;"><a href="publicusers.php">清除查找</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="100">昵称</th>
				<th width="48">性别</th>
				<th width="48">年龄</th>
				<th width="80">短信</th>
				<th width="100">注册时间</th>
				<th width="120">操作</th>
			</tr>
		</thead>	
        <tbody>
EOT;

		//用户列表(((
		$itemindex = 0;
		foreach ($responsexml->users->item as $item) {
			$itemindex += 1;
			$itembgcolor = '#f2f2f2';
			if (($itemindex % 2) == 0) $itembgcolor = '#e9e9e9';
			$facefile = getfaceurl($item);
			$sexstr = getsexstr($item);
			$agestr = getagestr($item);
			$clientipaddrstr = getclientipaddrstr($item);

			//subpageurlparams
			$subpageurlparams = 'userid=' . $item->userid . '&nickname=' . urlencode($item->nickname) . 
				'&searchtype=' . $searchtype . '&searchparam=' . urlencode($searchparam) . 
				'&sex=' . $sex . '&age=' . $age . '&online=' . $online . '&pageindex=' . $pageindex;

			$smshtml = '<td style="padding:12px 0 0 0;">未开通</td>';
			if ($item->smstotal > 0)
				$smshtml = '<td style="padding:7px 0 0 0;">最大:' . $item->smstotal . '<br>已发送:' . $item->smstotalsended . '</td>';
echo<<<EOT
		<tr>
			<td><a href="publicusers_modify.php?{$subpageurlparams}"><img width="40px" height="40px" title="点击修改资料" onerror="onfaceurlerror(this);" src="{$facefile}"/></a></td>
			<td style="padding:12px 0 0 0;">$item->userid</td>
			<td style="padding:12px 0 0 0;">$item->username</td>
			<td style="padding:12px 0 0 0;">$item->nickname</td>
			<td style="padding:12px 0 0 0;">$sexstr</td>
			<td style="padding:12px 0 0 0;">$agestr</td>
			$smshtml
			<td style="padding:12px 0 0 0;">$item->createtime</td>
			<td style="padding:12px 0 0 0;text-align:center">
				<a href="publicusers_modify.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/modify.gif" title="修改资料" /></a>
				<a href="publicusers_smssetting.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/sms.gif" title="短数设置" /></a>
				<a href="publicusers_resetpwd.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/pwd.gif" title="重设密码" /></a>
EOT;
			if ($item->loginstatus != '0') {
echo<<<EOT
				<a href="publicusers_offline.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/offline.gif" title="强制下线" /></a>
EOT;
			} else {
echo<<<EOT
				<img width="16px" height="16px" align="absmiddle" src="../images/offline2.gif" title="强制下线(访用户当前未登录)" />
EOT;
			}//if ($item->loginstatus
			if ($item->locked == '1') {
echo<<<EOT
				<a href="publicusers_unlock.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/unlock.gif" title="开通帐号" /></a>
EOT;
			} else {
echo<<<EOT
				<a href="publicusers_lock.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/lock.gif" title="冻结帐号" /></a>
EOT;
			}//if ($item->islock
echo<<<EOT
				<a href="publicusers_remove.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/remove.gif" title="删除帐号" /></a>
			</td>
		</tr>
EOT;
		}//)))用户列表


		//未设置用户(((
		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;
		}//)))未设置用户

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_text">
			<img width="40px" height="40px" align="absmiddle" src="../images/warning.gif" />&nbsp;帐号列表获取失败！
		</div>
	</div>
EOT;
}
?>

</body>
</html>
