<!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" style="margin-bottom: 10px;">
                        <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>
                    <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script type="text/javascript">
    layui.config({
        base: 'assets/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'table', 'laydate', 'ztree'], function() {
        var $ = layui.$;
        var $win = $(window);
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var oldSelectedFormList = top.dialog.dialogData.oldSelectedFormList ? top.dialog.dialogData.oldSelectedFormList : [];
        top.dialog.dialogData.newSelectedFormList = [];
        var newSelectedFormList = [];
        var tableData = [];

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

        // 初始化表格
        function initTable() {
            $.extend(table, {config: {checkName: 'checked'}});
            table.render({
                elem: '#dataTable',
                id: 'dataTable',
                url: top.restAjax.path('api/form/listpage-latest', []),
                width: admin.screen() > 1 ? '100%' : '',
                height: $win.height() - 60,
                limit: 20,
                limits: [20, 40, 60, 80, 100, 200],
                request: {
                    pageName: 'page',
                    limitName: 'rows'
                },
                cols: [
                    [
                        {type:'radio', fixed: 'left'},
                        {field:'rowNum', width:80, title: '序号', fixed: 'left', align:'center', templet: '<span>{{d.LAY_INDEX}}</span>'},
                        {field:'formId', width:200, title: '表单ID', align:'center',
                            templet: function(row) {
                                return row.formId;
                            }
                        },
                        {field:'formCode', width:200, title: '表单编码', align:'center',
                            templet: function(row) {
                                return row.formCode +' <span class="layui-badge layui-bg-blue">v:'+ row.formVersion +'</span>';
                            }
                        },
                        {field:'formName', width:160, title: '表单名称', align:'center',},
                        {field:'formSummary', width:160, title: '表单说明', align:'center',},
                        {field:'formType', width:100, title: '表单类型', align:'center',
                            templet: function(row) {
                                if(!row.formType) {
                                    return '-';
                                }
                                if(row.formType == 'default') {
                                    return '<span class="layui-badge layui-bg-green">默认</span>';
                                }
                                if(row.formType == 'oa') {
                                    return '<span class="layui-badge layui-bg-blue">OA表单</span>';
                                }
                                return '<span class="layui-badge">错误</span>';
                            }
                        }
                    ]
                ],
                page: true,
                parseData: function(data) {
                    for(var i = 0, item; item = data.rows[i++];) {
                        if(!newSelectedFormList) {
                            item.checked = false;
                            continue;
                        }
                        var isSelected = false;
                        for(var j = 0, jItem; jItem = newSelectedFormList[j++];) {
                            if(item.formId === jItem.formId) {
                                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() {
            table.reload('dataTable', {
                where: {
                    keywords: $('#keywords').val(),
                },
            });
        }
        // 初始化日期
        function initDate() {}
        initTable();
        initDate();

        // 事件 - 页面变化
        $win.on('resize', function() {
            reloadTable();
        });

        // 事件 - 搜索
        $(document).on('click', '#search', function() {
            reloadTable();
        });

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

        function addFormSelected(formItem) {
            if(isUserSelected(formItem.formId)) {
                return;
            }
            newSelectedFormList.push({
                formId: formItem.formId,
            })
        }

        function removeFormSelected(formItem) {
            for(var i = 0, item; item = newSelectedFormList[i++];) {
                if(formItem.formId == item.formId) {
                    newSelectedFormList.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++];) {
                        addFormSelected(item);
                    }
                } else {
                    // 删除全部
                    for(var i = 0, item; item = tableData[i++];) {
                        removeFormSelected(item);
                    }
                }
            } else {
                if(obj.checked) {
                    addFormSelected(obj.data);
                } else {
                    removeFormSelected(obj.data);
                }
            }
        });

        table.on('radio(dataTable)', function(obj) {
            newSelectedFormList.splice(0, newSelectedFormList.length);
            addFormSelected(obj.data);
        });

        $(document).on('click', '#confirm', function() {
            top.dialog.dialogData.newSelectedFormList = newSelectedFormList;
            top.dialog.closeBox();
        });
        
    });
</script>
</body>
</html>