<!doctype html>
<html lang="en" 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">
</head>
<body>
<div class="layui-anim layui-anim-fadein">
    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="test-table-reload-btn">
                        <div class="layui-inline">
                            <input type="text" id="keywords" class="layui-input search-item" placeholder="输入关键字">
                        </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="confirm" class="layui-btn layui-btn-normal layui-btn-sm" style="float: right;">确定</button>
                    </div>
                </div>
                <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
            </div>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index',  'table', 'laydate', 'common'], function() {
        var $ = layui.$;
        var $win = $(window);
        var table = layui.table;
        var admin = layui.admin;
        var common = layui.common;
        var selectType = top.restAjax.params(window.location.href).selectType;
        selectType = selectType ? selectType : 'radio'
        var tableUrl = 'api/user/listpageusersimple';
        var oldSelectedUserList = top.dialog.dialogData.oldSelectedUserList;
        // 清空上次选择
        top.dialog.dialogData.newSelectedUserList = [];
        var newSelectedUserList = [];
        var tableData = [];

        // 初始化选择列表
        function initNewSelectedUserList() {
            for(var i = 0, item; item = oldSelectedUserList[i++];) {
                newSelectedUserList.push(item);
            }
        }
        initNewSelectedUserList();

        // 初始化表格
        function initTable() {
            $.extend(table, {config: {checkName: 'checked'}});
            table.render({
                elem: '#dataTable',
                id: 'dataTable',
                url: top.restAjax.path(tableUrl, []),
                width: admin.screen() > 1 ? '100%' : '',
                height: $win.height() - 50,
                limit: 20,
                limits: [20, 40, 60, 80, 100, 200],
                request: {
                    pageName: 'page',
                    limitName: 'rows'
                },
                cols: [
                    [
                        {type: selectType, fixed: 'left'},
                        {field:'rowNum', width:80, title: '序号', fixed: 'left', align:'center', templet: '<span>{{d.LAY_INDEX}}</span>'},
                        {field: 'userUsername', width: 150, title: '用户名', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'userName', width: 150, title: '昵称', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'userPhone', width: 220, title: '用户手机', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'userEmail', width: 220, title: '用户邮箱', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        }
                    ]
                ],
                page: true,
                parseData: function(data) {
                    for(var i = 0, item; item = data.rows[i++];) {
                        if(!newSelectedUserList) {
                            item.checked = false;
                            continue;
                        }
                        var isSelected = false;
                        for(var j = 0, jItem; jItem = newSelectedUserList[j++];) {
                            if(item.userId === jItem.userId) {
                                isSelected = true;
                                break;
                            }
                        }
                        if(isSelected) {
                            item.checked = true;
                        } else {
                            item.checked = false;
                        }
                    }
                    tableData = data.rows;

                    return {
                        'code': 0,
                        'msg': '',
                        'count': data.total,
                        'data': data.rows
                    };
                }
            });
        }

        // 重载表格
        function reloadTable(currentPage) {
            table.reload('dataTable', {
                url: top.restAjax.path(tableUrl, []),
                where: {
                    keywords: $('#keywords').val(),
                },
                page: {
                    curr: currentPage
                },
                height: $win.height() - 50,
            });
        }
        initTable();

        function isUserSelected(userId) {
            for(var i = 0, item; item = newSelectedUserList[i++];) {
                if(userId == item.userId) {
                    return true;
                }
            }
            return false;
        }

        function addUserSelected(userItem) {
            if(isUserSelected(userItem.userId)) {
                return;
            }
            newSelectedUserList.push({
                userId: userItem.userId,
                userUsername: userItem.userUsername,
                userName: userItem.userName,
                userPhone: userItem.userPhone,
                userEmail: userItem.userEmail
            })
        }

        function removeUserSelected(userItem) {
            for(var i = 0, item; item = newSelectedUserList[i++];) {
                if(userItem.userId == item.userId) {
                    newSelectedUserList.splice(--i, 1);
                    i--;
                    return;
                }
            }
        }

        table.on('checkbox(dataTable)', function(obj) {
            if(obj.type === 'all') {
                if(obj.checked) {
                    // 添加全部
                    for(var i = 0, item; item = tableData[i++];) {
                        addUserSelected(item);
                    }
                } else {
                    // 删除全部
                    for(var i = 0, item; item = tableData[i++];) {
                        removeUserSelected(item);
                    }
                }
            } else {
                if(obj.checked) {
                    addUserSelected(obj.data);
                } else {
                    removeUserSelected(obj.data);
                }
            }
        });

        table.on('radio(dataTable)', function(obj) {
            newSelectedUserList.splice(0, newSelectedUserList.length);
            addUserSelected(obj.data);
        });
        $(document).on('click', '#search', function() {
            reloadTable(1);
        });
        $(document).on('click', '#confirm', function() {
            top.dialog.dialogData.newSelectedUserList = newSelectedUserList;
            top.dialog.closeBox();
        });
    });
</script>
</body>
</html>