<%@ 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="dgRole" class="easyui-treegrid" style="width:100%;height:100%"
           data-options="idField: 'id',treeField: 'rolename',toolbar:toolbar,lines:true,emptyMsg:'没有符合检索条件的结果',loadMsg:'正在检索请稍后...',singleSelect:true,fitColumns:true,url:'<%=basePath%>/role/json/list.action',method:'get'">
        <thead>
        <tr>
            <th data-options="field:'rolename',width:200">角色名称</th>
            <th data-options="field:'rolecode',width:150">角色代码</th>
            <th data-options="field:'id',width:80">编号</th>
            <th data-options="field:'addtiime',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="editRole" 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="roleid" type="hidden">
                        <input class="easyui-textbox" id="rolecode" style="width:100%" data-options="label:'角色代码:',required:true">
                    </div>
                    <div style="margin-bottom:20px">
                        <input class="easyui-textbox" id="rolename" style="width:100%" data-options="label:'角色名称:',required:true">
                    </div>
                    <div style="margin-bottom:20px">
                        <input class="easyui-combotree" id="parentid" data-options="idField: 'id',treeField: 'text',textField:'text',url:'<%=basePath%>/role/json/combox/list.action',method:'get',label:'父级角色:',labelPosition:'left,line:true'" style="width:100%">
                    </div>
                    <div style="margin-bottom:20px">
                        <input class="easyui-textbox" id="addtime" style="width:100%;" data-options="label:'注册时间:',disabled:true">
                    </div>
                </form>
            </div>
            <div data-options="region:'south',border:false" style="text-align:center;padding:5px 0 0;">
                <a id="btnSaveRole" href="javascript:void(0)" class="easyui-linkbutton" iconCls="fa fa-floppy-o" onclick="saveRoleInfo()" style="width:80px">保存</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="fa fa-times" onclick="roleCancle()" style="width:80px">取消</a>
            </div>
        </div>
    </div>
    <div id="roleGrantModule" 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="rgmId" type="hidden">
                    </div>
                    <div style="margin-bottom:20px">
                        <input class="easyui-textbox" id="rgmName" style="width:100%" data-options="label:'角色名称:',readonly:true">
                    </div>
                    <div style="margin-bottom:20px">
                        <input id="ccGrantModule" 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="grantRoleModule()" 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>
    <script type="text/javascript">
        function optFormatter(value, row, index) {
            if (row.id == 1) {
                return "--"
            } else {
                return "<a href='javascript:void(0);' onclick='viewRoleInfo(" + row.id + ");'>查看</a>&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' onclick='editRoleInfo(" + row.id + ");'>编辑</a>&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' onclick='grantModule(" + row.id + ");'>授权</a>&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' onclick='del(" + row.id + ");'>删除</a>";
            }
        }
        function viewRoleInfo(id) {
            $('#rolecode').textbox('disable');
            $('#rolename').textbox('disable');
            $('#addtime').textbox('disable');
            $('#parentid').combotree('disable');
            if (id < 0) {
                $.messager.alert('温馨提示', '系统参数错误！');
            } else {
                var row = $('#dgRole').treegrid('find',id);
                //$("#roleid").val(row.id);
                $("#rolecode").textbox("setValue", row.rolecode);
                $("#rolename").textbox("setValue", row.rolename);
                $("#addtime").textbox("setValue", datetimeformat('Y-m-d H:i:s',row.addtiime));
                var parentId=-1;
                if(row!=null&& row.parentid!=undefined && row.parentid!=null)
                    parentId=row.parentid;
                $('#parentid').combotree('setValue',parentId);
                $('#btnSaveRole').linkbutton('disable');
                $('#editRole').window('open');
            }
            return false;
        }
        function saveRoleInfo(){
            var win = null;
            var roleId=$("#roleid").val();
            var parentid=$("#parentid").combotree("getValue");
            var rolecode=$("#rolecode").textbox("getValue");
            var rolename=$("#rolename").textbox("getValue");
            $.ajax({
                type:"post",
                url:"<%=basePath %>/role/json/role/save.action",//json/role/save.action
                timeout:30000,
                dataType:"json",
                data: {role:roleId,parentid:parentid,code:rolecode,name:rolename},
                beforeSend:function(XMLHttpRequest){
                    win=$.messager.progress({
                        title:'角色管理',
                        msg:'正在添加...'
                    });
                },
                success:function(data){
                    if(data.status==1){
                        $.messager.alert({title:'温馨提示',msg:data.message,fn:function(){
                            $('#dgRole').treegrid('reload');
                            $('#editRole').window('close');
                        }});
                    }else{
                        $.messager.alert('温馨提示',data.message);
                    }
                },
                complete:function(XMLHttpRequest, textStatus){
                    $.messager.progress('close');
                },
                error:function(){
                    $.messager.alert('温馨提示', '请求出错！');
                }
            });
        }
        function addRoleInfo(){
            $("#roleid").val("");
            $("#rolecode").textbox("clear");
            $("#rolename").textbox("clear");

            $('#rolecode').textbox('readonly',false);

            $('#addtime').textbox('readonly',true);
            $('#addtime').textbox('disable');

            $("#rolecode").textbox("enable");
            $("#rolename").textbox("enable");
            $('#parentid').combotree('enable');
            $('#parentid').combotree('setValue',-1);
            $("#addtime").textbox("setValue", datetimeformat('Y-m-d H:i:s',new Date()));
            $('#btnSaveRole').linkbutton('enable');
            $('#editRole').window('open');
            return false;
        }

        function editRoleInfo(id) {
            $('#rolecode').textbox('enable');
            $('#rolename').textbox('enable');
            $('#addtime').textbox('enable');
            $('#parentid').combotree('enable');
            if (id < 0) {
                $.messager.alert('温馨提示', '系统参数错误！');
            } else {
                var row = $('#dgRole').treegrid('find',id);
                $("#roleid").val(row.id);
                $("#rolecode").textbox("setValue", row.rolecode);
                $('#rolecode').textbox('readonly',true);
                $('#rolecode').textbox('disable');
                $("#rolename").textbox("setValue", row.rolename);
                $("#addtime").textbox("setValue", datetimeformat('Y-m-d H:i:s',row.addtiime));
                $('#addtime').textbox('readonly',true);
                $('#addtime').textbox('disable');
                var parentId=-1;
                if(row!=null&& row.parentid!=undefined && row.parentid!=null)
                    parentId=row.parentid;
                $('#parentid').combotree('setValue',parentId);
                $('#btnSaveRole').linkbutton('enable');
                $('#editRole').window('open');
            }
            return false;
        }
        function grantRoleModule(){
            var strId=$("#rgmId").val();
            if(strId=="" || strId.length<=0){
                $.messager.alert('温馨提示', '系统参数错误！');
                return false;
            }
            var id=parseInt(strId);
            if (id < 0) {
                $.messager.alert('温馨提示', '系统参数错误！');
            } else {
                var row = $('#dgRole').treegrid('find',id);
                var win = null;
                var moduelIds=$('#ccGrantModule').combotree('getValues');
                $.ajax({
                    type:"post",
                    url:"<%=basePath %>/role/json/module/save.action",
                    timeout:30000,
                    dataType:"json",
                    data: {role:row.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(){
                                $('#roleGrantModule').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");
            $('#ccGrantModule').combotree('clear');
            $('#roleGrantModule').window('close');
        }

        function grantModule(id){
            if (id < 0) {
                $.messager.alert('温馨提示', '系统参数错误！');
            } else {
                var row = $('#dgRole').treegrid('find',id);
                $("#rgmId").val(row.id);
                $("#rgmName").textbox("setValue", row.rolename);
                $('#rgmName').textbox('readonly',true);
                $('#ccGrantModule').combotree('reload',"<%=basePath%>/role/menu/list.action?role="+row.id);
                var win = null;
                $.ajax({
                    type:"post",//请求方式
                    url:"<%=basePath %>/role/json/module/list.action",//发送请求地址
                    timeout:30000,//超时时间：30秒
                    dataType:"json",//设置返回数据的格式
                    data: {role: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){
                            $('#ccGrantModule').combotree('setValues',modules);
                        }else{
                            $('#ccGrantModule').combotree('clear');
                        }
                    },
                    complete:function(XMLHttpRequest, textStatus){
                        $.messager.progress('close');
                    },
                    error:function(){
                        alert("请求出错");
                    }
                });
                $('#roleGrantModule').window('open');
            }
            return false;
        }

        function roleCancle(){
            $("#roleid").val("");
            $("#rolecode").textbox("clear");
            $("#rolename").textbox("clear");
            $('#parentid').combotree('clear');
            $('#editRole').window('close');
        }

        function del(id) {
            var row = $('#dgRole').treegrid('find',id);
            if (row.id == 1) {
                $.messager.alert('温馨提示', '当前角色不允许删除！');
            } else {
                $.messager.confirm("温馨提示", "角色删除收将不能恢复,你确定要删除角色【" + row.rolename + "】吗?", function (r) {
                    if (r) {
                        var win = null;
                        $.ajax({
                            type:"post",
                            url:"<%=basePath %>/role/json/role/delete.action",
                            timeout:30000,
                            dataType:"json",
                            data: {role:row.id},
                            beforeSend:function(XMLHttpRequest){
                                win=$.messager.progress({
                                    title:'角色管理',
                                    msg:'正在校验删除...'
                                });
                            },
                            success:function(data){
                                if(data.status==1){
                                    $.messager.alert({title:'温馨提示',msg:data.message,fn:function(){
                                        $('#dgRole').treegrid('reload');
                                    }});
                                }else{
                                    $.messager.alert('温馨提示',data.message);
                                }
                            },
                            complete:function(XMLHttpRequest, textStatus){
                                $.messager.progress('close');
                            },
                            error:function(){
                                $.messager.alert('温馨提示', '请求出错！');
                            }
                        });
                    }
                });
            }
            return false;
        }

        var toolbar = [{
            text:'新增角色',
            iconCls:'fa fa-arrows',
            handler:function(){
                addRoleInfo();
            }
        },'-',{
            text:'刷新角色',
            iconCls:'fa fa-repeat',
            handler:function(){
                $('#dgRole').treegrid('reload');
            }
        }];
    </script>
</div>
</body>
</html>