<?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, 'users');

function step2() {
	require_once("../common.php");

	$userid = isset($_GET['userid']) ? $_GET['userid'] : '0'; 
	$nickname = isset($_GET['nickname']) ? urldecode($_GET['nickname']) : '';
	$searchtype = isset($_GET['searchtype']) ? $_GET['searchtype'] : '1'; 
	$searchparam = isset($_GET['searchparam']) ? urldecode($_GET['searchparam']) : '';
	$sex = isset($_GET['sex']) ? $_GET['sex'] : '-1'; 
	$age = isset($_GET['age']) ? $_GET['age'] : '-1'; 
	$online = isset($_GET['online']) ? $_GET['online'] : '-1'; 
	$pageindex = isset($_GET['pageindex']) ? $_GET['pageindex'] : '1';

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

	$departid = isset($_POST['departid']) ? $_POST['departid'] : '0'; 
	$departname = isset($_POST['departname']) ? $_POST['departname'] : ''; 

	$methodname = 'Admin.ChangeUserDepartment';
	$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
		'<ip>' . getip() . '</ip>' . 
		'<userid>' . $userid . '</userid>' .	
		'<departid>' . $departid . '</departid>' .
		'<departname>' . $departname . '</departname>' .
		'</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: 
			header("location:   users.php?{$subpageurlparams}");
			break;
		default:
			header("location:   users.php?{$subpageurlparams}");
	}//switch
}//step2

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

if ($step == '2') {
	step2();
	exit;
}
?>
<!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" />
<style type="text/css">
.td1 { padding:5px 0 0 0;width:70px;height:20px;vertical-align:top; }
.td2 { width:880px;vertical-align:top; }
</style>
<script type="text/javascript">
String.prototype.trim=function() {
     return this.replace(/(^\s*)(\s*$)/g, '');
}
function chkform() {
	var form = document.getElementById('form');
	if (form.departments.selectedIndex >= 0) {
		var selvalue = form.departments.options[form.departments.selectedIndex].value;
		var seltext = form.departments.options[form.departments.selectedIndex].text;
		if (selvalue > 0) {
			form.departid.value = selvalue;
			form.departname.value = seltext;
		}
	}
    return true; 
}//chkform
</script>
</head>
<body>

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

	$userid = isset($_GET['userid']) ? $_GET['userid'] : '0'; 
	$nickname = isset($_GET['nickname']) ? urldecode($_GET['nickname']) : '';
	$searchtype = isset($_GET['searchtype']) ? $_GET['searchtype'] : '1'; 
	$searchparam = isset($_GET['searchparam']) ? urldecode($_GET['searchparam']) : '';
	$sex = isset($_GET['sex']) ? $_GET['sex'] : '-1'; 
	$age = isset($_GET['age']) ? $_GET['age'] : '-1'; 
	$online = isset($_GET['online']) ? $_GET['online'] : '-1'; 
	$pageindex = isset($_GET['pageindex']) ? $_GET['pageindex'] : '1';

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

	$methodname = 'Admin.GetDepartments';
	$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
	    '<ip>' . getip() . '</ip>' . 
		'<parentdepartid>' . '-1' . '</parentdepartid>' . 
		'</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;

	function LoadDepartments($childs) {
		$result = '';
		foreach($childs as $child) {				
			if ($child->getName() == 'department') { 
				$attrs = $child->attributes();	
				$offset = '';
				for ($i=0;$i<$attrs->indent;$i++) $offset .= '&nbsp;&nbsp;';			
				$result .= '<option value="' . $attrs->departid . '">' . $offset . $attrs->departname . '</option>';
				$result .= LoadDepartments($child->children()); 
			}//if
		}//foreach
		return $result;
	}//function LoadDepartments

	switch($errorcode) {
		case ERRORCODE_OK: 
			$departmentshtml = '<option value="0">请选择</option>';
			$departmentshtml .= LoadDepartments($responsexml->departments->children());
echo<<<EOT
	<div class="rightbar">
		<div class="form_title">
			为&nbsp;&quot;{$nickname}&quot;&nbsp;设置部门
		</div>
		<div class="form_panel">
			<form action="users_changedepart.php?step=2&{$subpageurlparams}" method="post" id="form" onSubmit="return chkform();">
			<input type="hidden" name="departid" value="0"/>
			<input type="hidden" name="departname" value=""/>
			<table style="border:0;">
			<tr>
				<td class="td1">新的部门:</td>
				<td class="td2" style="height:40px;">
					<select id="departments" style="width:260px;">{$departmentshtml}</select>	
				</td>
			</tr>
			<tr>
				<td class="td1"></td>
				<td class="td2"><input class="cssbtn" type="submit" value="设置">&nbsp;<input class="cssbtn" type="button" value="取消" onclick="window.location='users.php?{$subpageurlparams}'" /></td>
			</tr>
			</table>
			</form>
		</div>
	</div>
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 class="msgbox_btns">
			<input class="cssbtn" type="button" value="返回" onclick="window.location='users.php'" />
		</div>
	</div>
EOT;
	}//switch
?>

</body>
</html>
