<!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">
    <link rel="stylesheet" type="text/css" href="assets/js/vendor/viewer/viewer.min.css">
    <style>
        .list-img-box .list-img{width: 28px; height: 28px; object-fit: cover; margin: 2px;}
        .list-img-box .list-img{width: 28px; height: 28px; object-fit: cover; margin: 2px;}
    </style>
</head>
<body>
<div th:class="${isFromMenu ? 'layui-fluid layui-anim layui-anim-fadein' : '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-sm" lay-event="save" th:if="${formType eq 'default'}">
                                <i class="fa fa-lg fa-plus"></i> 新增
                            </button>
                            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="update" th:if="${formType eq 'default'}">
                                <i class="fa fa-lg fa-edit"></i> 编辑
                            </button>
                            <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove" th:if="${formType eq 'default'}">
                                <i class="fa fa-lg fa-trash"></i> 删除
                            </button>
                        </div>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="assets/js/vendor/viewer/viewer.min.js"></script>
<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', 'common'], function() {
        var $ = layui.$;
        var $win = $(window);
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var common = layui.common;
        var formCode = [[${formCode}]];
        var formVersion = [[${formVersion}]];
        var formType = [[${formType}]];
        var showFields = [[${showFields}]];
        var isFromMenu = [[${isFromMenu}]]

        // 初始化表格
        function initTable() {
            var cols = [
                {type:'checkbox', fixed: 'left'},
                {field:'rowNum', width:80, title: '序号', fixed: 'left', align:'center', templet: '<span>{{d.LAY_INDEX}}</span>'},
                {field:'MAIN_TITLE', width:200, title: '主标题', align:'center',
                    templet: function(row) {
                        var rowData = row[this.field];
                        if(!rowData) {
                            return '-';
                        }
                        return rowData;
                    }
                },
            ];
            for(var i = 0, showField; showField = showFields[i++];) {
                if(showField.fieldTag === 'uploadImage') {
                    cols.push(
                        {
                            field: showField.fieldName,
                            width: 200,
                            title: showField.fieldExplain,
                            align:'center',
                            templet: function(row) {
                                var fieldName = this.field;
                                var rowData = row[fieldName];
                                if(!rowData) {
                                    return '-';
                                }
                                var downloadFile = '<div id="'+fieldName +'ListImg" class="list-img-box">';
                                var datas = rowData.split(',');
                                for(var i = 0, item = datas[i]; item = datas[i++];) {
                                    downloadFile += '<img class="list-img" src="route/file/download/true/'+ item +'"/>'
                                }
                                downloadFile += '</div>';
                                setTimeout(function() {
                                    new Viewer(document.getElementById(fieldName +'ListImg'), {navbar: true});
                                }, 50);
                                return downloadFile;
                            }
                        }
                    )
                } else if(showField.fieldTag === 'uploadFile') {
                    cols.push(
                        {
                            field: showField.fieldName,
                            width: 300,
                            title: showField.fieldExplain,
                            align:'center',
                            templet: function(row) {
                                var fieldName = this.field;
                                var rowData = row[fieldName];
                                if(!rowData) {
                                    return '-';
                                }
                                var downloadFile = '<div class="list-file-box">';
                                var datas = rowData.split(',');
                                for(var i = 0, item = datas[i]; item = datas[i++];) {
                                    var fileNameArray = item.split(':');
                                    downloadFile += '['+ i +'.<a class="list-file" href="route/file/download/false/'+ fileNameArray[0] +'" target="_blank">'+ fileNameArray[1] +'</a>] '
                                }
                                downloadFile += '</div>';
                                return downloadFile;
                            }
                        }
                    )
                } else if(showField.fieldTag === 'uploadVideo') {
                    cols.push(
                        {
                            field: showField.fieldName,
                            width: 300,
                            title: showField.fieldExplain,
                            align:'center',
                            templet: function(row) {
                                var fieldName = this.field;
                                var rowData = row[fieldName];
                                if(!rowData) {
                                    return '-';
                                }
                                var downloadFile = '<div class="list-file-box">';
                                var datas = rowData.split(',');
                                for(var i = 0, item = datas[i]; item = datas[i++];) {
                                    var fileNameArray = item.split(':');
                                    downloadFile += '['+ i +'.<a class="list-file" href="javascript:void(0);" lay-event="showVideoEvent" data-file-id="'+ fileNameArray[0] +'">'+ fileNameArray[1] +'</a>] '
                                }
                                downloadFile += '</div>';
                                return downloadFile;
                            }
                        }
                    )
                } else if(showField.fieldTag === 'uploadAudio') {
                    cols.push(
                        {
                            field: showField.fieldName,
                            width: 300,
                            title: showField.fieldExplain,
                            align:'center',
                            templet: function(row) {
                                var fieldName = this.field;
                                var rowData = row[fieldName];
                                if(!rowData) {
                                    return '-';
                                }
                                var downloadFile = '<div class="list-file-box">';
                                var datas = rowData.split(',');
                                for(var i = 0, item = datas[i]; item = datas[i++];) {
                                    var fileNameArray = item.split(':');
                                    downloadFile += '['+ i +'.<a class="list-file" href="javascript:void(0);" lay-event="showAudioEvent" data-file-id="'+ fileNameArray[0] +'">'+ fileNameArray[1] +'</a>] '
                                }
                                downloadFile += '</div>';
                                return downloadFile;
                            }
                        }
                    )
                } else {
                    cols.push(
                        {
                            field: showField.fieldName,
                            width: 180,
                            title: showField.fieldExplain,
                            align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(!rowData) {
                                    return '-';
                                }
                                return rowData;
                            }
                        }
                    )
                }
            }
            cols.push(
                {field:'creator', width: 200, title: '创建人', align:'center',
                    templet: function(row) {
                        var rowData = row[this.field];
                        if(!rowData) {
                            return '-';
                        }
                        return rowData;
                    }
                },
                {field:'gmt_create', width: 180, title: '创建时间', align:'center',
                    templet: function(row) {
                        var rowData = row[this.field];
                        if(!rowData) {
                            return '-';
                        }
                        return rowData;
                    }
                },
                {field:'modifier', width: 200, title: '修改人', align:'center',
                    templet: function(row) {
                        var rowData = row[this.field];
                        if(!rowData) {
                            return '-';
                        }
                        return rowData;
                    }
                },
                {field:'gmt_modified', width: 180, title: '修改时间', align:'center',
                    templet: function(row) {
                        var rowData = row[this.field];
                        if(!rowData) {
                            return '-';
                        }
                        return rowData;
                    }
                },
            )
            table.render({
                elem: '#dataTable',
                id: 'dataTable',
                url: top.restAjax.path('api/form-report/listpage-all-fields/code/{formCode}/version/{formVersion}', [formCode, formVersion]),
                width: admin.screen() > 1 ? '100%' : '',
                height: $win.height() - (isFromMenu ? 50 : 22),
                limit: 20,
                limits: [20, 40, 60, 80, 100, 200],
                toolbar: '#headerToolBar',
                request: {
                    pageName: 'page',
                    limitName: 'rows'
                },
                cols: [cols],
                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-report/remove/code/{formCode}/version/{formVersion}/uids/{uids}', [formCode, formVersion, 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();
        });
        // 事件 - 搜索
        $(document).on('click', '#search', 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-report/save/code/{formCode}/version/{formVersion}', [formCode, formVersion]),
                    width: isFromMenu ? '75%' : '60%',
                    height: '85%',
                    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-report/update/code/{formCode}/version/{formVersion}?uid={uid}', [formCode, formVersion, checkDatas[0].uid]),
                        width: isFromMenu ? '75%' : '60%',
                        height: '85%',
                        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.uid;
                    }
                    removeData(ids);
                }
            }
        });

        table.on('tool(dataTable)', function(obj) {
            var data = obj.data;
            var event = obj.event;
            if(event === 'showVideoEvent') {
                layer.open({
                    type: 1,
                    title: false,
                    area: ['320px', '240px'],
                    closeBtn: 0,
                    shadeClose: true,
                    content: '<video width="320" height="240" controls><source src="route/file/download/true/'+ this.dataset.fileId +'" type="video/mp4"></video>'
                });
            } else if(event === 'showAudioEvent') {
                layer.open({
                    type: 1,
                    title: false,
                    area: ['300px', '100px'],
                    closeBtn: 0,
                    shadeClose: true,
                    content: '<audio width="300" height="100" controls><source src="route/file/download/true/'+ this.dataset.fileId +'" type="audio/mpeg"></audio>'
                });
            }
        });
    });
</script>
</body>
</html>