<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>角色权限关联</title>	
<%@ include file="/include/header.jsp"%>
<link type="text/css" rel="stylesheet" href="${ctx}/css/baseFrame.css" />
<!-- 下拉树 -->
<%@ include file="/include/zTree.jsp" %>
<script type="text/javascript">
function initTree(){
	var setting = { 
		data: { 
			simpleData: { 
				enable: true 
				// idKey:"id", 
				// pIdKey:"pId", 
			} 
		},
	    check:{
    		enable: true,
    		chkboxType:{ "Y" : "ps", "N" : "ps" }
		},
		view:{showLine:false},
		async: { 
			enable: true, 
			url:"${ctx}/sysAuthority/authorityTree.do", 
			autoParam:["id"], 
			otherParam: {//这个是额外的附加的参数
		       diyParams:$("#roleId").val()
		    },
			dataType: "json",//默认text 
			//type:"get",//默认post 
			dataFilter: filter //异步返回后经过Filter 
			},
		callback:{ 
			asyncSuccess: zTreeOnAsyncSuccess,//异步加载成功的fun 
			asyncError: zTreeOnAsyncError, //加载错误的fun 
			beforeClick:beforeClick //捕获单击节点之前的事件回调函数 
		} }; 
	
		//treeId是authorityTree 
		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){ 
			if(!treeNode.isParent){ 
				//alert("请选择父节点"); 
				return false; 
			}else{ 
				return true; 
			} 
		} 
		function zTreeOnAsyncError(event, treeId, treeNode){ 
			alert("异步加载失败!"); 
		} 
		function zTreeOnAsyncSuccess(event, treeId, treeNode, msg){ 

		} 
		$.fn.zTree.init($("#authorityTree"),setting);
}

$(function(){
	initTree();
	$("#btnSave").click(function(){
		var treeObj = $.fn.zTree.getZTreeObj("authorityTree");
  		 var nodes = treeObj.getCheckedNodes(true);
		 var ids="";
  		 for (var i = 0; i < nodes.length; i++) {
      		ids += nodes[i].id+",";
  		 }
  		$.post("${ctx}/sysRole/setAuthority.do", {
  			authorityIds:ids,
  			roleId:$("#roleId").val(),
  			csrfToken:"${csrfToken}"
  		}, function (result) {
            if ("succ" == result) {
            	alert("操作成功");
				// 刷新列表页面
				// 关闭弹窗窗口
    			parent.sysRoleDialog.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>
</head>
<body style="overflow-y:auto;">
	<div>
		<div class="b_box">
			<%-- <div class="p_bar">您当前的位置：角色管理/设置权限 选择角色：${sysRole.roleName}</div> --%>
			<div class="p_bar">设置角色权限：${sysRole.roleName}</div>
			<div >
				<ul id="authorityTree" class="ztree" style="background-color: white;"></ul>
			<!-- </div>
			<div class="action_box m_t10 t_c"> -->
				<center><input type="button" class="b_btn" value="保存" id="btnSave" /></center>
			</div>
			<input type="hidden" id="roleId" value="${sysRole.roleId}" />
		</div>
	</div>
</body>
</html>