<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>私塾家</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="icon" href="/favicon.ico" th:href="@{/favicon.ico}" type="image/x-icon">
<link rel="shortcut icon" href="/favicon.ico" th:href="@{/favicon.ico}" type="image/x-icon" />
<link href="sys/css/main.css" th:href="@{/static/sys/css/zTree.css}" rel="stylesheet"/>
<link href="sys/css/zTree/demo.css" th:href="@{/static/sys/css/zTree/demo.css}" rel="stylesheet"/>
<link href="sys/css/zTree/zTreeStyle.css" th:href="@{/static/sys/css/zTree/zTreeStyle.css}" rel="stylesheet"/>
<script th:include="sys/common/metaJS" th:remove="tag"></script> 
<script src="sys/js/zTree/jquery.ztree.core-3.5.js" th:src="@{/static/sys/js/zTree/jquery.ztree.core-3.5.js}"></script>
<script src="sys/js/zTree/jquery.ztree.excheck-3.5.js" th:src="@{/static/sys/js/zTree/jquery.ztree.excheck-3.5.js}"></script>
</head>

<body>
<h1>分配权限</h1>
<h6></h6>
<div class="content_wrap">
	<div class="zTreeDemoBackground left">
		<ul id="treeMenu" class="ztree"></ul>
	</div>
	<div class="box-content">
							<div class="form-actions">
							
							<input  type="hidden" th:value="${id}" id="id" />
								<button type="button" data-loading-text="提交中..." onclick="save();"
									class="btn btn-primary">保存</button>
								<button type="button" class="btn"
									onclick="window.history.go(-1);">取消</button>
							</div>
					</div>
</div>
</body>
 <script type="text/javascript">
 var setting = {
			check: {
				enable: true
			},
			
			data:{
				key:{
					name:"menuName"
				},
				simpleData:{
					enable:true,
					idKey:"id",
					pIdKey:"parentId",
					open:true,
					checked:true,
					rootPId:0
				}
			}
			
		};

		 function checkNode() {
			 
			 var url ='[[${sysUrl}]]/sys/role/getRoleMenu';
			 var params = {'id' : $('#id').val()} ;
		     $.post(url,params,function(data){
		    		if(data.length>0){
	    				var treeObj = $.fn.zTree.getZTreeObj("treeMenu");
	    		        var nodes = treeObj.transformToArray(treeObj.getNodes()); 
		    			 for(var x=0; x<data.length; x++){
				                for (var i=0, l=nodes.length; i<l; i++) {
				                	if(data[x] == nodes[i].id){
				                		treeObj.checkNode(nodes[i], true, false);
				                		treeObj.expandNode(nodes[i], true, false);
				                		break;
					                }
							    }
				         }
	    		     }
		    	});
		    	
		 }
		

		
	  $(function(){
			var url ='[[${sysUrl}]]/sys/menu/sysAllMenuQuery';
			$.post(url,function(data){
				$.fn.zTree.init($("#treeMenu"), setting, data);
				checkNode();
			});
		  
	  })
		
		function save(){
			 var treeObj = $.fn.zTree.getZTreeObj("treeMenu");
			var nodes = treeObj.getCheckedNodes(true);
			var selectIds=[];
	        for(var x=0; x<nodes.length; x++){
	            var dataId = nodes[x].id;
	            selectIds.push(dataId);
	        };
	        if(selectIds.length==0){
	   		 selectIds.push('');
	   	   }
			var url="[[${sysUrl}]]/sys/role/addRoleCompetence";
			 var params ={
			            id : $("#id").val(),
			            menuIds : selectIds
			        };
			$.post(url,params,function(data){
				if(data.meta.success){
					layer.msg("操作成功",{icon:1});
			    	setTimeout(function(){
			    		window.location.href ='[[${sysUrl}]]/sys/role/rolelist.html';
					},1000);
				}else{
					layer.msg(data.meta.message,{icon:11});
				}
			});
			
		}
 
 </script>
</HTML>