<!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">
                    <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-danger layui-btn-sm" lay-event="remove">
                                <i class="fa fa-lg fa-trash"></i> 删除
                            </button>
                        </div>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script type="text/javascript" th:inline="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 hasMenuService = [[${hasMenuService}]];
        var queryParams = top.restAjax.params(window.location.href);
        var formId = queryParams.formId;

        // 初始化表格
        function initTable() {
            table.render({
                elem: '#dataTable',
                id: 'dataTable',
                url: top.restAjax.path('api/form/listpage-history/form-id/{formId}', [formId]),
                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: '序号', 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>';
                            }
                        },
                        {field:'formStatus', width:100, title: '表单状态', align:'center',
                            templet: function(row) {
                                var field = row.formStatus;
                                if(!field) {
                                    return '-';
                                }
                                var checked = '';
                                if(field === 'active') {
                                    checked = 'checked'
                                }
                                return '<input type="checkbox" lay-skin="switch" lay-text="激活|未激活" '+ checked +' lay-filter="formStatusFilter" data-form-id="'+ row.formId +'">';
                            }
                        },
                        {field:'formTableName', width:200, title: '表名', align:'center', },
                        {field:'pcPageCode', width:220, title: 'PC端代码', align:'center',
                            templet: function(item) {
                                return '<div class="layui-btn-group">' +
                                    '<button class="layui-btn layui-btn-xs" lay-event="savePageCodeEvent">新增页面</button>' +
                                    '<button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="updatePageCodeEvent">修改页面</button>' +
                                    '<button class="layui-btn layui-btn-xs layui-btn-primary" lay-event="showPageCodeEvent">详情页面</button>' +
                                    '</div>';
                            }
                        },

                        {field:'printPageCode', width:100, title: 'OA打印', align:'center',
                            templet: function(item) {
                                if(item.formType === 'default') {
                                    return '-';
                                }
                                return '<div class="layui-btn-group">' +
                                    '<button class="layui-btn layui-btn-xs" lay-event="printPageCodeEvent">打印页面</button>' +
                                    '</div>';
                            }
                        },
                        {field:'appPageCode', width:220, title: 'APP端代码', align:'center',
                            templet: function(item) {
                                return '<div class="layui-btn-group">' +
                                    '<button class="layui-btn layui-btn-xs" lay-event="appSavePageCodeEvent">新增页面</button>' +
                                    '<button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="appUpdatePageCodeEvent">修改页面</button>' +
                                    '<button class="layui-btn layui-btn-xs layui-btn-primary" lay-event="appShowPageCodeEvent">详情页面</button>' +
                                    '</div>';
                            }
                        },
                        {field:'formSourceData', width:100, title: '原数据', align:'center',
                            templet: function(item) {
                                return '<button class="layui-btn layui-btn-xs" lay-event="showDataEvent">查看数据</button>';
                            }
                        },
                        {field:'opition', width: hasMenuService ? 220 : 150, title: '操作', fixed:'right', align:'center',
                            templet: function(item) {
                                var btnGroup = '<div class="layui-btn-group">';
                                    btnGroup += '<button type="button" class="layui-btn layui-btn-xs" lay-event="dataListEvent">数据列表</button>';
                                    btnGroup += '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="deleteEvent" title="该操作将连同物理表（无数据）一并删除">物理删除</button>';
                                    if(hasMenuService && item.formType === 'default') {
                                        btnGroup += '<button type="button" class="layui-btn layui-btn-xs layui-btn-primary" lay-event="joinMenuEvent" title="将该表单加入菜单">加入菜单</button>';
                                    }
                                    btnGroup += '</div>';
                                return btnGroup;
                            }
                        }
                    ]
                ],
                page: true,
                parseData: function(data) {
                    return {
                        'code': 0,
                        'msg': '',
                        'count': data.total,
                        'data': data.rows
                    };
                }
            });
        }
        // 重载表格
        function reloadTable() {
            table.reload('dataTable', {
                where: {
                    keywords: $('#keywords').val(),
                },
            });
        }
        // 初始化日期
        function initDate() {}
        // 删除
        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/form/remove/{ids}', [ids]), {}, null, function (code, data) {
                        top.dialog.msg(top.dataMessage.deleteSuccess, {time: 1000});
                        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);
                    });
                }
            });
        }
        initTable();
        initDate();

        // 事件 - 页面变化
        $win.on('resize', function() {
            reloadTable();
        });
        // 事件 - 增删改
        table.on('toolbar(dataTable)', function(obj) {
            var layEvent = obj.event;
            var checkStatus = table.checkStatus('dataTable');
            var checkDatas = checkStatus.data;
            if(layEvent === 'save') {
                top.dialog.open({
                    title: '表单设计器',
                    url: top.restAjax.path('route/form-design/save', []),
                    width: '99%',
                    height: '99%',
                    onClose: function () {
                        reloadTable();
                    }
                })
            } else if(layEvent === 'update') {
                if(checkDatas.length === 0) {
                    top.dialog.msg(top.dataMessage.table.selectEdit);
                } else if(checkDatas.length > 1) {
                    top.dialog.msg(top.dataMessage.table.selectOneEdit);
                } else {
                    top.dialog.open({
                        title: '表单设计器',
                        url: top.restAjax.path('route/form-design/update?formId={formId}', [checkDatas[0].formId]),
                        width: '99%',
                        height: '99%',
                        onClose: function () {
                            reloadTable();
                        }
                    })
                }
            } else if(layEvent === 'remove') {
                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.formId;
                    }
                    removeData(ids);
                }
            }
        });

        table.on('tool(dataTable)', function(obj) {
            var event = obj.event;
            var data = obj.data;
            if(event === 'savePageCodeEvent') {
                top.dialog.open({
                    title: '新增页面模板',
                    url: top.restAjax.path('route/form/get-save-page-code?formId={formId}&formCode={formCode}&formVersion={formVersion}', [data.formId, data.formCode, data.formVersion]),
                    width: '99%',
                    height: '99%',
                    onClose: function () {
                    }
                })
            } else if(event === 'updatePageCodeEvent') {
                top.dialog.open({
                    title: '修改页面模板',
                    url: top.restAjax.path('route/form/get-update-page-code?formId={formId}&formCode={formCode}&formVersion={formVersion}', [data.formId, data.formCode, data.formVersion]),
                    width: '99%',
                    height: '99%',
                    onClose: function () {
                    }
                })
            } else if(event === 'showPageCodeEvent') {
                top.dialog.open({
                    title: '详情页面模板',
                    url: top.restAjax.path('route/form/get-show-page-code?formId={formId}&formCode={formCode}&formVersion={formVersion}', [data.formId, data.formCode, data.formVersion]),
                    width: '99%',
                    height: '99%',
                    onClose: function () {
                    }
                })
            } else if(event === 'printPageCodeEvent') {
                top.dialog.open({
                    title: '打印页面模板【需要打印的内容添加到 startprint 与 endprint 之间，两个注释不能删除，变量用 ${XXX}占位（视频、音频、文件、附件只打印文件名称）】',
                    url: top.restAjax.path('route/form/get-print-page-code?formId={formId}&formCode={formCode}&formVersion={formVersion}', [data.formId, data.formCode, data.formVersion]),
                    width: '99%',
                    height: '99%',
                    onClose: function () {
                    }
                })
            } else if(event === 'appSavePageCodeEvent') {
                top.dialog.open({
                    title: 'APP新增页面模板',
                    url: top.restAjax.path('route/form/get-app-save-page-code?formId={formId}&formCode={formCode}&formVersion={formVersion}', [data.formId, data.formCode, data.formVersion]),
                    width: '99%',
                    height: '99%',
                    onClose: function () {
                    }
                })
            } else if(event === 'appUpdatePageCodeEvent') {
                top.dialog.open({
                    title: 'APP修改页面模板',
                    url: top.restAjax.path('route/form/get-app-update-page-code?formId={formId}&formCode={formCode}&formVersion={formVersion}', [data.formId, data.formCode, data.formVersion]),
                    width: '99%',
                    height: '99%',
                    onClose: function () {
                    }
                })
            } else if(event === 'appShowPageCodeEvent') {
                top.dialog.open({
                    title: 'APP详情页面模板',
                    url: top.restAjax.path('route/form/get-app-show-page-code?formId={formId}&formCode={formCode}&formVersion={formVersion}', [data.formId, data.formCode, data.formVersion]),
                    width: '99%',
                    height: '99%',
                    onClose: function () {
                    }
                })
            } else if(event === 'showDataEvent') {
                top.dialog.open({
                    title: '表单数据',
                    url: top.restAjax.path('route/form/get-form-source-data?formId={formId}', [data.formId]),
                    width: '500px',
                    height: '90%',
                    onClose: function () {
                    }
                })
            } else if(event === 'showFormEvent') {
                top.dialog.open({
                    title: '预览',
                    url: top.restAjax.path('route/form-report/list?formCode={formCode}&formVersion={formVersion}', [data.formCode, data.formVersion]),
                    width: '80%',
                    height: '80%',
                    onClose: function () {
                    }
                })
            } else if(event === 'dataListEvent') {
                top.dialog.open({
                    title: '数据列表',
                    url: top.restAjax.path('route/form-report/list/code/{formCode}/version/{formVersion}', [data.formCode, data.formVersion]),
                    width: '70%',
                    height: '80%',
                    onClose: function () {
                    }
                })
            } else if(event === 'formFieldEvent') {

            } else if(event === 'deleteEvent') {
                top.dialog.confirm('该操作将连同物理表（无数据）一并删除，确定吗?', function(index) {
                    top.dialog.close(index);
                    var layIndex;
                    top.restAjax.delete(top.restAjax.path('api/form/delete/{id}', [data.formId]), {}, null, function (code, data) {
                        top.dialog.msg(top.dataMessage.deleteSuccess, {time: 1000});
                        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);
                    });
                });
            } else if(event === 'joinMenuEvent') {
                top.dialog.open({
                    title: '选择要添加的根节点',
                    width: '300px',
                    height: '400px',
                    url: top.restAjax.path('route/menu/list-tree-select', []),
                    onClose: function() {
                        var selectedNodes = top.dialog.dialogData.selectedNodes;
                        if(!selectedNodes || selectedNodes.length == 0) {
                            return;
                        }
                        var menuParentId = selectedNodes[0].id;
                        top.dialog.confirm('确定添加到该节点吗？', function(index) {
                            top.dialog.close(index);
                            var layIndex;
                            top.restAjax.put(top.restAjax.path('api/form/update-to-menu/form-id/{formId}/menu-parent-id/{parentMenuId}', [data.formId, menuParentId]), {}, null, function (code, data) {
                                top.dialog.msg('添加成功，刷新菜单查看');
                            }, function (code, data) {
                                top.dialog.msg(data.msg);
                            }, function () {
                                layIndex = top.dialog.msg('正在添加...', {icon: 16, time: 0, shade: 0.3});
                            }, function () {
                                top.dialog.close(layIndex);
                            });
                        });
                    }
                })
            }
        })

        form.on('switch(formStatusFilter)', function(obj) {
            var dataset = obj.elem.dataset;
            var formId = dataset.formId;
            var checked = obj.elem.checked;
            top.restAjax.put(top.restAjax.path('api/form/update-status/{formId}/{formStatus}', [formId, checked ? 'active' : 'inactive']), {}, null, function(code, data) {
                top.dialog.msg('修改成功');
            }, function(code, data) {
                top.dialog.msg(data.msg);
                obj.elem.checked = !checked;
                form.render('checkbox')
            })
        });
    });
</script>
</body>
</html>