<!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>
                            <label>赛事标题:</label>
                            <input type="text" name="competitionTitle" data-template="请输入活动标题"/>
                        </li>
                        <li>
                            <label>赛事类型:</label>
                            <select name="competitionType" th:with="type=${@dict.getType('competition_type')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>场地:</label>
                            <input type="text" name="competitionPlace" template="请输入场地"/>
                        </li>
                        <li>
                            <label>赛事状态:</label>
                            <select name="competitionState" th:with="type=${@dict.getType('competition_state')}">
                                <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="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.addToSet()" shiro:hasPermission="system:competition:add">
                <i class="fa fa-plus"></i> 新增
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var publisFlag = [[${@permission.hasPermi('system:competition:publish')}]]
    var editFlag = [[${@permission.hasPermi('system:competition:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:competition:remove')}]];
    var competitionTypeDatas = [[${@dict.getType('competition_type')}]];
    var competitionStateDatas = [[${@dict.getType('competition_state')}]];
    var prefix = ctx + "competition";

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            publishUrl: prefix + "/publish",
            exportUrl: prefix + "/export",
            modalName: "赛事",
            columns: [
                /* {
                     checkbox: false
                 },*/
                {
                    field: 'id',
                    title: 'id',
                    visible: false
                },
                {
                    field: 'competitionTitle',
                    title: '赛事标题'
                },
                {
                    field: 'competitionType',
                    title: '赛事类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(competitionTypeDatas, value);
                    }
                },
                {
                    field: 'teamMax',
                    title: '最大人数/团队数'
                },
                {
                    field: 'competitionPlace',
                    title: '场地'
                },
                {
                    field: 'applyStartTime',
                    title: '赛事开始时间'
                },
                {
                    field: 'applyEndTime',
                    title: '赛事结束时间'
                },
                {
                    field: 'applyBeforeTime',
                    title: '取消报名截至时间',
                    formatter: function (value, row, index) {
                        return "赛事开始前" + value + "小时";
                    }
                },
                {
                    field: 'competitionExpense',
                    title: '报名费用'
                }, {
                    field: 'viewingTicket',
                    title: '观看费用'
                },
                {
                    field: 'competitionState',
                    title: '赛事状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(competitionStateDatas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.competitionState === 'competiton_state_1') {
                            actions.push('<a class="btn btn-success btn-xs ' + publisFlag + '" href="javascript:void(0)" onclick="publish(\'' + row.id + '\', \'' + row.competitionTitle + '\', \'' + row.applyStartTime + '\', \'' + row.applyBeforeTime + '\', \'' + row.applyEndTime + '\')"><i class="fa fa-edit"></i>发布</a> ')
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editToSet(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        } else if (row.competitionState === 'competiton_state_4') {

                        } else {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="getTakePartList(\'' + row.id + '\')"><i class="fa fa-edit"></i>参赛名单</a> ')
                        }

                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function getTakePartList(id) {
        $.modal.open("参赛人员名单", prefix + "/openDetails/" + id);
    }

    function publish(id, dataName, applyStartTime, applyBeforeTime, applyEndTime) {
        table.set();
        $.modal.confirm("确定发布该条" + dataName + "信息吗？<br/>发布后赛事信息无法修改!", function () {
            var url = $.common.isEmpty(id) ? table.options.publishUrl : table.options.publishUrl.replace("{id}", id);
            if (table.options.type == table_type.bootstrapTreeTable) {
                $.operate.get(url);
            } else {
                // 将 applyStartTime 转换为 Date 对象
                var startTime = new Date(applyStartTime);
                // 将 applyBeforeTime 转换为毫秒（假设 applyBeforeTime 的单位是小时）
                var beforeTimeMs = applyBeforeTime * 60 * 60 * 1000; // 2小时转换为毫秒
                // 计算允许发布的时间点
                var allowPublishTime = new Date(startTime.getTime() - beforeTimeMs);
                // 获取当前时间
                var currentTime = new Date();
                if (startTime <= currentTime) {
                    $.modal.alertError("赛事开始时间早于当前时间, 请先修改赛事开始时间!")
                } else if (allowPublishTime <= currentTime) {
                    $.modal.alertError("取消报名时间早于当前时间, 请先修改取消报名时间!")
                } else if (allowPublishTime > currentTime) {
                    var data = {
                        "id": id,
                        "applyStartTime": formatApplyStartTime(startTime),
                        "allowPublishTime": (startTime.getTime() - beforeTimeMs) / 1000,
                        "applyEndTime": formatApplyStartTime(new Date(applyEndTime))
                    };
                    $.operate.submit(url, "post", "json", data);
                }
            }
        });
    }


    // 自定义格式化函数
    function formatApplyStartTime(date) {
        var year = date.getFullYear();
        var month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份从0开始
        var day = ('0' + date.getDate()).slice(-2);
        var hours = ('0' + date.getHours()).slice(-2);
        var minutes = ('0' + date.getMinutes()).slice(-2);
        var seconds = ('0' + date.getSeconds()).slice(-2);

        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }

</script>
</body>
</html>