<?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, 'departments');
?>
<!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" />
</head>
<body style="background-color:#f2f2f2;">

<?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 ViewDepartUsers($departid, $departname) {
	$methodname = 'Admin.GetDepartmentUsers';
	$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
		'<ip>' . getip() . '</ip>' . 
		'<departid>' . $departid . '</departid>' . 
		'</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, 10000)) {
		$responseinfo = iconv('GBK', 'UTF-8//IGNORE', $responseinfo);
		$responsexml = simplexml_load_string($responseinfo);
		$errorcode = $responsexml->errorcode;
	}
	$cmd = null;

	switch($errorcode) {
		case ERRORCODE_OK: 
echo<<<EOT
<div style="margin:0;padding:0;width:100%;color:#757575;">
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);
			$usernamestr = $item->username;
			if ($item->userid != $item->username) {
				$usernamestr = $item->username . '(<span style="color:#ff0000;">' . $item->userid . '</span>)';
			}
			//subpageurlparams
			$subpageurlparams = 'departid=' . $departid . '&departname=' . urlencode($departname) . 
				'&userid=' . $item->userid . '&nickname=' . urlencode($item->nickname);

			$smshtml = '<div style="float:left;margin:0;padding:18px 0 0 0;width:180px;text-align:left;">未开通短信</div>';
			if ($item->smstotal > 0)
				$smshtml = '<div style="float:left;margin:0;padding:10px 0 0 0;width:180px;text-align:left;">短信条数限制:' . $item->smstotal . '<br>已发送短信:' . $item->smstotalsended . '</div>';
echo<<<EOT
	<div style="margin:0;padding:0;width:100%;height:50px;text-align:left;font-size:11px;color:#797979;background-color:{$itembgcolor};">
		<div style="float:left;margin:0;padding:5px 10px 5px 10px;width:40px;height:40px;text-align:left;">
			<a href="departments_modifyuser.php?{$subpageurlparams}"><img width="40px" height="40px" title="点击修改资料" onerror="onfaceurlerror(this);" src="{$facefile}"/></a>
		</div>
		<div style="float:left;margin:0;padding:10px 0 0 0;width:180px;text-align:left;">
			用户ID:&nbsp;$item->userid<br>
			用户名:&nbsp;$usernamestr
		</div>
		<div style="float:left;margin:0;padding:10px 0 0 0;width:140px;text-align:left;">
			昵称:&nbsp;$item->nickname<br>
			姓名:&nbsp;$item->actualname
		</div>
		$smshtml
		<div style="float:right;margin:0;padding:18px 10px 0 0;width:100px;text-align:right;font-size:11px;">
			<a href="departments_moveupuser.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/up.gif" title="向上" /></a>
			<a href="departments_movedownuser.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/down.gif" title="向下" /></a>
			<a href="departments_modifyuser.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/modify.gif" title="修改资料" /></a>
			<a href="departments_smssetting.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/sms.gif" title="短数设置" /></a>
			<a href="departments_removeuser.php?{$subpageurlparams}"><img width="16px" height="16px" align="absmiddle" src="../images/remove.gif" title="删除帐号" /></a>
		</div>
	</div>
EOT;
		}//)))用户列表
		if ($itemindex == 0) {
echo<<<EOT
	<div style="margin:0;padding:200px 0 0 0;width:100%;text-align:center;color:#757575;line-height:22px;">
		[$departname]<br/>
		当前部门未设置人员
	</div>
EOT;
		}
echo<<<EOT
</div>
EOT;
			break;
		default:
	}//switch
}//function ViewDepartUsers

function ViewNullUsers() {
echo<<<EOT
		<div style="margin:0;padding:200px 0 0 0;width:100%;text-align:center;color:#757575;">
			请选择一个部门查看
		</div>
EOT;
}

$departid = isset($_GET['departid']) ? $_GET['departid'] : ''; 
$departname = isset($_GET['departname']) ? urldecode($_GET['departname']) : ''; 

if ($departid != '') {
	ViewDepartUsers($departid, $departname);
} else {
	ViewNullUsers();
}
?>

</body>
</html>
