<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>添加</title>
<%@ include file="/include/header.jsp"%>
<%@ include file="/include/zTree.jsp" %>
<script src="${ctx}/js/component/tree.js" type="text/javascript"></script>
<link type="text/css" rel="stylesheet" href="${ctx}/css/baseFrame.css" />
</head>
<body class="bg1">
	<div class="dialog_main">
		<div class="b_box">
			<form id="sysUserForm" action="post">
				<input type="hidden" name="CSRFToken" value="${csrfToken}" />
				<table class="c_table m_t10">
					<%-- <tr>
						<td class="t_tit">[系统用户ID]</td>
						<td><input type="text" id="sysuserId" name="sysuserId" data-validaterole="sysuserId" data-rule="sysuserId" data-param='{"code" : "mmm","ml":32}' data-message='最大不能超过<!=ml!>' value="" /></td>
					</tr>
					<tr>
						<td class="t_tit">[系统用户编号]</td>
						<td><input type="text" id="sysuserNo" name="sysuserNo" data-validaterole="sysuserNo" data-rule="sysuserNo" data-param='{"code" : "mmm","ml":16}' data-message='最大不能超过<!=ml!>' value="" /></td>
					</tr> --%>
					<tr>
						<td class="t_tit">用户名</td>
						<td><input type="text" id="sysuserAccount" name="username" data-validaterole="sysuserAccount" data-rule="sysuserAccount" data-param='{"code" : "mmm","ml":20}' data-message='最大不能超过<!=ml!>' value="" /></td>
					</tr>
					<tr>
						<td class="t_tit">密码</td>
						<td><input type="text" id="sysuserPassword" name="password" data-validaterole="sysuserPassword" data-rule="sysuserPassword" data-param='{"code" : "mmm","ml":64}' data-message='最大不能超过<!=ml!>' value="123456" readonly="true"/></td>
					</tr>
					<tr>
						<td class="t_tit">姓名</td>
						<td><input type="text" id="sysuserName" name="userCnName" data-validaterole="sysuserName" data-rule="sysuserName" data-param='{"code" : "mmm","ml":20}' data-message='最大不能超过<!=ml!>' value="" /></td>
					</tr>
					<tr>
						<td class="t_tit">单位</td>
						<td><input type="hidden" name="deptId" id="deptId" value="deptId"/><input type="text" id="dept" name="dept" data-validaterole="sysuserDept" data-rule="sysuserDept" data-param='{"code" : "mmm","ml":20}' data-message='最大不能超过<!=ml!>' value="" /></td>
					</tr>
					<tr>
						<td class="t_tit">手机号</td>
						<td><input type="text" id="sysuserPhone" name="mobile" data-validaterole="sysuserPhone" data-rule="sysuserPhone" data-param='{"code" : "mmm","ml":20}' data-message='最大不能超过<!=ml!>' value="" /></td>
					</tr>
					<tr>
						<td class="t_tit">邮箱</td>
						<td><input type="text" id="sysuserMail" name="email" data-validaterole="sysuserMail" data-rule="sysuserMail" data-param='{"code" : "mmm","ml":50}' data-message='最大不能超过<!=ml!>' value="" /></td>
					</tr>
				</table>
				<input name="CSRFToken" type="hidden" value="${csrfToken} "/>	
			</form>
		</div>
		<div class="action_box m_t10 t_c">
			<input type="button" class="b_btn" id="addBtn" value="保存" />
		</div>
	</div>
	<div id="menuContent" class="menuContent" style="display:none; position: absolute;background-color: white;">
		<ul id="treeDemo" class="ztree" style="margin-top:0; width:300px;"></ul>
	</div>
	<div id="menuContentForRole" class="menuContent" style="display:none; position: absolute;background-color: white;">
		<ul id="treeDemoForRole" class="ztree" style="margin-top:0; width:300px;"></ul>
	</div>
	<script type="text/javascript">
	$(function(){
		initTree();
		$("#dept").bind("click",function(){
			showMenu();
		});
	});
	function showMenu() {
		var cityObj = $("#dept");
		var cityOffset = $("#dept").offset();
		console.info(cityOffset);
		$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
		$("body").bind("mousedown", onBodyDown);
	}
	function hideMenu() {
		$("#menuContent").fadeOut("fast");
		$("body").unbind("mousedown", onBodyDown);
	}
	function onBodyDown(event) {
		if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
			hideMenu();
		}
	}
	function initTree() {
		var setting = {
				view: {
					dblClickExpand: false
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					beforeClick: beforeClick,
					onClick: onClick,
					asyncSuccess: zTreeOnAsyncSuccess,//异步加载成功的fun 
					asyncError: zTreeOnAsyncError, //加载错误的fun
				},async: { 
					enable: true, 
					url:"${ctx}/sysDept/deptTree.do", 
					autoParam:["id"], 
					dataType: "json",//默认text 
					//type:"get",//默认post 
					dataFilter: filter //异步返回后经过Filter 
				}
			};
		
		function onClick(e, treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
			nodes = zTree.getSelectedNodes();
			if(nodes.length > 0) {
				var parentNameObj = $("#dept");
				parentNameObj.attr("value", nodes[0].name);
				var parentDeptIdObj = $("#deptId");
				parentDeptIdObj.attr("value", nodes[0].id);
			}
			hideMenu();
		}
		
		//treeId是treeDemo 
		function filter(treeId, parentNode, childNodes) { 
			if (!childNodes) return null; 
				for (var i=0, l=childNodes.length; i<l; i++) { 
					childNodes[i].name = childNodes[i].name.replace('',''); 
			} 
				return childNodes; 
		}
		
		function beforeClick(treeId, treeNode) {
			//var check = (treeNode && !treeNode.isParent);
			//if (!check) alert("只能选择城市...");
			return true;
		}
		function zTreeOnAsyncError(event, treeId, treeNode){ 
			alert("异步加载失败!"); 
		} 
		function zTreeOnAsyncSuccess(event, treeId, treeNode, msg){ 

		} 
		$.fn.zTree.init($("#treeDemo"),setting);
	}
	
	
	// 验证
	seajs.use(["component/validate", "component/tip"], function(validate, tip) {
		var ntip = function() {
			// 定义一个提示对象
			return new tip({});
		};

		// 定义一个验证对象
		var d = new validate({
			con: "#adminRankForm",
			// 验证容器
			ntip: ntip,
			onSubmit: "#addBtn" // 提交按钮
		});

		// 设置拼装规则: 这里加上各个验证规则
		/* d.setValidateRule({ sysuserId: ["isBlank", "checkLen"] }); */
		d.setValidateRule({ sysuserNo: ["isBlank", "checkLen"] });
		d.setValidateRule({ sysuserAccount: ["isBlank", "checkLen"] });
		d.setValidateRule({ sysuserPassword: ["isBlank", "checkLen"] });
		d.setValidateRule({ sysuserName: ["isBlank", "checkLen"] });
		d.setValidateRule({ sysuserPhone: ["isBlank", "checkLen"] });
		d.setValidateRule({ sysuserMail: ["isBlank", "checkLen"] });
		d.setValidateRule({ sysuserStatus: ["isBlank", "checkLen"] });
		d.setValidateRule({ sysuserDept: ["isBlank", "checkLen"] });
		d.setValidateRule({ STATUS: ["isBlank", "checkLen"] });
		/* d.setValidateRule({ CREATTIME: ["isBlank", "checkLen"] });
		d.setValidateRule({ UPDATETIME: ["isBlank", "checkLen"] }); */

	
		// 注册保存按钮事件
		d.bind("onsubmit", function() {
			if (confirm('确定提交？')) {
				// 序列化表单参数
				var serializedForm = $("#sysUserForm").serialize();
				$.post("${ctx}/sysUser/add.do", serializedForm, function(result) {
					if ("succ" == result) {
						alert("操作成功");
						// 刷新列表页面
						parent.location.replace("${ctx}/sysUser/list.do?ts=" + new Date().getTime()); //如果发表成功则跳到列表界面 
						// 关闭弹窗窗口
						parent.sysUserDialog.close();
					} else if ("notThroughSecurityCheck" == result["isThroughSecurityCheck"]) { // 如果不通过安全检查
						var mess = result["mess"];
						$.post("${ctx}/common/notThroughSecurityCheck.do?ts=" + new Date().getTime(), mess, function(res) {
							alert(res);
						}, "json");

					} else {
						alert(result);
					}
				},
				"json");
			}
	
		});
	});
	</script>
</body>
</html>