<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('报名活动列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            活动：<select name="activityName" th:with="type=${@dict.getType('sweet_form_activity')}">
                            <option value="">全部</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"></option>
                        </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-table table-bordered">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var exportFlag = [[${@permission.hasPermi('sweet:formSubmission:export')}]];

    var prefix = ctx + "sweet/formSubmission";

    function formatDateTime(value) {
        return $.common.sprintf("<span>%s</span>", null != value ? value.replace("T", " ").substring(0, 19) : value);
    }

    function exportAll(activityName) {
        doExport({activityName: activityName});
    }

    function exportByTime(activityName) {
        var html = '<div style="padding: 20px;">' +
            '<div class="form-group">' +
            '<label>开始时间：</label>' +
            '<input type="text" class="time-input form-control" id="exportBeginTime" placeholder="yyyy-MM-dd HH:mm:ss"/>' +
            '</div>' +
            '<div class="form-group">' +
            '<label>结束时间：</label>' +
            '<input type="text" class="time-input form-control" id="exportEndTime" placeholder="yyyy-MM-dd HH:mm:ss"/>' +
            '</div>' +
            '</div>';
        layer.open({
            type: 1,
            title: '按时间导出',
            area: ['420px', '260px'],
            content: html,
            btn: ['导出', '取消'],
            success: function () {
                layui.use('laydate', function () {
                    var laydate = layui.laydate;
                    laydate.render({elem: '#exportBeginTime', type: 'datetime', trigger: 'click'});
                    laydate.render({elem: '#exportEndTime', type: 'datetime', trigger: 'click'});
                });
            },
            yes: function (index) {
                var beginTime = $('#exportBeginTime').val();
                var endTime = $('#exportEndTime').val();
                if (!beginTime && !endTime) {
                    $.modal.msgWarning('请至少填写一个时间');
                    return;
                }
                layer.close(index);
                doExport({activityName: activityName, beginTime: beginTime, endTime: endTime});
            }
        });
    }

    function doExport(data) {
        $.modal.loading("正在导出数据，请稍后...");
        $.post(prefix + "/export", data, function (result) {
            $.modal.closeLoading();
            if (result.code == web_status.SUCCESS) {
                window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true;
            } else {
                $.modal.alertError(result.msg);
            }
        });
    }

    $(function () {
        var options = {
            url: prefix + "/list",
            modalName: "报名活动",
            sortable: false,
            columns: [
                {
                    field: 'activityName',
                    title: '活动标识(英文)'
                },
                {
                    field: 'activityLabel',
                    title: '活动名称(中文)',
                    formatter: function (value, row) {
                        return value || row.activityName;
                    }
                },
                {
                    field: 'submissionCount',
                    title: '报名数量'
                },
                {
                    field: 'firstCreatedAt',
                    title: '最早提交',
                    formatter: function (value) {
                        return formatDateTime(value);
                    }
                },
                {
                    field: 'lastCreatedAt',
                    title: '最新提交',
                    formatter: function (value) {
                        return formatDateTime(value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row) {
                        var actions = [];
                        actions.push('<a class="btn btn-warning btn-xs ' + exportFlag + '" href="javascript:void(0)" onclick="exportAll(\'' + row.activityName + '\')"><i class="fa fa-download"></i> 全部导出</a>');
                        actions.push('<a class="btn btn-info btn-xs ' + exportFlag + '" href="javascript:void(0)" onclick="exportByTime(\'' + row.activityName + '\')"><i class="fa fa-clock-o"></i> 按时间导出</a>');
                        return actions.join(' ');
                    }
                }
            ]
        };
        $.table.init(options);
    });
</script>
</body>
</html>
