<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>权限管理</title>
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <style>
        .site-dropdown-demo .layui-menu li:hover{background-color: #333;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- 上方搜索栏 -->
        <fieldset class="table-search-fieldset">
            <legend>搜索</legend>
            <div class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <!-- 搜索【工作流名】和【权限字符】-->
                    <div class="layui-inline">
                        <label class="layui-form-label" id="demo8">权限名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="roleName" autocomplete="off" class="layui-input" placeholder="请输入权限名称"/>
                        </div>

                    </div>
                    <!-- 搜索按钮 -->
                    <div class="layui-inline">
                        <button type="button" class="layui-btn layui-btn-primary layui-border-green btn-role-action" data-type="search">
                            <i class="layui-icon layui-icon-search"></i> 搜 索
                        </button>
                        <button type="button" class="layui-btn layui-btn-primary layui-border-box btn-role-action" data-type="refresh">
                            <i class="layui-icon layui-icon-refresh"></i> 清 空
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>
        <!-- 表格 -->
        <table class="layui-hide" id="table-data" lay-filter="table-data"></table>
    </div>
</div>

<script type="text/html" id="toolbar-table">
    <div class="layui-btn-container">
        <button style="background-color: #154d96;" class="layui-btn layui-btn-sm layuimini-btn-primary" lay-event="btn-refresh"><i class="fa fa-refresh"></i></button>
        <button style="background-color: #154d96;" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="btn-add">
            <i class="fa fa-plus"></i> 添加权限
        </button>
    </div>
</script>

<!-- 【行内按钮】表格每行末尾的操作按钮-->
<script type="text/html" id="table-tool">
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="btn-edit">编辑</a>
    <a class="layui-btn layui-btn-xs " lay-event="role-showUser">授权用户</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="btn-delete">删除</a>
</script>
<!--引入JS文件-->
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/jquery-1.10.2.min.js" charset="utf-8"></script>
<script>
    layui.use(['table','dropdown', 'util', 'layer',], function () {
        let dropdown = layui.dropdown,
            util = layui.util,
            layer = layui.layer,
            $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        let currPage = 1
        let table_data = table.render({
            even: true,
            elem: '#table-data',
            url: '/cloud/role/list',
            response: {statusCode: 200},
            toolbar: '#toolbar-table',
            defaultToolbar: ['filter'],
            title: '权限信息',
            height: 'full-200',
            cols: [
                [
                    {field: 'roleName', title: '权限名称',align: 'center', fixed: 'left'},
                    {
                        field: 'roleUpload', title: '上传权限', align: 'center', width: 95, templet: function (d) {
                            let checkStr = "";
                            if (d.roleUpload === 1) {
                                checkStr += " checked "
                            }
                            return '<input type="checkbox" lay-filter="check-upload" value="' + d.id + '" lay-skin="switch" lay-text="正常|禁用" ' + checkStr + '/>'
                        }
                    },
                    {
                        field: 'roleDown', title: '下载权限', align: 'center', width: 95, templet: function (d) {
                            let checkStr = "";
                            if (d.roleDown === 1) {
                                checkStr += " checked "
                            }
                            return '<input type="checkbox" lay-filter="check-down" value="' + d.id + '" lay-skin="switch" lay-text="正常|禁用" ' + checkStr + '/>'
                        }
                    },
                    {
                        field: 'roleDelete', title: '删除权限', align: 'center', width: 95, templet: function (d) {
                            let checkStr = "";
                            if (d.roleDelete === 1) {
                                checkStr += " checked "
                            }
                            return '<input type="checkbox" lay-filter="check-delete" value="' + d.id + '" lay-skin="switch" lay-text="正常|禁用" ' + checkStr + '/>'
                        }
                    },

                    {field: 'creatorName', title: '创建人',align: 'center'},
                    {field: 'createTime', title: '创建时间',align: 'center'},
                    {fixed: 'right', title: '操作', align: 'center', toolbar: '#table-tool', width: 200}
                ]
            ],
            page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip', 'refresh'], // 自定义分页布局
                curr: 1, // 初始当前页
                limits: [30, 50, 100,500,1000], // 自定义每页显示条数选择项
                limit: 30 // 默认每页显示条数
            },
            done: function (res, curr, count) {
                if (res.data.length === 0 && curr > 1) {
                    table_data.reload({page: {curr: curr - 1}})
                }
                currPage = curr
            }
        });


        let active = {
            search: function () {
                table_data.reload({
                    page: {curr: 1},
                    where: {
                        roleName: $('#roleName').val()
                    }
                });
            },
            refresh: function () {
                table_data.reload({
                    page: {curr: 1},
                    where: {roleName: ""}
                });
                $('#roleName').val('')
            }
        };
        $('.btn-role-action').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        table.on('toolbar(table-data)', function (obj) {
            let that = this;
            switch (obj.event) {
                // 【刷新】按钮
                case "btn-refresh":
                    table_data.reload()
                    break;
                case "btn-add":
                    layer.open({
                        title: '添加信息',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: false,
                        area: ['1000px', '700px'],
                        btn: ["确定", "取消"],
                        content: '/cloud/role/add',
                        yes: function(index, layero){
                            let body = layer.getChildFrame('body',index);

                            let roleName = body.find("input[name='roleName']").val();
                            if (roleName.length <= 0) {
                                layer.msg('请输入权限名称！');
                                return;
                            }
                            let ids = body.find("input[name='ids']").val();
                            if (ids.length <= 0) {
                                layer.msg('请选择授权用户！');
                                return;
                            }
                            let dataSet = {
                                "roleName" : roleName,
                                "ids":ids
                            }

                            $.post("/cloud/role/addSave", dataSet, function (data) {
                                if (data.code === 200) {
                                    layer.open({
                                        type: 0,
                                        title: data.msg,
                                        closeBtn: 0,
                                        btn: ["继续添加", "关闭"],
                                        content: '是否继续添加？',
                                        btn1: function (index) {
                                            layer.close(index)
                                        },
                                        btn2: function (index2) {
                                            layer.close(index2);
                                            layer.close(index);
                                            table_data.reload();
                                        }
                                    });
                                } else {
                                    layer.msg(data.msg, {icon: 2});
                                }
                            });
                        }
                        ,btn2: function(index, layero){

                        },
                    });
                    break;
            }
        });

        table.on('tool(table-data)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            switch (layEvent) {
                // 【编辑】按钮响应事件
                case "btn-edit":
                    layer.open({
                        title: '编辑信息',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: false,
                        area: ['1000px', '700px'],
                        content: '/cloud/role/edit/' + data.id,
                        btn: ["确定", "取消"],
                        yes: function(index, layero){
                            let body = layer.getChildFrame('body',index);

                            let roleName = body.find("input[name='roleName']").val();
                            if (roleName.length <= 0) {
                                layer.msg('请输入权限名称！');
                                return;
                            }



                            let ids = body.find("input[name='ids']").val();
                            if (ids.length <= 0) {
                                layer.msg('请选择授权用户！');
                                return;
                            }

                            let dataSet = {
                                "id" : data.id,
                                "roleName" : roleName,
                                "ids":ids
                            }
                            let index_loading = layer.load(1, {
                                shade: [0.2, '#fff'] //0.1透明度的白色背景
                            });
                            $.post("/cloud/role/editSave", dataSet, function (data) {
                                if (data.code === 200) {
                                    layer.close(index_loading)
                                    layer.msg(data.msg, {time: 3000, icon: 1});
                                    table_data.reload();
                                    layer.close(index);

                                } else {
                                    layer.msg(data.msg, {icon: 2});
                                }
                            });
                        }
                        ,btn2: function(index, layero){

                        }
                    });
                    break;
                // 【删除】按钮响应事件
                case "btn-delete":
                    layer.confirm('确认要删除 <b>' + data.roleName + ' </b>吗？', {
                        title: false,
                        btn: ['确定', '取消'],
                        closeBtn: 0,        // 不显示右上角的关闭按钮
                        // 按钮【确定】的回调
                        btn1: function (index) {
                            // 加载层
                            let index_loading = layer.load(1, {
                                shade: [0.2, '#fff'] //0.1透明度的白色背景
                            });
                            $.post("/cloud/role/delete", {id: data.id}, function (data2) {
                                // 关闭加载层
                                layer.close(index_loading)
                                if (data2.code === 200) {
                                    table_data.reload()
                                    layer.msg(data2.msg, {time: 3000, icon: 1});
                                } else {
                                    layer.msg(data2.msg, {time: 3000, icon: 2});
                                }
                            });
                        },
                    });
                    break;
                case "role-showUser":
                    let user_list_index = layer.open({
                        title: '角色已授权用户列表',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: false,
                        area: ['700px', '500px'],
                        content: '/cloud/role/goShowUser/' + data.id
                    });
                    layer.full(user_list_index);
                    break;
            }
        });

        form.on('switch(check-upload)', function (data) {
            // 注意：isChecked是操作之后的值
            let isChecked = data.elem.checked;
            let actionText = isChecked ? "开启" : "禁用"
            let newStatus = isChecked ? "1" : "0"
            let ids = data.value;
            layer.confirm('确认要 ' + actionText + '上传权限？', {
                title: false,
                btn: ['确定', '取消'],
                closeBtn: 0,        // 不显示右上角的关闭（X）按钮
                // 按钮【确定】的回调
                btn1: function (index) {
                    $.post("/cloud/role/changeUploadStatus/" + newStatus, {ids: ids}, function (d) {
                        if (d.code === 200) {
                            layer.msg(d.msg, {time: 3000, icon: 1});
                            data.elem.checked = isChecked;
                            form.render();
                        } else {
                            layer.msg(d.msg, {time: 3000, icon: 2});
                            data.elem.checked = !isChecked;
                            form.render();
                        }
                    });
                },
                // 按钮【取消】的回调
                btn2: function (index) {
                    data.elem.checked = !isChecked;
                    form.render();
                    layer.close(index);
                }
            });
        });

        form.on('switch(check-down)', function (data) {
            // 注意：isChecked是操作之后的值
            let isChecked = data.elem.checked;
            let actionText = isChecked ? "开启" : "禁用"
            let newStatus = isChecked ? "1" : "0"
            let ids = data.value;
            layer.confirm('确认要 ' + actionText + '上传权限？', {
                title: false,
                btn: ['确定', '取消'],
                closeBtn: 0,        // 不显示右上角的关闭（X）按钮
                // 按钮【确定】的回调
                btn1: function (index) {
                    $.post("/cloud/role/changeDownStatus/" + newStatus, {ids: ids}, function (d) {
                        if (d.code === 200) {
                            layer.msg(d.msg, {time: 3000, icon: 1});
                            data.elem.checked = isChecked;
                            form.render();
                        } else {
                            layer.msg(d.msg, {time: 3000, icon: 2});
                            data.elem.checked = !isChecked;
                            form.render();
                        }
                    });
                },
                // 按钮【取消】的回调
                btn2: function (index) {
                    data.elem.checked = !isChecked;
                    form.render();
                    layer.close(index);
                }
            });
        });

        form.on('switch(check-delete)', function (data) {
            // 注意：isChecked是操作之后的值
            let isChecked = data.elem.checked;
            let actionText = isChecked ? "开启" : "禁用"
            let newStatus = isChecked ? "1" : "0"
            let ids = data.value;
            layer.confirm('确认要 ' + actionText + '下载权限？', {
                title: false,
                btn: ['确定', '取消'],
                closeBtn: 0,        // 不显示右上角的关闭（X）按钮
                // 按钮【确定】的回调
                btn1: function (index) {
                    $.post("/cloud/role/changeDeleteStatus/" + newStatus, {ids: ids}, function (d) {
                        if (d.code === 200) {
                            layer.msg(d.msg, {time: 3000, icon: 1});
                            data.elem.checked = isChecked;
                            form.render();
                        } else {
                            layer.msg(d.msg, {time: 3000, icon: 2});
                            data.elem.checked = !isChecked;
                            form.render();
                        }
                    });
                },
                // 按钮【取消】的回调
                btn2: function (index) {
                    data.elem.checked = !isChecked;
                    form.render();
                    layer.close(index);
                }
            });
        });
    });

</script>
</body>
</html>
