<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/jsp/common/taglibs.jspf"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<hejin:header title="资源管理 - "></hejin:header>
	</head>
	<body class="home-template">
		<!-- 菜单 -->
		<jsp:include page="../../menu.jsp"/>
		<!-- 面包屑 -->
		<div class="container-fluid">
			<ol class="breadcrumb"><!-- BEGIN 面包屑 -->
				<li><a href="#">系统管理</a></li>
				<li><a href="system/res/resIndex.do">资源管理</a></li>
			</ol><!-- END 面包屑 -->
		</div>
		
		<form id="resForm" action="" method="post">
			<!-- <div class="container-fluid">
				<div class="row">
					<div class="col-sm-1">
	  					<span class="col-sm-12" style="text-align: right;padding-top: 5px;">名称</span>
					</div>
					<div class="col-sm-2">
						<div class="form-group">
	 						<input type="text" class="form-control" id="resName" name="resName" value="${resName }" placeholder="请输入名称">
	 					</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> -->

			<div class="container-fluid">

				<div class="row">
					<div class="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="addResBtn" type="button" class="btn btn-primary">新增</button>
											</div>
										</shiro:hasPermission>
										<shiro:hasPermission name="${_current_menu }:delete">
											<div class="btn-group">
												<button id="deleteResBtn" type="button" class="btn btn-primary">删除</button>
											</div>
										</shiro:hasPermission>
										<shiro:hasPermission name="${_current_menu }:update">
											<div class="btn-group">
												<button id="lockResBtn" type="button" class="btn btn-primary">禁用</button>
											</div>
										</shiro:hasPermission>
										<shiro:hasPermission name="${_current_menu }:update">
											<div class="btn-group">
												<button id="unlockResBtn" type="button" class="btn btn-primary">启用</button>
											</div>
										</shiro:hasPermission>
									</div>
								</div> -->
							</div>
							<div class="box-body table-responsive">
								<table id="dataTable" class="table table-bordered">
									<thead>
										<tr>
											<!-- <th><input type="checkbox" id="allResIds" name="allResIds"></th> -->
											<th>资源名称</th>
											<th style="width: 20%;">资源内容</th>
											<th>资源权限标识</th>
											<th>资源排序号</th>
											<th width="8%">资源类型</th>
											<th width="8%">资源状态</th>
											<!-- <shiro:hasPermission name="${_current_menu }:update">
												<th>操作</th>
											</shiro:hasPermission> -->
										</tr>
									</thead>
					                <tbody>
					                	<c:forEach items="${resList }" var="res">
					                		<tr id="menu-${res.resParentId }-${res.resId}"
					                			<c:if test="${(res.level > 0) }">
					                				style="display:none;"
					                			</c:if>
					                		>
												<!-- <td>
					                				<input type="checkbox" name="resIds" value="${res.resId }"/>
					                			</td> -->
					                			<td>
					                				<c:forEach begin="1" end="${res.level }" var="level">
				                						&nbsp;&nbsp;
				                					</c:forEach>
					                				<c:if test="${!res.leaf }">
					                					<span id="${res.resId }" class="glyphicon glyphicon-plus" style="cursor: pointer;" onclick="expandToggle(this)"></span>
					                				</c:if>
					                				<c:if test="${!(res.resIcon ==null || res.resIcon == '') }">
					                					<span class="${res.resIcon }"></span>
					                				</c:if>
					                				<a href="javascript:showRes('${res.resId }')">${res.resName }</a>
					                			</td>
					                			<td>${res.resContent }</td>
					                			<td>${res.resIdentity }</td>
					                			<td>${res.resSeq }</td>
					                			<td>${res.resTypeName }</td>
					                			<td>${res.resStatusName }</td>
					                			<!-- <shiro:hasPermission name="${_current_menu }:update">
						                			<td>
						                				<span class="glyphicon glyphicon-pencil" style="cursor: pointer;" 
						                					onclick="javascript:updateRes('${res.resId}')">
						                				</span>
						                			</td>
					                			</shiro:hasPermission> -->
					                		</tr>
					                	</c:forEach>
					                </tbody>
				             	</table>
				             </div>
						</div>
					</div>
				</div>
			</div>
		</form>
		
		<!-- COMPOSE MESSAGE MODAL -->
        <div class="modal fade" id="compose-modal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title"><i class="fa fa-envelope-o"></i>查看资源</h4>
                    </div>
					<div class="modal-body">
						<div class="container-fluid">
							<div class="row">
								<div class="col-sm-3" style="text-align: right;">
									<label >资源名称</label>
								</div>
								<div id="view_res_name" class="col-sm-9" style="text-align: left;">
									
								</div>
							</div>
							
							<div class="row">
								<div class="col-sm-3" style="text-align: right;">
									<label >资源内容</label>
								</div>
								<div id="view_res_content" class="col-sm-9" style="text-align: left;word-break:break-all;">
									
								</div>
							</div>
							<div class="row">
								<div class="col-sm-3" style="text-align: right;">
									<label >资源权限标识</label>
								</div>
								<div id="view_res_identity" class="col-sm-9" style="text-align: left;word-break:break-all;">
									
								</div>
							</div>
							<div class="row">
								<div class="col-sm-3" style="text-align: right;">
									<label >资源图标</label>
								</div>
								<div id="view_res_icon" class="col-sm-9" style="text-align: left;">
									
								</div>
							</div>
							
							<div class="row">
								<div class="col-sm-3" style="text-align: right;">
									<label >上级资源</label>
								</div>
								<div id="view_res_parent" class="col-sm-9" style="text-align: left;">
									
								</div>
							</div>
							<div class="row">
								<div class="col-sm-3" style="text-align: right;">
									<label >资源类型</label>
								</div>
								<div id="view_res_type" class="col-sm-9" style="text-align: left;">
									
								</div>
							</div>
							<div class="row">
								<div class="col-sm-3" style="text-align: right;">
									<label >资源排序号</label>
								</div>
								<div id="view_res_seq" class="col-sm-9" style="text-align: left;">
									
								</div>
							</div>
							<div class="row">
								<div class="col-sm-3" style="text-align: right;">
									<label >资源状态</label>
								</div>
								<div id="view_res_status" class="col-sm-9" style="text-align: left;">
									
								</div>
							</div>
							<div class="row">
								<div class="col-sm-3" style="text-align: right;">
									<label >资源描述</label>
								</div>
								<div id="view_res_desc" class="col-sm-9" style="text-align: left;word-break:break-all;">
									
								</div>
							</div>
						</div>
					</div>
					<div class="modal-footer clearfix">
					    <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-times"></i>取消</button>
					</div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
        
        <div class="modal fade" id="res-edit-modal" tabindex="-1">
        </div>
        
        <hejin:commonScript></hejin:commonScript>
	    <script type="text/javascript">
	    	
			//取得参数的函数
			function expandToggle(element){
				//alert("tr[id^='menu-"+$(element).attr("id")+"-']");
				if($(element).hasClass("glyphicon-plus")){
					$(element).removeClass("glyphicon-plus");
					$(element).addClass("glyphicon-minus");
					$("tr[id^='menu-"+$(element).attr("id")+"-']").show();
				}else{
					mergeChild(element);
				}
			}
			
			//通过递归方式将所有的子资源全部合并
			function mergeChild(element){
				
				var childMinus = $("tr[id^='menu-"+$(element).attr("id")+"-']").eq(0).find('.glyphicon-minus');
				if(childMinus.size() > 0){
					for(var i = 0; i < childMinus.size(); i++){
						mergeChild(childMinus.eq(i)[0]);
					}
				}
				$(element).addClass("glyphicon-plus");
				$(element).removeClass("glyphicon-minus");
				$("tr[id^='menu-"+$(element).attr("id")+"-']").hide();
				
			}
			
			//查看资源信息
			function showRes(resId){
				$("#view_res_name").html("");
				$("#view_res_identity").html("");
				$("#view_res_content").html("");
				//'<span class='+res.resIcon+'></span>'
				$("#view_res_icon").html("");
				$("#view_res_desc").html("");
				$("#view_res_parent").html("");
				$("#view_res_type").html("");
				$("#view_res_seq").html("");
				$("#view_res_status").html("");
				$.ajax({
				   type: "POST",
				   url: "system/res/getResInfo.do",
				   data: "resId="+resId,
				   dataType: "json",
				   success: function(res){
						$("#view_res_name").html(res.resName);
						$("#view_res_identity").html(res.resIdentity);
						$("#view_res_content").html(res.resContent);
						//'<span class='+res.resIcon+'></span>'
						$("#view_res_icon").html(res.resIcon);
						$("#view_res_desc").html(res.resDesc);
						$("#view_res_parent").html(res.resParentName);
						$("#view_res_type").html(res.resType);
						$("#view_res_seq").html(res.resSeq);
						$("#view_res_status").html(res.resStatus);
						$("#compose-modal").modal('show');
				   },
				   error: function(){
					   platformDanger("获得资源信息失败！");
				   }
				});
				
			}
			
			function updateRes(resId){
				$("#res-edit-modal").load("system/res/forwardUpdateRes.do",{resId:resId},function(mes){
					$("#res-edit-modal").modal("show");
				});
				
			}
			
			$(function(){
				$("#allResIds").click(function() {
					$("input[name='resIds']").prop("checked",$(this).is(":checked"));
				});
				
				//新增资源
				$("#addResBtn").click(function(){
					$("#res-edit-modal").load("system/res/forwardAddRes.do");
					$("#res-edit-modal").modal("show");
				});
				
				//删除资源
				$("#deleteResBtn").click(function(){
					if(!$("input[name='resIds']").is(":checked")){
						platformDanger("请选择资源！");
						return false;
					}
					if(confirm("确定要删除资源吗？")){
						var checkedRes = $("input[name='resIds']:checked");
						var resIds = "";
						var checkedResSize = checkedRes.size();
						for(var i = 0; i < checkedResSize; i++){
							var curRes = checkedRes.eq(i);
							if(i != 0){
								resIds += "&";
							}
							resIds += "resIds="+curRes.val();
						}
						$.ajax({
						   type: "POST",
						   url: "system/res/deleteRes.do",
						   data: resIds,
						   dataType: "json",
						   success: function(res){
								if(res.result == "success"){
									window.location.href="<%=request.getContextPath()%>/system/res/resIndex.do";
								}else{
									platformDanger(res.message);
								}
						   },
						   error: function(){
							   platformDanger("删除资源失败！");
						   }
						});
					}else{
						return false;
					}
				});
				
				//禁用资源
				$("#lockResBtn").click(function(){
					if(!$("input[name='resIds']").is(":checked")){
						platformDanger("请选择资源！");
						return false;
					}
					if(confirm("确定要禁用资源吗？")){
						var checkedRes = $("input[name='resIds']:checked");
						var resIds = "";
						var checkedResSize = checkedRes.size();
						for(var i = 0; i < checkedResSize; i++){
							var curRes = checkedRes.eq(i);
							if(i != 0){
								resIds += "&";
							}
							resIds += "resIds="+curRes.val();
						}
						$.ajax({
						   type: "POST",
						   url: "system/res/lockRes.do",
						   data: resIds,
						   dataType: "json",
						   success: function(res){
								if(res.result == "success"){
									window.location.href="<%=request.getContextPath()%>/system/res/resIndex.do";
								}else{
									platformDanger(res.message);
								}
						   },
						   error: function(){
							   platformDanger("禁用资源失败！");
						   }
						});
					}else{
						return false;
					}
				});
				
				//启用资源
				$("#unlockResBtn").click(function(){
					if(!$("input[name='resIds']").is(":checked")){
						platformDanger("请选择资源！");
						return false;
					}
					if(confirm("确定要启用资源吗？")){
						$("#resForm").attr("action", "system/res/unlockRes.do");
						$("#resForm").submit();
					}else{
						return false;
					}
				});
				
				//查询
				/*$("#searchBtn").click(function() {
					$("#resForm").attr("action", "system/res/resIndex.do");
					$("#resForm").submit();
				});*/
				
				//重置
				$("#resetBtn").click(function(){
					$("#resName").val("");
				});
			});
		</script>
	</body>
</html>