<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/include/taglib.jsp" %>
<!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>
	<div>
		<div class="b_box">
			<form id="sysAuthorityForm" action="post">
				<input type="hidden" name="CSRFToken" value="${csrfToken}" />
				<input type="hidden" name="authorityId" value="${sysAuthorityShowVo.authorityId}" />
				<table class="c_table m_t10">
					<tr>
						<td class="t_tit">父权限</td>
						<td><input type="hidden" name="parentId" id="parentId" value="${sysAuthorityShowVo.parentId}"/><input type="text" id="parentName" name="parentName" data-validaterole="parentName" data-rule="parentName" data-param='{"code" : "mmm","ml":32}' data-message='最大不能超过<!=ml!>' value="${sysAuthorityShowVo.parentAuthorityName}" /></td>
					</tr>
					<tr>
						<td class="t_tit">权限名称</td>
						<td><input type="text" id="authorityName" name="authorityName" data-validaterole="authorityName" data-rule="authorityName" data-param='{"code" : "mmm","ml":32}' data-message='最大不能超过<!=ml!>' value="${sysAuthorityShowVo.authorityName}" /></td>
					</tr>
					<tr>
						<td class="t_tit">权限值</td>
						<td><input type="text" id="authorityValue" name="authorityValue" data-validaterole="authorityValue" data-rule="authorityValue" data-param='{"code" : "mmm","ml":32}' data-message='最大不能超过<!=ml!>' value="${sysAuthorityShowVo.authorityValue}" /></td>
					</tr>
					<tr>
						<td class="t_tit">描述</td>
						<td><input type="text" id="remark" name="remark" data-validaterole="remark" data-rule="remark" data-param='{"code" : "mmm","ml":255}' data-message='最大不能超过<!=ml!>' value="${sysAuthorityShowVo.remark}" /></td>
					</tr>
					<tr>
						<td class="t_tit">权限类型</td>
						<td><select id="authorityType" name="authorityType"  data-validaterole="authorityType" data-rule="authorityType" data-param='{"code" : "mmm","ml":1}' data-message='最大不能超过<!=ml!>' >
							<option value="1" <c:if test="${sysAuthorityShowVo.authorityType ne 3}">selected="selected"</c:if> >普通权限</option>
							<option value="3" <c:if test="${sysAuthorityShowVo.authorityType eq 3}">selected="selected"</c:if> >数据权限</option>
						</select></td>
					</tr>
				</table>			
			</form>
		</div>
		<div class="action_box m_t10 t_c">
			<input type="button" class="b_btn" id="editBtn" 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();
		$("#parentName").bind("click",function(){
			showMenu();
		});
	});
	function showMenu() {
		var cityObj = $("#parentName");
		var cityOffset = $("#parentName").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}/sysAuthority/authorityMenuTree.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 = $("#parentName");
				parentNameObj.attr("value", nodes[0].name);
				var parentDeptIdObj = $("#parentId");
				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: "#editBtn" // 提交按钮
		});

		// 设置拼装规则: 这里加上各个验证规则
		d.setValidateRule({ authorityId: ["isBlank", "checkLen"] });
		d.setValidateRule({ parentId: ["checkLen"] });
		d.setValidateRule({ parentName: ["checkLen"] });
		d.setValidateRule({ authorityName: ["isBlank", "checkLen"] });
		d.setValidateRule({ authorityValue: ["isBlank", "checkLen"] });
		d.setValidateRule({ remark: ["checkLen"] });

	
		// 注册保存按钮事件
		d.bind("onsubmit", function() {
			if (confirm('确定提交审批？')) {
				// 序列化表单参数
				var serializedForm = $("#sysAuthorityForm").serialize();
				$.post("${ctx}/sysAuthority/edit.do", serializedForm, function(result) {
					if ("succ" == result) {
						alert("操作成功");
						// 刷新列表页面
						parent.location.replace("${ctx}/sysAuthority/list.do?ts=" + new Date().getTime()); //如果发表成功则跳到列表界面 
						// 关闭弹窗窗口
						parent.sysAuthorityDialog.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>