<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", -10);
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@include file="/WEB-INF/views/base.jsp"%>
<!DOCTYPE html>
<html>

<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>角色授权</title>

<link href="${cssPath }/plugins/jsTree/style.min.css" rel="stylesheet" />

<link href="${cssPath }/bootstrap.min.css" rel="stylesheet">

<!-- Data Tables -->
<link href="${cssPath }/plugins/dataTables/dataTables.bootstrap.css"
	rel="stylesheet">
<link href="${cssPath }/plugins/dataTables/dataTables.responsive.css"
	rel="stylesheet">
<link
	href="${cssPath }/plugins/dataTables/dataTables.tableTools.min.css"
	rel="stylesheet">
	
<link href="${fontAwsPath }/css/font-awesome.css" rel="stylesheet">

<link href="${cssPath }/animate.css" rel="stylesheet">
<link href="${cssPath }/style.css" rel="stylesheet">
<link
	href="${cssPath }/plugins/sweetalert/sweetalert.css"
	rel="stylesheet">

</head>

<body>
	<div id="wrapper">

		<jsp:include page="../menu.jsp"></jsp:include>

		<div id="page-wrapper" class="gray-bg">
			<jsp:include page="../head.jsp"></jsp:include>
			<jsp:include page="../pageNav.jsp"></jsp:include>
			<div class="wrapper wrapper-content animated fadeInRight">
				<div class="row">


					<input type="hidden" id="roleId" value="${role.roleId }">
					<div class="alert alert-success">当前授权角色：${role.roleName }</div>
					<div class="tabs-container">
						<ul class="nav nav-tabs">
							<li class="active"><a data-toggle="tab" href="#tab-1"
								aria-expanded="true">菜单权限</a></li>
							<li class=""><a data-toggle="tab" href="#tab-2"
								aria-expanded="false">按钮权限</a></li>
						</ul>
						<div class="tab-content">
							<div id="tab-1" class="tab-pane active">
								<div class="panel-body">
									<div class="row">

										<div id="jstree1">
										</div>

									</div>
									<div class="hr-line-dashed"></div>
									<div class="form-group">
										<div class="col-lg-12">
											<div class="tabs-container" style="text-align: right;">
												<button type="button" class="btn btn-warning" id="btnback"
													style="width: 120px">
													<i class="fa fa-mail-forward"></i>&nbsp;&nbsp;返回
												</button>
												<button type="submit" class="btn btn-primary" id="btnSave"
													style="width: 120px">
													<i class="fa fa-save"></i>&nbsp;&nbsp;授权
												</button>
											</div>
										</div>
									</div>

								</div>
							</div>
							<div id="tab-2" class="tab-pane">
								<div class="panel-body">
									<div class="row">

										<div id="jstree2">
										
										</div>

									</div>
									<div class="hr-line-dashed"></div>
									<div class="form-group">
										<div class="col-lg-12">
											<div class="tabs-container" style="text-align: right;">
												<button type="button" class="btn btn-warning" id="btnback1"
													style="width: 120px">
													<i class="fa fa-mail-forward"></i>&nbsp;&nbsp;返回
												</button>
												<button type="submit" class="btn btn-primary" id="btnSave1"
													style="width: 120px">
													<i class="fa fa-save"></i>&nbsp;&nbsp;授权
												</button>
											</div>
										</div>
									</div>

								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- Mainly scripts -->
	 <%@include file="/WEB-INF/views/common_base_script.jsp"%>
	<script>
		$(document).ready(function() {
			loadMenuTree();
		});
		
		function loadMenuTree(){
			var basePath = "<%=basePath %>";
			var roleId = $("#roleId").val();
			$.ajax({
				type : 'POST',
				async : false,
				url : basePath + 'sysRole/loadMenuTree.json',
				data : {'roleId' : roleId },
				dataType : 'json',
				success : function(data){
					var menuList = data.pageMenu;
					var pageMenuJson = eval('(' + data.pageMenu + ')');
					var result =  eval('(' +data.result+ ')');
					var result1 =  eval('(' +data.result1+ ')');
					//加载 第一个树
					$('#jstree1').jstree({
						'core' : { 'data' : [ pageMenuJson ], 'check_callback' : true },
						'plugins' : [ 'themes', 'types', 'json_data', 'checkbox', 'ui', 'state' ],}).
						bind("loaded.jstree", function(e, data) {
							setTimeout(function(){
								$('#jstree1').jstree("uncheck_all"); 
								for(var j = 0; j < result.length; j++ ){
									if($("#jstree1").jstree("is_parent", result[j].menuId)){
									
									}else{
										$("#jstree1").jstree("check_node", result[j].menuId);
									}
								}
							}, 100)
					});
					
					//加载 第二个树
					var bottonMenuJson = eval('(' + data.buttonMenu + ')');
					$('#jstree2').jstree({ 
						'core' : { 'data' : [ bottonMenuJson ], 'check_callback' : true },
						'plugins' : [ 'themes', 'types', 'json_data', 'checkbox', 'ui', 'state' ],}).
						bind("loaded.jstree", function(e, data) {
							setTimeout(function(){
								$('#jstree2').jstree("uncheck_all"); 
								for(var j = 0; j < result1.length; j++ ){
									$("#jstree2").jstree("check_node", result1[j].menuId);
								}
							},100)
					});
					
				}
			});
		}

	</script>

	<script type="text/javascript">
		$("#btnback").on("click", function() {
			history.go(-1);
		});

		//返回
		$("#btnback1").click(function() {
			history.go(-1);
		});

		//授权菜单,子菜单 选中 一个， 默认 设置 父菜单选中
		$("#btnSave").click(function() {
			//获取所有选中菜单MenuID
			var menudata = $("#jstree1").jstree("get_checked");
			var menusplist = [];
			for (var i = 0; i < menudata.length; i++) {
				var checkedId = menudata[i];
				if(checkedId != 'root' ) {			
					var parentId = $("#jstree1").jstree("get_parent", menudata[i]);
					if(parentId != 'root' && !contain(menusplist, parentId)) {
						menusplist.push(parentId);
					}
					if(parentId != 'root') {
						var supParentId = $("#jstree1").jstree("get_parent", parentId);
						if(supParentId && supParentId != 'root' && !contain(menusplist, supParentId) ) {
							menusplist.push(supParentId);
						}
					}
				}
				if(checkedId != 'root' &&  !contain(menusplist, checkedId) ){
					menusplist.push(checkedId);
				}
			}
			var menusplists = "";
			for(var i=0; i<menusplist.length; i++){
				menusplists += menusplist[i] + ",";
			}
			menusplists = menusplists.substring(0, menusplists.length - 1);
			var basePath = "<%=basePath %>";
			$.ajax({
				type : 'POST',
				async : false,
				url : basePath + 'sysRole/addRoleRelevancePer.json',
				data : {"menuIDS" : menusplists, "roleId" : $("#roleId").val() },
				dataType : 'json',
				success : function(data){
					if( data.success) {
						CommonUtils.alertMsg(1,"授权菜单成功！");
						var checked = data.checked;
						if( checked ) {
							for(var i=0; i<checked.length; i++){
								$("#jstree2").jstree("check_node", checked[i]);
							}
						}
						var unchecked = data.unchecked;
						if(unchecked) {
							for(var i=0; i<unchecked.length; i++){
								$("#jstree2").jstree("uncheck_node", unchecked[i]);
							}
						}
					} else {
						CommonUtils.alertMsg(2,"授权菜单失败！");
					}
				}
			});
		});

		//授权菜单按钮
		$("#btnSave1").click(function() {
			//获取所有选中菜单MenuID
			var menudata = $("#jstree2").jstree("get_checked");
			var menusplist = "";
			for (var i = 0; i < menudata.length; i++) {
				if(menudata[i] != 'root'){
					if($("#jstree2").jstree("is_parent", menudata[i])) {
					} else {
						menusplist += menudata[i] + ",";
					}
				}
			}
			var basePath = "<%=basePath %>";
			menusplist = menusplist.substring(0, menusplist.length-1);
			$.ajax({
				type : 'POST',
				async : false,
				url : basePath + 'sysRole/addButtonPerByRole.json',
				data : {"menuIDS" : menusplist, "roleId" : $("#roleId").val() },
				dataType : 'json',
				success : function(data){
					if( data.success) {
						CommonUtils.alertMsg(1,"授权菜单成功！");
						var checked = data.checked;
						if( checked ) {
							for(var i=0; i<checked.length; i++){
								$("#jstree1").jstree("check_node", checked[i]);
							}
						}
						
					} else {
						CommonUtils.alertMsg(2,"授权菜单失败！");
					}
				}
			});

		});
		
		function contain(array, obj){
			var flag = false;
			if(array != null && array.length >0){
				for(var i=0; i<array.length; i++){
					var v = array[i];
					if(v == obj){
						flag = true;
						break;
					}
				}
			}
			return flag;
		}
		
	</script>
</body>

</html>
