<%@ page language="java" pageEncoding="UTF-8"
         contentType="text/html; charset=UTF-8" %>
<%@ page import="org.apache.commons.lang.StringEscapeUtils" %>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String basePath = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>兰亭集智知识管理系统-权限管理</title>
    <jsp:include page="../../common/head.jsp"></jsp:include>
</head>
<body>
<div class="easyui-layout" title="Register" style="width:100%;height:100%;padding:0px 0px;">
    <table id="dgUserGrant" style="width:100%;height:100%"
           data-options="emptyMsg:'没有符合检索条件的结果',loadMsg:'正在检索请稍后...',singleSelect:true,toolbar:'#tbUserGrantTool',pagination:true,fitColumns:true,url:'<%=basePath%>/user/json/list.action',method:'get'">
        <thead>
        <tr>
            <th data-options="field:'id',width:80">编号</th>
            <th data-options="field:'username',width:150">用户名</th>
            <th data-options="field:'showname',width:200">全称</th>
            <th data-options="field:'accountlock',width:60,formatter:function(lock){
                if(lock==1)
                    return '锁定';
                else
                    return '正常';
            }">状态
            </th>
            <th data-options="field:'addtime',width:200,formatter:function(date){
            return datetimeformat('Y-m-d H:i:s',date);
            }">添加时间
            </th>
            <th data-options="field:'_operation',width:180,formatter:optFormatter">操作</th>
        </tr>
        </thead>
    </table>

    <div id="userGrantModule" class="easyui-window" title="用户授权管理" data-options="modal:true,closed:true,collapsible:false,resizable:false,minimizable:false,maximizable:false,iconCls:'icon-save'" style="width:500px;height:390px;padding:10px;">
        <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'center'" style="padding:10px;">
                <form>
                    <div style="margin-bottom:20px">
                        <input id="ugmId" type="hidden">
                    </div>
                    <div style="margin-bottom:20px">
                        <input class="easyui-textbox" id="ugmName" style="width:100%" data-options="label:'用户名称:',readonly:true">
                    </div>
                    <div style="margin-bottom:20px">
                        <input id="ccUserGrantModule" class="easyui-combotree" data-options="url:'<%=basePath%>/role/json/combox/list.action?combox=false',method:'get',label:'所属角色:',labelPosition:'left',multiple:true,multiline:true" style="width:100%">
                    </div>
                    <div style="margin-bottom:20px">
                        <input class="easyui-textbox" label="授权说明:" data-options="readonly:true" labelPosition="left" multiline="true" value="父级角色拥有的最大范围的用户权限，其子角色默认拥将有其所有权限，子角色可以在父角色的权限范围内进一步缩小权限范围。" style="width:100%;height:100px">
                    </div>
                </form>
            </div>
            <div data-options="region:'south',border:false" style="text-align:center;padding:5px 0 0;">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="fa fa-floppy-o" onclick="grantRoleUser()" style="width:80px">授权</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="fa fa-times" onclick="grantCancle()" style="width:80px">取消</a>
            </div>
        </div>
    </div>


    <div id="userGrantRole" class="easyui-window" title="角色授权管理" data-options="modal:true,closed:true,collapsible:false,resizable:false,minimizable:false,maximizable:false,iconCls:'icon-save'" style="width:500px;height:390px;padding:10px;">
        <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'center'" style="padding:10px;">
                <form>
                    <div style="margin-bottom:20px">
                        <input id="ugrId" type="hidden">
                    </div>
                    <div style="margin-bottom:20px">
                        <input class="easyui-textbox" id="ugrName" style="width:100%" data-options="label:'用户名称:',readonly:true">
                    </div>
                    <div style="margin-bottom:20px">
                        <input id="comboxUserGrantModule" class="easyui-combotree" data-options="url:'<%=basePath%>/role/menu/list.action',method:'get',label:'系统模块:',labelPosition:'left',multiple:true,multiline:true" style="width:100%">
                    </div>
                    <div style="margin-bottom:20px">
                        <input class="easyui-textbox" label="授权说明:" data-options="readonly:true" labelPosition="left" multiline="true" value="父级角色拥有的最大范围的用户权限，其子角色默认拥将有其所有权限，子角色可以在父角色的权限范围内进一步缩小权限范围。" style="width:100%;height:100px">
                    </div>
                </form>
            </div>
            <div data-options="region:'south',border:false" style="text-align:center;padding:5px 0 0;">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="fa fa-floppy-o" onclick="grantUserModule()" style="width:80px">授权</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="fa fa-times" onclick="grantUserModuleCancle()" style="width:80px">取消</a>
            </div>
        </div>
    </div>

    <div id="tbUserGrantTool" style="padding-left: 10px;">
        <input class="easyui-searchbox" style="width: 200px;" data-options="prompt:'请输入检索关键词',searcher:search">
        <a href="javascript:void(0);" onclick="search()" class="easyui-linkbutton" data-options="iconCls:'fa fa-search-plus',plain:true">开始检索</a>
        <a href="javascript:void(0);" onclick="addUserInfo()" class="easyui-linkbutton" data-options="iconCls:'fa fa-arrows',plain:true">添加用户</a>
        <a href="javascript:void(0);" onclick="refreshGrid()" class="easyui-linkbutton" data-options="iconCls:'fa fa-repeat',plain:true">刷新用户</a>
    </div>
    <script type="text/javascript">
        function search(){
            return false;
        }
        function optFormatter(value, row, index) {
            var optStr="";
            if (row.id>1) {
                optStr += "<a href='javascript:void(0);' onclick='return grantRole(" + index + ");'>角色授权</a>&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' onclick='return grantUserModuleWindows(" + index + ");'>模块授权</a>";
            }else{
                optStr="--";
            }
            return optStr;
        }
        function grantRole(index){
            if (index < 0) {
                $.messager.alert('温馨提示', '系统参数错误！');
            } else {
                $("#ugmId").val("");
                $("#ugmName").textbox("clear");
                $('#ccUserGrantModule').combotree('clear');
                var row = $('#dgUserGrant').datagrid('getRows')[index];
                $("#ugmId").val(row.id);
                $("#ugmName").textbox("setValue", row.showname);
                $('#ugmName').textbox('readonly',true);
                var win = null;
                $.ajax({
                    type:"post",//请求方式
                    url:"<%=basePath %>/user/json/role/list.action",//发送请求地址
                    timeout:30000,//超时时间：30秒
                    dataType:"json",//设置返回数据的格式
                    data: {uid:row.id},
                    //请求成功后的回调函数 data为json格式
                    beforeSend:function(XMLHttpRequest){
                        win=$.messager.progress({
                            title:'获取信息',
                            msg:'正在获取角色信息...'
                        });
                    },
                    success:function(data){
                        var roles=Array();
                        if(data!=null && data.length>0){
                            $.each(data,function(index,element){
                                roles.push(element.roleid);
                            });
                        }
                        if(roles.length>0){
                            $('#ccUserGrantModule').combotree('setValues',roles);
                        }else{
                            $('#ccUserGrantModule').combotree('clear');
                        }
                    },
                    complete:function(XMLHttpRequest, textStatus){
                        $.messager.progress('close');
                    },
                    error:function(){
                        alert("请求出错");
                    }
                });
                $('#userGrantModule').window('open');
            }
            return false;
        }
        function grantRoleUser(){
            var strId=$("#ugmId").val();
            if(strId=="" || strId.length<=0){
                $.messager.alert('温馨提示', '系统参数错误！');
                return false;
            }
            var id=parseInt(strId);
            if (id < 0) {
                $.messager.alert('温馨提示', '系统参数错误！');
            } else {
                var win = null;
                var moduelIds=$('#ccUserGrantModule').combotree('getValues');
                $.ajax({
                    type:"post",
                    url:"<%=basePath %>/user/json/role/grant.action",
                    timeout:30000,
                    dataType:"json",
                    data: {uid:id,ids:moduelIds.join(";")},
                    beforeSend:function(XMLHttpRequest){
                        win=$.messager.progress({
                            title:'用户授权',
                            msg:'正在授权...'
                        });
                    },
                    success:function(data){
                        if(data.status==1){
                            $.messager.alert({title:'温馨提示',msg:data.message,fn:function(){
                                $('#userGrantModule').window('close');
                            }});
                        }else{
                            $.messager.alert('温馨提示',data.message);
                        }
                    },
                    complete:function(XMLHttpRequest, textStatus){
                        $.messager.progress('close');
                    },
                    error:function(){
                        $.messager.alert('温馨提示', '请求出错！');
                    }
                });
            }
            return false;
        }
        function grantCancle(){
            $("#rgmId").val("");
            $("#rgmName").textbox("clear");
            $('#ccUserGrantModule').combotree('clear');
            $('#userGrantModule').window('close');
        }
        /********/
        function grantUserModuleWindows(index){
            if (index < 0) {
                $.messager.alert('温馨提示', '系统参数错误！');
            } else {
                $("#ugrId").val("");
                $("#ugrName").textbox("clear");
                $('#comboxUserGrantModule').combotree('clear');
                var row = $('#dgUserGrant').datagrid('getRows')[index];
                $("#ugrId").val(row.id);
                $("#ugrName").textbox("setValue", row.showname);
                $('#ugrName').textbox('readonly',true);
                $('#comboxUserGrantModule').combotree('reload',"<%=basePath%>/user/json/menu/list.action?uid="+row.id);
                var win = null;
                $.ajax({
                    type:"post",//请求方式
                    url:"<%=basePath %>/user/json/module/list.action",//发送请求地址
                    timeout:30000,//超时时间：30秒
                    dataType:"json",//设置返回数据的格式
                    data: {uid:row.id},
                    //请求成功后的回调函数 data为json格式
                    beforeSend:function(XMLHttpRequest){
                        win=$.messager.progress({
                            title:'获取信息',
                            msg:'正在获取角色信息...'
                        });
                    },
                    success:function(data){
                        var modules=Array();
                        if(data!=null && data.length>0){
                            $.each(data,function(index,element){
                                modules.push(element.moduleid);
                            });
                        }
                        if(modules.length>0){
                            $('#comboxUserGrantModule').combotree('setValues',modules);
                        }else{
                            $('#comboxUserGrantModule').combotree('clear');
                        }
                    },
                    complete:function(XMLHttpRequest, textStatus){
                        $.messager.progress('close');
                    },
                    error:function(){
                        alert("请求出错");
                    }
                });
                $('#userGrantRole').window('open');
            }
            return false;
        }
        function grantUserModule(){
            var strId=$("#ugrId").val();
            if(strId=="" || strId.length<=0){
                $.messager.alert('温馨提示', '系统参数错误！');
                return false;
            }
            var id=parseInt(strId);
            if (id < 0) {
                $.messager.alert('温馨提示', '系统参数错误！');
            } else {
                var win = null;
                var moduelIds=$('#comboxUserGrantModule').combotree('getValues');
                $.ajax({
                    type:"post",
                    url:"<%=basePath %>/user/json/module/grant.action",
                    timeout:30000,
                    dataType:"json",
                    data: {uid:id,ids:moduelIds.join(";")},
                    beforeSend:function(XMLHttpRequest){
                        win=$.messager.progress({
                            title:'用户授权',
                            msg:'正在授权...'
                        });
                    },
                    success:function(data){
                        if(data.status==1){
                            $.messager.alert({title:'温馨提示',msg:data.message,fn:function(){
                                $('#userGrantModule').window('close');
                            }});
                        }else{
                            $.messager.alert('温馨提示',data.message);
                        }
                    },
                    complete:function(XMLHttpRequest, textStatus){
                        $.messager.progress('close');
                    },
                    error:function(){
                        $.messager.alert('温馨提示', '请求出错！');
                    }
                });
            }
            return false;
        }
        function grantUserModuleCancle(){
            $("#ugrId").val("");
            $("#ugrName").textbox("clear");
            $('#comboxUserGrantModule').combotree('clear');
            $('#userGrantRole').window('close');
        }
        /********/
        $(function () {
            var pager = $('#dgUserGrant').datagrid().datagrid('getPager');
            pager.pagination({
                showPageList:false
            });
        })

       function refreshGrid(){
           $('#dgUserGrant').datagrid('reload');
       }
    </script>
</div>
</body>
</html>