<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta name="description" content="Xenon Boostrap Admin Panel" />
	<meta name="author" content="" />
	<link rel="stylesheet" href="${ROOT_PATH}/public/plugin/zTree/v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
	<title>权限分配</title>
</head>
<body class="page-body">
	<article style="height: 200px;" class="page-container">
		<table class="table">
			<tr>
				<td class="va-t"><ul id="ztree" class="ztree"></ul></td>  
			</tr>
			<tr>
				<td class="va-t">
				<div class="row cl">
					<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
						<button type="button" id="btn_setting"  class="btn btn-info" onclick="save();">确定</button>
						<button type="button" class="btn btn-white" onclick="disMissModel()">取消</button>   
					 </div>
				</div>
				</td> 
			</tr>
		</table> 
	</article>
	
	<script type="text/javascript" src="${ROOT_PATH}/public/plugin/zTree/v3/js/jquery.ztree.all-3.5.min.js"></script> 
	<script type="text/javascript">
	var setting = {
		   view: {
			dblClickExpand: false,//双击节点时，是否自动展开父节点的标识
			showLine: true,//是否显示节点之间的连线
			selectedMulti: false,//设置是否允许同时选中多个节点
		},
		check:{
			/* chkboxType: { "Y":"ps", "N": "ps" }, */
			chkStyle: "checkbox",//复选框类型
			enable: true //每个节点上是否显示 CheckBox 
		  },
		data: {
			simpleData: {//简单数据模式
				enable:true,
				idKey: "id",
				pIdKey: "pId",
				rootPId: ""
			}
		}, 
		callback: {
			beforeClick: function(treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("tree");
				if (treeNode.isParent) {
					zTree.expandNode(treeNode);//如果是父节点，则展开该节点
				} else {
					zTree.checkNode(treeNode, !treeNode.checked, true, true);//单击勾选，再次单击取消勾选
				}
			}
		}
	};
	
	var zNodes =JSON.parse('${zTreeNodes}');
	var size = '${size}';
	var code;

	function showCode(str) {
		if (!code) code = $("#code");
		code.empty();
		code.append("<li>"+str+"</li>");
	}
			
	$(document).ready(function(){
		var t = $("#ztree");
		if(size <= 0){
			$("#modal-setting-body").html("请先设置菜单权限");
			$("#modal-footer").removeAttr("hidden");
		}else{
			t = $.fn.zTree.init(t, setting, zNodes);
			$("#modal-footer").attr("hidden","hidden");
		}
	});
	
	function save(){
		var zTree = $.fn.zTree.getZTreeObj("ztree")
		var nodes = zTree.getCheckedNodes();
		var tmpNode;
		var ids = "";
		for(var i=0; i<nodes.length; i++){
			tmpNode = nodes[i];
			if(i!=nodes.length-1){
				ids += tmpNode.id+",";
			}else{
				ids += tmpNode.id;
			}
		}
		
		var roleId = "${roleId}";
		var msg = "${msg}";
		var url = "${ROOT_PATH}/sysRole/saveButton.do";
		var postData = {"ROLE_ID":roleId,"menuIds":ids,"msg":msg};
		$("#btn_setting").attr("disabled","disabled");
		$.post(url,postData,function(result){
			window.setTimeout("enableBtn('#btn_setting');",Constants.reloadTime);
			if(result.status == 1){ 
				$("#modal-setting").modal('hide');
				toastr.success("操作成功", null, opts);
			}else{
				toastr.error(result.msg, null, opts);
			}
			location.reload();
		});
	}
	
	function disMissModel(){
    	location.reload();
	}
</script>	
</body>
</html>