<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/jsp/common/taglibs.jspf"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html>
	<head>
		<hejin:header title="角色管理"></hejin:header>
		<!-- treegride css -->
		<link rel="stylesheet" type="text/css" href="static/css/bootstrap/jquery.treegrid.css">
		<style type="text/css">
		.span {
			position: absolute;
			left: 50%;
			top: 50%;
			margin-top: 5px;
		}
		</style>
	</head>
	<body>
		<!-- 菜单 -->
		<jsp:include page="../../menu.jsp"/>
		<!-- 当前用户选中的菜单 -->
		<input type="hidden" id="userSelectMenuId" value="${_current_menu}"/>
		<input type="hidden" id="currentUserPermissionDataId" value="${_current_menu_perms }"/>
		<!-- 面包屑 -->
		<div class="container-fluid">
			<ol class="breadcrumb">
				<li><a href="javascript:void(0);">系统管理</a></li>
				<li><a href="system/role/roleIndex.do">角色管理</a></li>
			</ol>
		</div>
		
		<!-- 查询条件 -->
		<div class="container-fluid">
			<div class="row">
				<div class="col-sm-1">
  					<span class="span">角色名称</span>
				</div>
				<div class="col-sm-2">
					<div class="form-group">
 						<input type="text" class="form-control" id="roleName" name="roleName" placeholder="请输入角色名称">
 					</div>
				</div>
				<div class="col-sm-1">
					<span class="span">角色状态</span>
				</div>
				<div class="col-sm-2">
					<div class="form-group">
						<select id="status" name="status" class="form-control">
							<option value="" selected>--请选择--</option>
							<c:forEach items="${roleStatusMap}" var="map">
								<c:if test="${map.key != 10}">
									<option value="${map.key}">
											${map.value}
									</option>
								</c:if>
							</c:forEach>
						</select>
					</div>
				</div>
				<div class="col-sm-1">
					<div class="form-group">
	 					<a class="btn btn-block btn-primary btn-query" id="searchBtn" data-target="#compose-modal"><i class="fa fa-pencil"></i>查询</a>
	 				</div>
				</div>
				<div class="col-sm-1">
					<div class="form-group">
	 					<a class="btn btn-block btn-primary btn-cancel" id="resetBtn" data-target="#compose-modal"><i class="fa fa-pencil"></i>重置</a>
	 				</div>
				</div>
			</div>
		</div>
		<!-- END 条件 -->
		
		<!-- BEGIN container-fluid -->
		<div class="container-fluid">
			<div class="row"><!-- BEGIN row-fluid -->
				<div class="col-sm-12"><!-- BEGIN col-sm-12 -->
					<div class="box">
						<div class="box-header">
							<h3 class="box-title">角色列表</h3>
							<!-- <div class="col-sm-9" style="float: right;margin-top: 10px;margin-right: 10px;margin-bottom: 5px;">
								<div class="btn-toolbar" style="float: right;">
									<shiro:hasPermission name="${_current_menu }:create">
										<div class="btn-group">
											<button id="addRoleBtn" type="button" class="btn btn-primary">新增</button>
										</div>
									</shiro:hasPermission>
									<shiro:hasPermission name="${_current_menu }:delete">
										<div class="btn-group">
											<button id="deleteRoleBtn" type="button" class="btn btn-primary">删除</button>
										</div>
									</shiro:hasPermission>
									<shiro:hasPermission name="${_current_menu }:update">
										<div class="btn-group">
											<button id="lockRoleBtn" type="button" class="btn btn-primary">禁用</button>
										</div>
									</shiro:hasPermission>
									<shiro:hasPermission name="${_current_menu }:update">
										<div class="btn-group">
											<button id="unlockRoleBtn" type="button" class="btn btn-primary">启用</button>
										</div>
									</shiro:hasPermission>
								</div>
							</div> -->
						</div><!-- /.box-header -->
						<div class="box-body table-responsive table-hover">
							<table id="role_tab" class="table table-bordered table-striped">
								<thead>
									<tr>
										<!-- <th><input id="allRoleIds" name="allRoleIds" type="checkbox"></th> -->
										<th>角色标识</th>
										<th>角色名称</th>
										<th>角色描述</th>
										<th>角色状态</th>
										<!-- <th>操作</th> -->
									</tr>
								</thead>
								<tbody>
								</tbody>
							</table>
						</div>
					</div>
				</div><!-- END col-sm-12 -->
			</div><!-- END row-fluid -->
		</div><!-- END container-fluid -->
		
		<div class="modal fade" id="role-modal" tabindex="-1"></div>
		
		<hejin:commonScript></hejin:commonScript>
		<!-- treegrid js -->
		<script src="static/js/bootstrap/jquery.treegrid.js" type="text/javascript"></script>
		<script src="static/js/bootstrap/jquery.treegrid.bootstrap3.js" type="text/javascript"></script>
		<script type="text/javascript">
			//查看角色
			function viewRole(roleId){
				$("#role-modal").load("system/role/viewRole.do",{roleId:roleId},function(mes){
					$("#role-modal").modal("show");
				});
			}
			
			//编辑角色
			function editRole(roleId){
				$("#role-modal").load("system/role/forwardUpdateRole.do",{roleId:roleId},function(mes){
					$("#role-modal").modal("show");
				});
			}
			
			$(function(){
				//全选
				$("#allRoleIds").click(function() {
					$("input[name='roleIds'][disabled != 'disabled']").prop("checked",$(this).is(":checked"));
				});
				
				//初始化datatable构建角色列表
				var tables = $('#role_tab').platformDataTable({
					//数据源     返回数据必须封装aaData
					"sAjaxSource" : 'system/role/getRoleList.do?timestamp='+ new Date().getTime(),
					"iDisplayStart" : 0,
					"iDisplayLength" : 10,//每页显示6条记录
					"bSort":false,//关闭排序
					"bAutoWidth":true, //列的宽度会根据table的宽度自适应 
					"aoColumns" : [ //列
		                /* {"sClass" : "center",
						 "mDataProp" : "",
						 "bSearchable" : false,
						 "fnRender" : function(obj) {
							 if(obj.aData.roleStatusName == '删除'){
								return '<input name="roleIds" value="' + obj.aData.roleId + '" type="checkbox" disabled="disabled">';
							 }
							return '<input name="roleIds" value="' + obj.aData.roleId + '" type="checkbox" >';
							}
						}, */
						{"sClass" : "center",
						 "mDataProp" : "roleIdentity",
						 "fnRender" : function(obj) {
							return '<a href="javascript:void(0)" onclick=\"viewRole(\'' + obj.aData.roleId + '\')\">' + obj.aData.roleIdentity + '</a>';
							}
						 },
						{"sClass" : "center", "mDataProp" : "roleName"},
						{"sClass" : "center", "mDataProp" : "roleDesc"},
						{"sClass" : "center", "mDataProp" : "roleStatusName"}/*,
						{"sClass" : "center",
						 "mDataProp" : "roleId",
						 "bSearchable" : false,
						 "fnRender" : function(obj) {
							 if(obj.aData.roleStatusName == '删除'){
								 return;
							 }
							 var butt = '<span class="glyphicon glyphicon-pencil" style="cursor: pointer;" onclick=\"editRole(\'' + obj.aData.roleId + '\')\" ></span>';
							 var userPerOpt = {
								 //"update" : butt
							 };
							 return formatCurrentUserPermissionOption(userPerOpt);
						 }
						}*/
					]
				});
				
				// 查询事件
	            $('#searchBtn').click(function(){
					//$("#allRoleIds").attr("checked","false");
	            	var data=[];//存放json类型的查询条件，可以放多个
	            	data.push({ "name": "roleName", "value": $('#roleName').val()},
							{ "name": "roleStatus", "value": $('#status').val()});
	            	//Ajax重新load控件数据
	            	drawTable(tables,data);
					tables.fnPageChange(0);
	            });
				
				//重置
				$("#resetBtn").click(function(){
					$("#roleName").val("");
					$('#status').val('');
				});
				
				//新增角色
				$("#addRoleBtn").click(function(){
					$("#role-modal").load("system/role/forwardAddRole.do");
					$("#role-modal").modal("show");
				});
				
				//删除角色
				$("#deleteRoleBtn").click(function(){
					if(!$("input[name='roleIds']").is(":checked")){
						alert("请选择角色！");
						return false;
					}
					if(confirm("确定要删除角色吗？")){
						var roleIds = "";
						$(":checkbox[name='roleIds']:checked").each(function(i){
							if(i != 0){
								roleIds += "&";
							}
							roleIds += "roleIds="+$(this).val();
						});
						
						$.ajax({
						   type: "POST",
						   url: "system/role/deleteRole.do",
						   data: roleIds,
						   dataType: "json",
						   success: function(res){
								if(res == "0"){
									platformAlert("alert-danger","删除角色失败！",3000);
								}else{
									drawTable(tables,[]);
									$("#allRoleIds").attr("checked",false);
									platformAlert("alert-success","删除角色成功！",3000);
								}
						   },
						   error: function(){
							   platformAlert("alert-danger","删除角色失败！",3000);
						   }
						});
					}else{
						return false;
					}
				});
				
				//禁用角色
				$("#lockRoleBtn").click(function(){
					if(!$("input[name='roleIds']").is(":checked")){
						alert("请选择角色！");
						return false;
					}
					if(confirm("确定要禁用角色吗？")){
						var roleIds = "";
						$(":checkbox[name='roleIds']:checked").each(function(i){
							if(i != 0){
								roleIds += "&";
							}
							roleIds += "roleIds="+$(this).val();
						});
						
						$.ajax({
						   type: "POST",
						   url: "system/role/lockRole.do",
						   data: roleIds,
						   dataType: "json",
						   success: function(res){
								if(res == "0"){
									platformAlert("alert-danger","禁用角色失败！",3000);
								}else{
									drawTable(tables,[]);
									$("#allRoleIds").attr("checked",false);
									platformAlert("alert-success","禁用角色成功！",3000);
								}
						   },
						   error: function(){
							   platformAlert("alert-danger","禁用角色失败！",3000);
						   }
						});
					}else{
						return false;
					}
				});
				
				//启用角色
				$("#unlockRoleBtn").click(function(){
					if(!$("input[name='roleIds']").is(":checked")){
						alert("请选择角色！");
						return false;
					}
					if(confirm("确定要启用角色吗？")){
						var roleIds = "";
						$(":checkbox[name='roleIds']:checked").each(function(i){
							if(i != 0){
								roleIds += "&";
							}
							roleIds += "roleIds="+$(this).val();
						});
						
						$.ajax({
						   type: "POST",
						   url: "system/role/unlockRole.do",
						   data: roleIds,
						   dataType: "json",
						   success: function(res){
								if(res == "0"){
									platformAlert("alert-danger","启用角色失败！",3000);
								}else{
									drawTable(tables,[]);
									$("#allRoleIds").attr("checked",false);
									platformAlert("alert-success","启用角色成功！",3000);
								}
						   },
						   error: function(){
							   platformAlert("alert-danger","启用角色失败！",3000);
						   }
						});
					}else{
						return false;
					}
				});
			});

			function redirect(){
				if($('#page').val() && $('#page').val()>=0){
					var tables = $('#role_tab').dataTable();
					var oSettings = tables.fnSettings();
					var maxPage = oSettings.fnRecordsTotal() % oSettings._iDisplayLength == 0?
					oSettings.fnRecordsTotal() / oSettings._iDisplayLength :
					Math.floor(oSettings.fnRecordsTotal() / oSettings._iDisplayLength)+ 1;
					if($('#page').val() == 0){
						$('#page').val(1);
					}
					if($('#page').val() > maxPage){
						$('#page').val(maxPage);
					}
					var redirectpage = $('#page').val() - 1;
					tables.fnPageChange(redirectpage);
				}else{
					return;
				}
			}

			function onlyNum(){
				$('#page').val($("#page").val().replace(/\D/g, ''));
			}
		</script>
	</body>
</html>