<!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">
                    <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
                    <!-- 表头按钮组 -->
                    <script type="text/html" id="headerToolBar">
                        <div class="layui-btn-group">
                            <button type="button" class="layui-btn layui-btn-sm" lay-event="saveEvent">
                                <i class="fa fa-lg fa-plus"></i> 新增
                            </button>
                            <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" lay-event="removeEvent">
                                <i class="fa fa-lg fa-trash"></i> 删除
                            </button>
                        </div>
                    </script>
                </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 gridId = top.restAjax.params(window.location.href).gridId;
        var resizeTimeout = null;
        var tableUrl = 'api/grid/user/listpage/{gridId}';

        // 初始化表格
        function initTable() {
            table.render({
                elem: '#dataTable',
                id: 'dataTable',
                url: top.restAjax.path(tableUrl, [gridId]),
                width: admin.screen() > 1 ? '100%' : '',
                height: $win.height() - 20,
                limit: 20,
                limits: [20, 40, 60, 80, 100, 200],
                toolbar: '#headerToolBar',
                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:'gmtCreate', width:180, title: '添加时间', align:'center',
                            templet: function(item) {
                                if(!item.gmtCreate) {
                                    return '-';
                                }
                                return item.gmtCreate;
                            }
                        }
                    ]
                ],
                page: true,
                parseData: function(data) {
                    return {
                        'code': 0,
                        'msg': '',
                        'count': data.total,
                        'data': data.rows
                    };
                }
            });
        }
        // 重载表格
        function reloadTable(currentPage) {
            table.reload('dataTable', {
                url: top.restAjax.path(tableUrl, [gridId]),
                where: {
                    keywords: $('#keywords').val(),
                },
                page: {
                    curr: currentPage
                },
            });
        }
        // 删除
        function removeData(ids) {
            top.dialog.msg(top.dataMessage.delete, {
                time: 0,
                btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                shade: 0.3,
                yes: function(index) {
                    top.dialog.close(index);
                    var layIndex;
                    top.restAjax.delete(top.restAjax.path('api/grid/user/delete/{gridId}/{ids}', [gridId, ids]), {}, null, function(code, data) {
                        top.dialog.msg(top.dataMessage.deleteSuccess);
                        var deleteUserIds = ids.split('_');
                        var selectedUserIds = $('#selectedUserIds').val().split('_');
                        var tempIds = common.resultIdsOfDeleteIds(deleteUserIds, selectedUserIds);
                        $('#selectedUserIds').val(tempIds);
                        reloadTable();
                    }, function(code, data) {
                        top.dialog.msg(data.msg);
                    }, function() {
                        layIndex = top.dialog.msg(top.dataMessage.deleting, {icon: 16, time: 0, shade: 0.3});
                    }, function() {
                        top.dialog.close(layIndex);
                    });
                }
            });
        }
        // 初始化职位用户ID列表
        function initSelectUserIds() {
            var layIndex;
            top.restAjax.get(top.restAjax.path('api/grid/user/list-user-id/{gridId}', [gridId]), {}, null, function(code, data) {
                var selectedUserIds = '';
                for(var i = 0, item; item = data[i++]; ) {
                    if('' != selectedUserIds) {
                        selectedUserIds += '_';
                    }
                    selectedUserIds += item;
                }
                $('#selectedUserIds').val(selectedUserIds);
                initTable();
            }, function(code, data) {
                top.dialog.msg(data.msg);
            }, function() {
                layIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
            }, function() {
                top.dialog.close(layIndex);
            });
        }
        initSelectUserIds();

        // 事件 - 页面变化
        $win.on('resize', function() {
            clearTimeout(resizeTimeout);
            resizeTimeout = setTimeout(function() {
                reloadTable();
            }, 500);
        });
        // 事件 - 搜索
        $(document).on('click', '#search', function() {
            reloadTable(1);
        });
        // 事件 - 增删
        table.on('toolbar(dataTable)', function(obj) {
            var layEvent = obj.event;
            var checkStatus = table.checkStatus('dataTable');
            var checkDatas = checkStatus.data;
            if(layEvent === 'saveEvent') {
                top.dialog.dialogData.selectedUserIds = $('#selectedUserIds').val();
                top.dialog.open({
                    url: top.restAjax.path('route/department/user/select-user', []),
                    title: '选择用户',
                    width: '500px',
                    height: '500px',
                    closeBtn: 0,
                    onClose: function() {
                        var selectedUsers = top.dialog.dialogData.selectedDepartmentUsers;
                        if(selectedUsers != null && selectedUsers.length > 0) {
                            var ids = [];
                            for (var i = 0, item; item = selectedUsers[i++];) {
                                ids.push(item.userId);
                            }
                            top.dialog.msg(top.dataMessage.update, {
                                time: 0,
                                btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                                yes: function (index) {
                                    top.dialog.close(index);
                                    var loadLayerIndex;
                                    top.restAjax.put(top.restAjax.path('api/grid/user/update/{gridId}', [gridId]), {
                                        ids: ids
                                    }, null, function (code, data) {
                                        top.dialog.msg(top.dataMessage.updated);
                                        initSelectUserIds();
                                    }, function (code, data) {
                                        top.dialog.msg(data.msg);
                                    }, function () {
                                        loadLayerIndex = top.dialog.msg(top.dataMessage.updating, {
                                            icon: 16,
                                            time: 0,
                                            shade: 0.3
                                        });
                                    }, function () {
                                        top.dialog.close(loadLayerIndex);
                                    });
                                },
                            });
                        }
                    }
                })
            } else if(layEvent === 'removeEvent') {
                if(checkDatas.length === 0) {
                    top.dialog.msg(top.dataMessage.table.selectDelete);
                } else {
                    var ids = '';
                    for(var i = 0, item; item = checkDatas[i++];) {
                        if(i > 1) {
                            ids += '_';
                        }
                        ids += item.userId;
                    }
                    removeData(ids);
                }
            }
        });
    });
</script>
</body>
</html>