<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'}">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
    <style>
        html,body{height:100%;}
        #customUserBox {height: 155px; border: 1px solid #e6e6e6;text-align:center;overflow: auto;display: none;cursor: pointer;}
    </style>
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein" style="height:100%;padding: 0;overflow: hidden;">
    <div class="layui-card" style="height:100%;">
        <div class="layui-card-body" style="height:100%;padding: 15px;">
            <form class="layui-form" lay-filter="dataForm">
                <div class="layui-form-item">
                    <select name="roleDataRight" lay-filter="roleDataRight">
                        <option value="all">全部</option>
                        <option value="department">部门</option>
                        <option value="custom">自定义</option>
                        <option value="self" selected>个人</option>
                    </select>
                </div>
                <div id="customUserBox"></div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button type="button" class="layui-btn" lay-submit lay-filter="submitForm">提交</button>
                            <button type="button" class="layui-btn layui-btn-primary close">返回</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate'], function(){
        var $ = layui.$;
        var form = layui.form;
        var roleId = top.restAjax.params(window.location.href).roleId;
        var roleDataRightUserIds = [];
        var roleUsers = [];
        function closeBox() {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }
        // 初始化角色人员
        function initCustomRoleUsers(selectedUsers) {
            roleDataRightUserIds.splice(0, roleDataRightUserIds.length);
            roleUsers.splice(0, roleUsers.length);
            $('#customUserBox').empty();
            for (var i = 0, item; item = selectedUsers[i++];) {
                roleDataRightUserIds.push(item.userId);
                roleUsers.push({
                    userName: item.userName,
                    userTitle: item.userUsername
                });
            }
        }
        // 初始化
        function initCustomData(ids) {
            $('#customUserBox').show();
            if(!ids || ids.length == 0) {
                return;
            }
            var loadLayerIndex;
            top.restAjax.post(top.restAjax.path('api/user/listbyids', []), {
                ids: ids
            }, null, function(code, data) {
                initCustomRoleUsers(data);
                setCustomRoleUsers();
            }, function(code, data) {
                top.dialog.msg(data.msg);
            }, function() {
                loadLayerIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
            }, function() {
                top.dialog.close(loadLayerIndex);
            });
        }
        // 初始化
        function initData() {
            var loadLayerIndex;
            top.restAjax.get(top.restAjax.path('api/role/data-right/get/{roleId}', [roleId]), {}, null, function(code, data) {
                form.val('dataForm', {
                    roleDataRight: data.roleDataRight
                });
                form.render(null, 'dataForm');
                if(data.roleDataRight === 'custom') {
                    initCustomData(data.userIds);
                }
            }, function(code, data) {
                top.dialog.msg(data.msg);
            }, function() {
                loadLayerIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
            }, function() {
                top.dialog.close(loadLayerIndex);
            });
        }
        // 设置自定义角色用户
        function setCustomRoleUsers() {
            for (var i = 0, item = roleUsers[i]; item = roleUsers[i++];) {
                $('#customUserBox').append('<span class="layui-badge layui-bg-green" title="' + item.userTitle + '" style="margin: 0 1px;">' + item.userName + '</span>')
            }
        }
        // 打开自定义角色用户
        function openCustomRoleUsers() {
            $('#customUserBox').show();
            console.log(roleDataRightUserIds)
            top.dialog.dialogData.selectedUserIds = roleDataRightUserIds.toString().replaceAll(',', '\_');
            top.dialog.open({
                url: top.restAjax.path('route/department/user/select-user', []),
                title: '选择人员',
                width: '500px',
                height: '500px',
                onClose: function() {
                    var selectedUsers = top.dialog.dialogData.selectedDepartmentUsers;
                    if(selectedUsers != null && selectedUsers.length > 0) {
                        $('#customUserBox').empty();
                        initCustomRoleUsers(selectedUsers);
                        setCustomRoleUsers();
                    } else {
                        roleDataRightUserIds.splice(0, roleDataRightUserIds.length);
                        roleUsers.splice(0, roleUsers.length);
                        $('#customUserBox').empty();
                    }
                }
            });
        }
        initData();

        // 提交表单
        form.on('submit(submitForm)', function(formData) {
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var userIds = [];
                var loadLayerIndex;
                for(var i = 0, item = roleDataRightUserIds[i]; item = roleDataRightUserIds[i++];) {
                    userIds.push(item)
                }
                top.restAjax.put(top.restAjax.path('api/role/data-right/update/{roleId}', [roleId]), {
                    roleDataRight: formData.field.roleDataRight,
                    userIds: userIds
                }, null, function(code, data) {
                    top.dialog.msg(top.dataMessage.commitSuccess, {
                        time: 0,
                        btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                        shade: 0.3,
                        yes: function(index) {
                            top.dialog.close(index);
                        },
                        btn2: function() {
                            closeBox();
                        }
                    });
                }, function(code, data) {
                    top.dialog.msg(data.msg);
                }, function() {
                    loadLayerIndex = top.dialog.msg(top.dataMessage.committing, {icon: 16, time: 0, shade: 0.3});
                }, function() {
                    top.dialog.close(loadLayerIndex);
                });
            });
            return false;
        });
        form.on('select(roleDataRight)', function(data) {
            var roleDataRight = data.value;
            if(roleDataRight === 'custom') {
                openCustomRoleUsers();
            } else {
                $('#customUserBox').hide();
            }
        });
        $(document).on('click', '#customUserBox', function() {
            openCustomRoleUsers();
        });
        $('.close').on('click', function() {
            closeBox();
        });
    });
</script>
</body>
</html>