<?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" />
<script src="../js/md5.js" type="text/javascript"></script>
<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.userid.value.trim() == "") { 
      alert('用户ID不能为空！'); 
      return false; 
    } 
    if(form.username.value.trim() == "") { 
      alert('用户名不能为空！'); 
      return false; 
    } 
	var newpwd = '123456';
	if(form.pwd1.value.trim() != "") { 
      newpwd = form.pwd1.value.trim();
    }
	var md5Value = hex_md5(newpwd);
	md5Value = md5Value.toUpperCase();
	if(md5Value == "") { 
      alert('登录密码MD5加密出错！'); 
      return false; 
    }
	form.pwd.value = md5Value;
    return   true; 
}//chkform
</script>
</head>
<body>

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

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

	//subpageurlparams
	$subpageurlparams = 'departid=' . $departid . '&departname=' . urlencode($departname);

echo<<<EOT
	<div class="rightbar">
		<div class="form_title">为&nbsp;&quot;{$departname}&quot;&nbsp;部门添加帐号</div>
		<div class="form_panel">
			<form action="departments_adduser.php?step=2&{$subpageurlparams}" method="post" id="form" onSubmit="return chkform();">
			<input type="hidden" name="pwd" value=""/>
			<table style="border:0;">
			<tr>
				<td class="td1">用户ID:</td>
				<td class="td2"><input style="width:260px;" type="text" name="userid" value=""/><span style="color:#ff0000;">&nbsp;*</span>&nbsp;<span style="color:#2fb900;">非"0"开头的9位以内数字</span></td>
			</tr>
			<tr>
				<td class="td1">用户名:</td>
				<td class="td2"><input style="width:260px;" type="text" name="username" value=""/><span style="color:#ff0000;">&nbsp;*</span>&nbsp;<span style="color:#2fb900;">最大长度为20个字符</span></td>
			</tr>
			<tr>
				<td class="td1">登录密码:</td>
				<td class="td2"><input style="width:260px;" type="text" name="pwd1" value=""/><span style="color:#ff0000;">&nbsp;*</span>&nbsp;<span style="color:#2fb900">密码为空时，自动设置默认密码(123456)</span></td>
			</tr>
			<tr>
				<td class="td1"></td>
				<td class="td2" style="height:40px;"><input type="checkbox" class="checkbox" name="canfound" value="0" />允许与外部帐号沟通</td>
			</tr>
			<tr>
				<td class="td1">昵称:</td>
				<td class="td2"><input style="width:260px;" type="text" name="nickname" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">真实姓名:</td>
				<td class="td2"><input style="width:260px;" type="text" name="actualname" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#2fb900;"></span></td>
			</tr>
			<tr>
				<td class="td1">性别:</td>
				<td class="td2">
					<select name="sex" style="width:266px;">
					<option value="0">请选择</option>
					<option value="1">男</option>
					<option value="2">女</option>
					</select>
				</td>
			</tr>
			<tr>
				<td class="td1">年龄:</td>
				<td class="td2">
					<select name="age" style="width:266px;">
					<option value="-1">请选择</option>
EOT;
for($i=0;$i<120;$i++) echo '<option value="' . $i . '">' . $i . '&nbsp;岁</option>';
echo<<<EOT
					</select>
				</td>
			</tr>
			<tr>
				<td class="td1">职位名称:</td>
				<td class="td2"><input style="width:260px;" type="text" name="jobtitle" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#2fb900;"></span></td>
			</tr>
			<tr>
				<td class="td1">工作编号:</td>
				<td class="td2"><input style="width:260px;" type="text" name="jobnumber" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#2fb900;"></span></td>
			</tr>
			<tr>
				<td class="td1">联系地址:</td>
				<td class="td2"><input style="width:260px;" type="text" name="address" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">电话:</td>
				<td class="td2"><input style="width:260px;" type="text" name="telephone" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">电话(短号):</td>
				<td class="td2"><input style="width:260px;" type="text" name="telephone_short" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">电话2:</td>
				<td class="td2"><input style="width:260px;" type="text" name="telephone2" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">电话2(短号):</td>
				<td class="td2"><input style="width:260px;" type="text" name="telephone2_short" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">手机:</td>
				<td class="td2"><input style="width:260px;" type="text" name="mobile" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">手机(短号):</td>
				<td class="td2"><input style="width:260px;" type="text" name="mobile_short" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">手机2:</td>
				<td class="td2"><input style="width:260px;" type="text" name="mobile2" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">手机2(短号):</td>
				<td class="td2"><input style="width:260px;" type="text" name="mobile2_short" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">传真:</td>
				<td class="td2"><input style="width:260px;" type="text" name="fax" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">QQ:</td>
				<td class="td2"><input style="width:260px;" type="text" name="qq" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">微信:</td>
				<td class="td2"><input style="width:260px;" type="text" name="weixin" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">微博:</td>
				<td class="td2"><input style="width:260px;" type="text" name="weibo" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">Email:</td>
				<td class="td2"><input style="width:260px;" type="text" name="email" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">科室名称:</td>
				<td class="td2"><input style="width:260px;" type="text" name="roomname" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">办公室号:</td>
				<td class="td2"><input style="width:260px;" type="text" name="roomnumber" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">身份证号:</td>
				<td class="td2"><input style="width:260px;" type="text" name="idnumber" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">车牌号:</td>
				<td class="td2"><input style="width:260px;" type="text" name="carnumber" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1">个人主页:</td>
				<td class="td2" style="height:40px;"><input style="width:260px;" type="text" name="homepage" value=""/>&nbsp;&nbsp;&nbsp;<span style="color:#33cc00;"></span></td>
			</tr>
			<tr>
				<td class="td1"></td>
				<td class="td2" style="height:60px"><input class="cssbtn" type="submit" value="添加">&nbsp;<input class="cssbtn" type="button" value="取消" onclick="window.location='departments_users.php?{$subpageurlparams}'"></td>
			</tr>
			</table>
			</form>
		</div>
	</div>
EOT;
}//step1

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

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

	//subpageurlparams
	$subpageurlparams = 'departid=' . $departid . '&departname=' . urlencode($departname);

    $userid = isset($_POST['userid']) ? $_POST['userid'] : '0'; 
	$usertype = 1;//EnterpriseUsers 
    $canfound = isset($_POST['canfound']) ? $_POST['canfound'] : '0'; 
    $username = isset($_POST['username']) ? $_POST['username'] : ''; 
    $nickname = isset($_POST['nickname']) ? $_POST['nickname'] : ''; 
	if (trim($nickname) == '') $nickname = $username;
    $actualname = isset($_POST['actualname']) ? $_POST['actualname'] : ''; 
    $sex = isset($_POST['sex']) ? $_POST['sex'] : '0'; 
    $age = isset($_POST['age']) ? $_POST['age'] : '-1'; 
    $birth_year = isset($_POST['birth_year']) ? $_POST['birth_year'] : '0'; 
    $birth_month = isset($_POST['birth_month']) ? $_POST['birth_month'] : '0'; 
    $birth_day = isset($_POST['birth_day']) ? $_POST['birth_day'] : '0'; 
    $province = isset($_POST['province']) ? $_POST['province'] : '0'; 
    $city = isset($_POST['city']) ? $_POST['city'] : '0'; 
    $address = isset($_POST['address']) ? $_POST['address'] : ''; 
    $telephone = isset($_POST['telephone']) ? $_POST['telephone'] : ''; 
	$telephone_short = isset($_POST['telephone_short']) ? $_POST['telephone_short'] : ''; 
    $telephone2 = isset($_POST['telephone2']) ? $_POST['telephone2'] : ''; 
	$telephone2_short = isset($_POST['telephone2_short']) ? $_POST['telephone2_short'] : ''; 
    $mobile = isset($_POST['mobile']) ? $_POST['mobile'] : ''; 
	$mobile_short = isset($_POST['mobile_short']) ? $_POST['mobile_short'] : ''; 
    $mobile2 = isset($_POST['mobile2']) ? $_POST['mobile2'] : ''; 
	$mobile2_short = isset($_POST['mobile2_short']) ? $_POST['mobile2_short'] : ''; 
    $fax = isset($_POST['fax']) ? $_POST['fax'] : ''; 
    $qq = isset($_POST['qq']) ? $_POST['qq'] : ''; 
    $weixin = isset($_POST['weixin']) ? $_POST['weixin'] : ''; 
	$weibo = isset($_POST['weibo']) ? $_POST['weibo'] : ''; 
    $email = isset($_POST['email']) ? $_POST['email'] : ''; 
	$roomname = isset($_POST['roomname']) ? $_POST['roomname'] : '';
	$roomnumber = isset($_POST['roomnumber']) ? $_POST['roomnumber'] : '';
	$idnumber = isset($_POST['idnumber']) ? $_POST['idnumber'] : '';
	$carnumber = isset($_POST['carnumber']) ? $_POST['carnumber'] : '';
    $homepage = isset($_POST['homepage']) ? $_POST['homepage'] : ''; 
    //$departid = isset($_POST['departid']) ? $_POST['departid'] : '0'; 
    //$departname = isset($_POST['departname']) ? $_POST['departname'] : ''; 
    $jobtitle = isset($_POST['jobtitle']) ? $_POST['jobtitle'] : ''; 
    $jobnumber = isset($_POST['jobnumber']) ? $_POST['jobnumber'] : ''; 
    $introduction = isset($_POST['introduction']) ? $_POST['introduction'] : ''; 
    $password = isset($_POST['pwd']) ? $_POST['pwd'] : 'E10ADC3949BA59ABBE56E057F20F883E'; //123456(md5)

	$methodname = 'Admin.AddUser';
	$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
		'<ip>' . getip() . '</ip>' . 
		'<userid>' . $userid . '</userid>' . 
		'<username>' . $username . '</username>' .
		'<usertype>' . $usertype . '</usertype>' .  
		'<canfound>' . $canfound . '</canfound>' .
		'<nickname>' . $nickname . '</nickname>' .
		'<actualname>' . $actualname . '</actualname>' .
		'<sex>' . $sex . '</sex>' .
		'<age>' . $age . '</age>' .
		'<birth_year>' . $birth_year . '</birth_year>' .
		'<birth_month>' . $birth_month . '</birth_month>' .
		'<birth_day>' . $birth_day . '</birth_day>' .
		'<province>' . $province . '</province>' .
		'<city>' . $city . '</city>' .
		'<address>' . $address . '</address>' .
		'<telephone>' . $telephone . '</telephone>' .
		'<telephone_short>' . $telephone_short . '</telephone_short>' .
		'<telephone2>' . $telephone2 . '</telephone2>' .
		'<telephone2_short>' . $telephone2_short . '</telephone2_short>' .
		'<mobile>' . $mobile . '</mobile>' .
		'<mobile_short>' . $mobile_short . '</mobile_short>' .
		'<mobile2>' . $mobile2 . '</mobile2>' .
		'<mobile2_short>' . $mobile2_short . '</mobile2_short>' .
		'<fax>' . $fax . '</fax>' .
		'<qq>' . $qq . '</qq>' .
		'<weixin>' . $weixin . '</weixin>' .
		'<weibo>' . $weibo . '</weibo>' .
		'<email>' . $email . '</email>' .
		'<roomname>' . $roomname . '</roomname>' .
		'<roomnumber>' . $roomnumber . '</roomnumber>' .
		'<idnumber>' . $idnumber . '</idnumber>' .
		'<carnumber>' . $carnumber . '</carnumber>' .
		'<homepage>' . $homepage . '</homepage>' .
		'<departid>' . $departid . '</departid>' .
		'<departname>' . $departname . '</departname>' .
		'<jobtitle>' . $jobtitle . '</jobtitle>' .
		'<jobnumber>' . $jobnumber . '</jobnumber>' .
		'<introduction>' . $introduction . '</introduction>' .
		'<password>' . $password . '</password>' .
		'</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: 
echo<<<EOT
	<div class="msgbox">
		<div class="msgbox_text">
			<img width="40px" height="40px" align="absmiddle" src="../images/ok.gif" />&nbsp;添加用户成功！
		</div>
		<div class="msgbox_btns">
			<input class="cssbtn" type="button" value="继续添加" onclick="window.location='departments_adduser.php?{$subpageurlparams}'" />
			<input class="cssbtn" type="button" value="返回" onclick="window.location='departments_users.php?{$subpageurlparams}'" />
		</div>
	</div>
EOT;
			break;
		case ERRORCODE_EXISTS: 
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='departments_adduser.php?{$subpageurlparams}'" />
			<input class="cssbtn" type="button" value="返回" onclick="window.location='departments_users.php?{$subpageurlparams}'" />
		</div>
	</div>
EOT;
			break;
		case ERRORCODE_NOACCOUNT: 
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='departments_adduser.php?{$subpageurlparams}'" />
			<input class="cssbtn" type="button" value="返回" onclick="window.location='departments_users.php?{$subpageurlparams}'" />
		</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='departments_adduser.php?{$subpageurlparams}'" />
			<input class="cssbtn" type="button" value="返回" onclick="window.location='departments_users.php?{$subpageurlparams}'" />
		</div>
	</div>
EOT;
	}//switch
}//step2

if ($step == 1) {
	step1();
}
else if ($step == 2) {
	step2();
}
?>

</body>
</html>
