<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../../common/init.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel='stylesheet' href='${ctx}/resources/css/easyui.css' type='text/css' media='screen' />
<link rel='stylesheet' href='${ctx}/resources/css/easyui/icon.css' type='text/css' media='screen' />

<link rel="stylesheet" href="${ctx}/resources/js/multiselect/jquery.multiselect.css" type="text/css" />
<%@ include file="../../common/top.jsp"%>
<script type="text/javascript" src="${ctx}/resources/js/multiselect/jquery-ui.min.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/multiselect/jquery.multiselect.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/jquery.checktable.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${ctx}/resources/js/easyui-lang-zh_CN.js"></script>
<%@ include file="../../common/validation.jsp"%>
<title>用户管理</title>
<style>
    #user-info{
         box-shadow: 0 0 6px #aaa;
    }
</style>
<script language="javascript">
$(document).ready(function(){
    $('#search-form').submit(function(event){
        event.preventDefault(); 
        var param = $('#search-form').serializeJson();
        param = $.extend(param, {page:1,rows:10});
        $('#dg').datagrid('load',param);
    });
    
    $("#searchBtn").click(function(){
    	$('#search-form').submit();
    });
});

function addUser(){
	window.location.href = "${ctx}/user/add";
}
function editUser(){
	var row = $('#dg').datagrid('getSelected');
    if (row){
        window.location.href = "${ctx}/user/editUser/" + row.id;
    }
    else{
    	$.messager.alert('提示','请选择需要修改的用户');
    }
}
function activeUser(){
	var row = $('#dg').datagrid('getSelected');
	if(row.isEnabled!="1"){
		var obj={
				"id" :row.id	
			};
			var User = JSON.stringify(obj);
			 $.ajax({
					type : "POST",
					url : "${ctx}/user/updateStatus/active",
					data : User,
					contentType : "application/json; charset=utf-8",
					dataType : "json",
					success : function(callback) {
						callback = eval(callback);
						if(callback.type=="fail"){
							$.messager.alert('提示','操作失败');
						}else{
							var rowIndex = $('#dg').datagrid('getRowIndex',row);
				            $('#dg').datagrid('updateRow',{
				                index: rowIndex,
				                row: {isEnabled: 1}
				            });
						}
					}
				});
    }
}
function inactiveUser(){
	var row = $('#dg').datagrid('getSelected');
    if(row.isEnabled!="0"){
    	var obj={
			"id" :row.id	
		};
		var User = JSON.stringify(obj);
		 $.ajax({
				type : "POST",
				url : "${ctx}/user/updateStatus/inactive",
				data : User,
				contentType : "application/json; charset=utf-8",
				dataType : "json",
				success : function(callback) {
					callback = eval(callback);
					if(callback.type=="fail"){
						$.messager.alert('提示','操作失败');
					}else{
						 var rowIndex = $('#dg').datagrid('getRowIndex',row);
				            $('#dg').datagrid('updateRow',{
				                index: rowIndex,
				                row: {isEnabled: 0}
				            });

					}
				}
			});
    }
}

function resetStatus(){
	var row = $('#dg').datagrid('getSelected');
	 if (row){
		 var obj={
					"id" :row.id	 
				};
				var User = JSON.stringify(obj);
		 $.ajax({
				type : "POST",
				url : "${ctx}/user/resetPwd",
				data : User,
				contentType : "application/json; charset=utf-8",
				dataType : "json",
				success : function(callback) {
					callback = eval(callback);
					if(callback.type=="success"){
						$.messager.alert('提示','重置该用户密码成功!');
					}else{
						$.messager.alert('提示','重置该用户密码失败!');
					}
				}
			});
	 }else{
		 $.messager.alert('提示','请选择需要重置密码的用户');
	 }
	
}
function isEnabledFormatter(value,row,index){
	if(value==1){
		return "启用";
	}else{
		return "停用";
	}
}
</script>
</head>
<body>
<div class="container-fluid">
    <%@ include file="../../common/header.jsp"%>
    <div class="row-fluid">
        <%@ include file="../../common/sub_menu.jsp"%>
        <div class="span10">
        	<ul class="breadcrumb">
	            <li class="active">用户管理</li>
            </ul>
	        <div class="page-body">
	            <form id="search-form" method="post" >
	            	<div class="change">
	            		<ul>
	            			<li><span>角色</span>
								<select name="roleId" class="select-medium">
									<option value="">选择角色</option>
					                <c:forEach var="role" items="${roles }">
					                    <option value="${role.id }">${role.name }</option>
					                </c:forEach>
								</select>
							</li>
	            			<li><span>姓名</span><input class="input-medium" type="text" name="name"  id="name" placeholder="姓名"></li>
	            			<li>
								<a id="searchBtn" href="#" type="submit" class="btn"><i class="icon-search"></i> 查询</a>
							</li>
	            		</ul>
	            	</div>
	            </form>
		        <div style="clear:both;"></div>
                 <table id="dg" class="easyui-datagrid"
                     data-options="url:'${ctx}/user/queryUsers'
                     ,toolbar: '#tb'
                     ,fitColumns:true
                     ,singleSelect:true
                     ,striped:true
                     ,pagination:true">
                     <thead>
                         <tr class="title">
                             <th data-options="field:'id',width:60">ID</th>
                             <th data-options="field:'loginName',width:100">登录名</th>
                             <th data-options="field:'name',width:100">姓名</th>
                             <th data-options="field:'roleString',width:100">角色</th>
                             <th data-options="field:'isEnabled',width:80,
                                   formatter: isEnabledFormatter">状态</th>
                         </tr>
                     </thead>
                   </table>
                   
                   <div id="tb" style="height:auto">
                       <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-add',plain:true" onclick="addUser()">添加</a>
                       <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-edit',plain:true" onclick="editUser()">编辑</a>
                       <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-confirm',plain:true" onclick="activeUser()">启用</a>
                       <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-no',plain:true" onclick="inactiveUser()">停用</a>
                       <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'easyui-icon-undo',plain:true" onclick="resetStatus()">重置密码</a>
                   </div>
		        </div>
        	</div>
    	</div>
   	</div>
  	<%@ include file="../../common/footer.jsp"%>
</body>
</html>