<!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" type="text/css" href="assets/js/vendor/viewer/viewer.min.css">
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein" style="padding: 0;">
    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="test-table-reload-btn" style="margin-bottom: 10px;">
                        <div class="layui-inline">
                            <input type="text" id="keywords" class="layui-input search-item" placeholder="输入关键字">
                        </div>
                        <div class="layui-inline layui-form search-item">
                            <select id="userType" name="userType" lay-filter="typeFilter">
                                <option value="">选择类型</option>
                                <option value="1">系统用户</option>
                                <option value="2">普通用户</option>
                                <option value="3">公共用户</option>
                            </select>
                        </div>
                        <div class="layui-inline layui-form search-item">
                            <select id="userState" name="userState" lay-filter="typeFilter">
                                <option value="">选择类型</option>
                                <option value="0">正常</option>
                                <option value="1">锁定</option>
                            </select>
                        </div>
                        <button type="button" id="search" class="layui-btn layui-btn-sm">
                            <i class="fa fa-lg fa-search"></i> 搜索
                        </button>
                        <button type="button" id="saveBtn" class="layui-btn layui-btn-sm" style="float: right;">保存选择</button>
                    </div>
                    <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<input id="selectedUserIds" type="hidden"/>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'table', 'common'], function() {
        var $ = layui.$;
        var $win = $(window);
        var table = layui.table;
        var admin = layui.admin;
        var common = layui.common;
        var roleId = top.restAjax.params(window.location.href).roleId;
        var resizeTimeout = null;
        var tableUrl = 'api/role/user/listpage-user/exclude-role-id/{roleId}';
        var selectUserIdObj = {};

        // 初始化表格
        function initTable() {
            $.extend(table, {config: {checkName: 'checked'}});
            table.render({
                elem: '#dataTable',
                id: 'dataTable',
                url: top.restAjax.path(tableUrl, [roleId]),
                width: admin.screen() > 1 ? '100%' : '',
                height: $win.height() - 60,
                limit: 20,
                limits: [20, 40, 60, 80, 100, 200],
                toolbar: false,
                request: {
                    pageName: 'page',
                    limitName: 'rows'
                },
                cols: [
                    [
                        {type:'checkbox', fixed: 'left'},
                        {field:'rowNum', width:80, title: '序号', align:'center', templet: '<span>{{d.LAY_INDEX}}</span>'},
                        {field:'userUsername', width:140, title: '用户名', align:'center'},
                        {field:'userName', width:140, title: '昵称', align:'center'},
                        {field:'userPhone', width:140, title: '手机', align:'center',
                            templet: function(item) {
                                if(!item.userPhone) {
                                    return '-';
                                }
                                return item.userPhone;
                            }
                        },
                        {field:'userEmail', width: 160, title: '邮箱', align:'center',
                            templet: function(item) {
                                if(!item.userEmail) {
                                    return '-';
                                }
                                return item.userEmail;
                            }
                        },
                        {field:'userType', width:90, title: '类型', sort: true, align:'center',
                            templet: function(item) {
                                if(item.userType == 1) {
                                    return '<span class="layui-badge layui-bg-green">系统用户</span>';
                                } else if(item.userType == 2) {
                                    return '<span class="layui-badge layui-bg-orange">普通用户</span>';
                                } else if(item.userType == 3) {
                                    return '<span class="layui-badge layui-bg-gray">公共用户</span>';
                                }
                                return '<span class="layui-badge">类型错误</span>';
                            }
                        },
                        {field:'userState', width:80, title: '状态', sort: true, align:'center',
                            templet: function(item) {
                                var value;
                                switch (item.userState) {
                                    case 1:
                                        value = '<span class="layui-badge layui-bg-blue">锁定</span>';
                                        break;
                                    case -1:
                                        value = '<span class="layui-badge layui-bg-gray">未审核</span>';
                                        break;
                                    case -2:
                                        value = '<span class="layui-badge">审核不通过</span>';
                                        break;
                                    default:
                                        value = '<span class="layui-badge layui-bg-green">正常</span>';
                                }
                                return value;
                            }
                        },
                    ]
                ],
                page: true,
                parseData: function(data) {
                    for(var i = 0, item; item = data.rows[i++];) {
                        if(selectUserIdObj[item.userId]) {
                            item.checked = true;
                        } else {
                            item.checked = false;
                        }
                    }
                    return {
                        'code': 0,
                        'msg': '',
                        'count': data.total,
                        'data': data.rows
                    };
                }
            });
        }
        initTable();
        // 重载表格
        function reloadTable(currentPage) {
            table.reload('dataTable', {
                url: top.restAjax.path(tableUrl, [roleId]),
                where: {
                    keywords: $('#keywords').val(),
                    userType: $('#userType').val(),
                    userState: $('#userState').val()
                },
                page: {
                    curr: currentPage
                },
                height: $win.height() - 60,
            });
        }
        // 事件 - 页面变化
        $win.on('resize', function() {
            clearTimeout(resizeTimeout);
            resizeTimeout = setTimeout(function() {
                reloadTable();
            }, 500);
        });
        // 事件 - 搜索
        $(document).on('click', '#search', function() {
            reloadTable(1);
        });
        $(document).on('click', '#saveBtn', function() {
            var selectUserIdArray = [];
            for(var k in selectUserIdObj) {
                selectUserIdArray.push(k);
            }
            if(selectUserIdArray.length === 0) {
                top.dialog.msg('请选择用户');
                return;
            }
            top.dialog.confirm('确定保存吗？', function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                top.restAjax.post(top.restAjax.path('api/role/user/save/{roleId}', [roleId]), {
                    ids: selectUserIdArray
                }, null, function(code, data) {
                    top.dialog.msg('提交成功');
                    reloadTable(1);
                }, function(code, data) {
                    top.dialog.msg(data.msg);
                }, function() {
                    loadLayerIndex = top.dialog.msg('正在提交...', {icon: 16, time: 0, shade: 0.3});
                }, function() {
                    top.dialog.close(loadLayerIndex);
                });
            });
        })
        table.on('checkbox(dataTable)', function(obj) {
            if(obj.type === 'one') {
                if(obj.checked) {
                    selectUserIdObj[obj.data.userId] = obj.data;

                } else {
                    delete selectUserIdObj[obj.data.userId];
                }
            } else {
                var datas = table.cache.dataTable;
                if(obj.checked) {
                    for(var i = 0, item; item = datas[i++];) {
                        selectUserIdObj[item.userId] = item;
                    }
                } else {
                    for(var i = 0, item; item = datas[i++];) {
                        delete selectUserIdObj[item.userId];
                    }
                }
            }
        });
    });
</script>
</body>
</html>