<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<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>
                            <input type="hidden" name="busId" id="busId" th:value="${busId}"/>
                        </li>
                        <li>
                            <label>门票类型:</label>
                            <select name="orderType" id="orderType">
                                <option value="cansai" selected>报名门票</option>
                                <option value="menpiao">观看门票</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>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="closeCompetition()">
                                <i class="fa fa-search"></i>&nbsp;
                                关闭赛事
                            </a>
                        </li>
                    </ul>
                </div>
            </form>
        </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 paymentStatus = [[${@dict.getType('payment_status')}]];
    var prefix = ctx + "competition";

    $(function () {
        // 初始化表格
        var options = {
            url: prefix + "/getTakePartList",
            columns: getColumns()
        };
        $.table.init(options);

        // 监听下拉菜单的变化
        $('#orderType').change(function () {
            var selectedValue = $(this).val();
            // 根据选择的值重新加载表格
            $('#bootstrap-table').bootstrapTable('refresh', {
                columns: getColumns(selectedValue)
            });
        });
    });

    function getColumns(orderType) {
        orderType = orderType || $('#orderType').val(); // 默认值为当前选中的值

        var commonColumns = [
            {
                field: 'competitionName',
                title: '联系人'
            },
            {
                field: 'competitionMobile',
                title: '联系电话'
            },
            {
                field: 'registrationTime',
                title: '下单时间'
            },
            {
                field: 'cancellationTime',
                title: '取消时间'
            },
            {
                field: 'paymentTime',
                title: '支付时间'
            },
            {
                field: 'refundTime',
                title: '退款时间'
            },
            {
                field: 'paymentStatus',
                title: '支付状态',
                formatter: function (value, row, index) {
                    return $.table.selectDictLabel(paymentStatus, value);
                }
            },
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var actions = [];
                    if (row.paymentStatus === 'payment_status_have_paid') {
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="closeStadium(\'' + row.orderId + '\')"><i class="fa fa-close"></i>退款</a> ')
                    }
                    return actions.join('');
                }
            }
        ];

       /* if (orderType === 'cansai') {
            // 报名门票
            return [

            ].concat(commonColumns);
        } else if (orderType === 'menpiao') {
            // 观看门票
            return [
                {
                    field: 'username',
                    title: '联系人'
                }
            ].concat(commonColumns);
        } else {

        }*/

        return commonColumns;
    }

    function closeStadium(id) {
        $.ajax({
            url: prefix + "/close/" + id,
            type: "GET",
            success: function (res) {
                if (res.code === 0) {
                    $.modal.msgSuccess("操作成功");
                    $.table.refresh();
                } else {
                    $.modal.msgSuccess("未知异常! 请联系管理员处理! ")
                }
            },
            error: function (err) {
                $.modal.msgSuccess("系统发生了错误, 请及时联系管理员进行处理! ")
            }
        })
    }

    function closeCompetition() {
        var busId = $("#busId").val();
        $.ajax({
            url: ctx + "competition/close",
            type: "GET",
            data: {id: busId},
            success: function (res) {
                console.log(res)
                if (res.code === 0) {
                    var parent = activeWindow();
                    $.modal.close();
                    parent.$.modal.msgSuccess(res.msg);
                    parent.$.table.refresh();
                } else {
                    $.modal.alertWarning("还存在未退款的订单! 不可以关闭赛事! ");
                }
            },
            error: function (err) {
                $.modal.msgSuccess("系统发生了错误, 请及时联系管理员进行处理! ")
            }
        })
    }
</script>
</body>
</html>
