<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" style="padding: 11px;"></ul>
	</div>
	<div class="right box-content">
							<div class="form-actions">
								<button type="button" data-loading-text="提交中..." onclick="save();"
									class="btn btn-primary">保存</button>
								<button type="button" class="btn"
									onclick="closeWin();">取消</button>
							</div>
	</div>
</div>
</body>
 <script type="text/javascript">
 var setting = {
			check: {
				enable: true,
			},
			
			data:{
				key:{
					name:"name"
				},
				simpleData:{
					enable:true,
					idKey:"id",
					pIdKey:"pId",
					open:true,
					checked:true,
					rootPId:1
				}
			}
			
		};


	 function checkNode() {
		 
		 var url ='[[${sysUrl}]]/sys/role/getAdminOrg';
		 var params = {'id' : '[[${id}]]'} ;
	     $.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].orgId == nodes[i].id){
			                		treeObj.checkNode(nodes[i], true, false);
			                		treeObj.expandNode(nodes[i], true, false);
			                		break;
				                }
						    }
			         }
			     }
	    	});
	    	
	 }
		

		
	  $(function(){
			var url ='[[${sysUrl}]]/sys/org/query';
			$.post(url,null,function(data){
				$.fn.zTree.init($("#treeMenu"), setting, data);
				checkNode();
			});
		  
	  })
		
		function save(){
			 var treeObj = $.fn.zTree.getZTreeObj("treeMenu");
			var nodes = treeObj.getCheckedNodes(true);
			 var datatmp=[];
	        for(var x=0; x<nodes.length; x++){
	        	var json = {};
	        	json.orgCode = nodes[x].code;
				json.orgId =nodes[x].id;
				datatmp.push(json);
	        };
	        if(datatmp.length==0){
	        	layer.msg("请选择节点",{icon:11});
	        	return false;
	        }
			var url="[[${sysUrl}]]/sys/role/saveAdminOrgCode";
			 var params ={
					 'datatmp':JSON.stringify(datatmp),
					 'adminId':'[[${id}]]'
			        };
			$.post(url,params,function(data){
				if(data.meta.success){
					layer.msg("操作成功",{icon:1});
			    	setTimeout(function(){
			    		closeWin();
					},1000);
				}else{
					layer.msg(data.meta.message,{icon:11});
					closeWin();
				}
			});
			
		}
	  function closeWin(){
		    parent.location.reload();
			parent.layer.close(parent.layer.getFrameIndex(window.name));
		}
 </script>
</HTML>