<!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">
</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" style="margin-bottom: 10px;">
                        <div class="layui-inline">
                            <input type="text" id="keywords" class="layui-input search-item search-item-width-100" 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" class="layui-btn layui-btn-sm layui-btn-normal" id="confirm" style="float:right;">确定</button>
                        <!-- 确定按钮 -->
                    </div>
                    <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script src="assets/js/vendor/viewer/viewer.min.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 laydate = layui.laydate;
        var common = layui.common;
        var resizeTimeout = null;
        var selectType = top.restAjax.params(window.location.href).selectType;
        selectType = selectType ? selectType : 'radio';

        // 已经选择的列表
        var selectedIdArray = top.dialog.dialogData.selectedIdArray ? top.dialog.dialogData.selectedIdArray : [];
        // 将新的选择列表置空
        top.dialog.dialogData.newSelectedArray = null;
        // 正在选择
        var selectMap = {};
        var tableData = [];

        var tableUrl = 'api/doc-no/listpage';

        // 初始化已选择的列表
        function initSelectMap() {
            for(var i = 0, item; item = selectedIdArray[i++];) {
                selectMap[item] = {
                    docNoId: item
                }
            }
        }
        initSelectMap();

        // 初始化表格
        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() - 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: '序号', fixed: 'left', align:'center', templet: '<span>{{d.LAY_INDEX}}</span>'},
                        {field: 'title', width: 180, title: '标题', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'summary', width: 180, title: '描述', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'type', width: 100, title: '类型', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(rowData === 'always') {
                                    return '一直累加';
                                }
                                if(rowData === 'year') {
                                    return '按年累加';
                                }
                                if(rowData === 'month') {
                                    return '按月累加';
                                }
                                if(rowData === 'day') {
                                    return '按日累加';
                                }
                                if(rowData === 'manual') {
                                    return '手动';
                                }
                                return '错误';
                            }
                        },
                        {field: 'template', width: 180, title: '模板', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'startNu', width: 120, title: '开始编号', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'nuLength', width: 120, title: '编号长度', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'isActive', width: 100, title: '是否激活', align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(rowData === 0) {
                                    return '<span class="layui-badge">否</span>';
                                }
                                if(rowData === 1) {
                                    return '<span class="layui-badge layui-bg-green">是</span>';
                                }
                                return rowData;
                            }
                        },
                        {field: 'gmtCreate', width: 180, 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(selectMap[item.docNoId]) {
                            item.checked = true;
                            // 重新赋值：在打开界面后直接确定关闭页面时回显正常
                            selectMap[item.docNoId] = item;
                        } else {
                            item.checked = false;
                        }
                    }
                    tableData = data.rows;
                    // 默认勾选

                    return {
                        'code': 0,
                        'msg': '',
                        'count': data.total,
                        'data': data.rows
                    };
                }
            });
        }
        // 重载表格
        function reloadTable(currentPage) {
            table.reload('dataTable', {
                where: {
                    keywords: $('#keywords').val(),
                    startTime: $('#startTime').val(),
                    endTime: $('#endTime').val()
                },
                page: {
                    curr: currentPage
                },
            });
        }
        initTable();
        // 关闭
        function close() {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }

        /**
         * 清空选择
         */
        function clearSelect() {
            selectMap = {};
        }

        /**
         * 添加选择
         * @param item
         */
        function addSelect(item) {
            selectMap[item.docNoId] = item;
        }

        /**
         * 删除选择
         * @param item
         */
        function removeSelect(item) {
            delete selectMap[item.docNoId];
        }

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

        // 勾选事件
        table.on('radio(dataTable)', function(obj) {
            clearSelect()
            addSelect(obj.data);
        });

        // 事件 - 确定
        $(document).on('click', '#confirm', function() {
            top.dialog.dialogData.newSelectedArray = [];
            for(var key in selectMap) {
                top.dialog.dialogData.newSelectedArray.push(selectMap[key]);
            }
            close();
        });

        // 事件 - 搜索
        $(document).on('click', '#search', function() {
            reloadTable(1);
        });
    });
</script>
</body>
</html>