<template>
    <section class="content">
        <div class="box box-primary">
            <div class="box-body">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1 id="itemTitle"></h1>
                </section>
                <div id="toolbar">
                    轮次: <input type="text" class="input-sm" name="roundId" id="roundId">
                    开始时间：<input type="text" name="startTime" id="startTime" class="input-sm">
                    结束时间：<input type="text" name="endTime" id="endTime" class="input-sm">
                    <button id="btnSearch" class="btn btn-default">
                        <i class="glyphicon glyphicon-search"></i>查询
                    </button>
                    <div class="col-sm-12">
                        当前库存 <span name="stock" id="stock" />钻石
                        <button id="editStock" class="btn btn-default">编辑库存</button>
                    </div>
                </div>
            </div>

            <!-- .content -->
            <div id="table"></div>
        </div>
    </section>

    <div class="modal fade" id="roundDetailModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="roundDetailModalTitle">单轮明细</h4>
                </div>
                <div class="modal-body">
                    <div id="roundDetailTable"></div>
                </div>
                <div class="modal-footer">
                    <h4 id="roundDetailModalFooter"></h4>
                    <button type="button" class="btn btn-primary" id="exportRoundDetail">导出</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>

            </div>
        </div>
    </div>


    <div class="modal fade" id="editStockModal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="editStockModalTitle">编辑库存</h4>
                </div>
                <div class="modal-body">
                    设置库存为 <input type="text" name="editedStock" id="editedStock" class="input-sm"> 钻石
                </div>
                <div class="modal-footer">
                    <h4 id="editStockModalFooter"></h4>
                    <button type="button" class="btn btn-primary" id="editStockModelBtn">保存</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>

            </div>
        </div>
    </div>
</template>

<script>
import TableHelper from '@/utils/bootstrap-table-helper';

export default {
    name: "ActCallBattleRecordAdminView",
    setup() {
        return {};
    },
    created() {
        this.$nextTick(function () {
            this.initData();
        });
    },
    methods: {
        initData() {
            $(function () {
                let roundDetailRoundId;
                getStock()
                $('#table').bootstrapTable('destroy');
                $('#table').bootstrapTable({
                    columns: [
                        { field: 'roundId', title: '轮次', align: 'center', width: '5%' },
                        {
                            field: 'startTime', title: '开始时间', align: 'center', width: '5%',
                            formatter: function (val, row, index) {
                                if (val) {
                                    var date = new Date(val);
                                    return date.format('yyyy-MM-dd hh:mm:ss');
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'endTime', title: '结束时间', align: 'center', width: '5%',
                            formatter: function (val, row, index) {
                                if (val) {
                                    var date = new Date(val);
                                    return date.format('yyyy-MM-dd hh:mm:ss');
                                } else {
                                    return '-';
                                }
                            }
                        },
                        { field: 'memberNum', title: '人数', align: 'center', width: '5%' },
                        { field: 'userInputPieceNum', title: '投入', align: 'center', width: '5%' },
                        { field: 'prizePieceNum', title: '平台价值', align: 'center', width: '5%' },
                        { field: 'ticket', title: '门票', align: 'center', width: '5%' },
                        { field: 'prizeName', title: '召唤物结果', align: 'center', width: '5%' },
                        { field: 'restraintName', title: '克制物结果', align: 'center', width: '5%' },
                        {
                            field: 'roundId',
                            title: '操作',
                            align: 'center',
                            width: '10%',
                            formatter: function (val, row, index) {
                                const roundId = row.roundId
                                const startTime = new Date(row.startTime).format('yyyy-MM-dd hh:mm:ss')
                                const endTime = new Date(row.endTime).format('yyyy-MM-dd hh:mm:ss')
                                const prizeName = row.prizeName
                                const memberNum = row.memberNum
                                const userInputPieceNum = row.userInputPieceNum
                                const prizePieceNum = row.prizePieceNum
                                const ticket = row.ticket
                                return '<button id="btnDetail" name="btnDetail" class="btn btn-sm btn-success opt-info" data-roundId=' + roundId +
                                    ' startTime=' + startTime + ' endTime=' + endTime + ' prizeName=' + prizeName + ' memberNum=' + memberNum + ' userInputPieceNum=' + userInputPieceNum +
                                    ' prizePieceNum=' + prizePieceNum + ' ticket=' + ticket
                                    + '>' +
                                    '详情</button>';
                            }
                        }
                    ],
                    undefinedText: 0,
                    cache: false,
                    striped: true,
                    showRefresh: false,
                    pageSize: 20,
                    pagination: true,
                    pageList: [20, 50, 100, 200, 300, 500],
                    search: false,
                    sidePagination: "server", //表示服务端请求
                    queryParamsType: "undefined",
                    queryParams: function queryParams(params) {   //设置查询参数
                        var param = {
                            page: params.pageNumber,
                            pageSize: params.pageSize,
                            startTime: $('#startTime').val(),
                            endTime: $('#endTime').val(),
                            roundId: $('#roundId').val()
                        };
                        return param;
                    },
                    toolbar: '#toolbar',
                    url: '/admin/callBattle/listActRecord',
                    onLoadSuccess: function () {  //加载成功时执行
                        console.log("load success");
                    },
                    onLoadError: function () {  //加载失败时执行
                        console.log("load fail");
                    }
                });

                // 查询刷新
                $('#btnSearch').on('click', function () {
                    getStock();
                    TableHelper.doRefresh('#table');
                });

                var picker1 = $("#startTime").datetimepicker({
                    format: 'yyyy-mm-dd hh:ii:00',
                    autoclose: true
                })
                var picker2 = $('#endTime').datetimepicker({
                    format: 'yyyy-mm-dd hh:ii:00',
                    autoclose: true
                })

                // 获取轮次详情
                $('#table').on('click', '.opt-info', function () {
                    const roundId = $(this).attr("data-roundId");
                    roundDetailRoundId = roundId;
                    const startTime = $(this).attr("startTime");
                    const endTime = $(this).attr("endTime");
                    const prizeName = $(this).attr("prizeName");
                    const memberNum = $(this).attr("memberNum");
                    const userInputPieceNum = $(this).attr("userInputPieceNum");
                    const prizePieceNum = $(this).attr("prizePieceNum");
                    const ticket = $(this).attr("ticket");
                    console.log("roundId", roundId, startTime, endTime, prizeName, memberNum, userInputPieceNum, prizePieceNum, ticket)
                    $('#roundDetailModal #roundDetailModalTitle').text(`${roundId}轮 ${startTime} -- ${endTime} 结果：${prizeName} 明细`)
                    $('#roundDetailModal #roundDetailModalFooter').text(`共计${memberNum}人参与 投入${userInputPieceNum} 平台价值 ${prizePieceNum} 门票${ticket}`)
                    $('#roundDetailTable').bootstrapTable('destroy');
                    $('#roundDetailTable').bootstrapTable({
                        columns: [
                            { field: 'uid', title: 'uid', align: 'center', width: '5%' },
                            { field: 'erbanNo', title: '66号', align: 'center', width: '5%' },
                            { field: 'nick', title: '昵称', align: 'center', width: '5%' },
                            { field: 'costPieceNum', title: '投入', align: 'center', width: '5%', },
                            { field: 'prizePieceNum', title: '平台价值', align: 'center', width: '5%', }
                        ],
                        undefinedText: 0,
                        cache: false,
                        striped: true,
                        showRefresh: false,
                        pageSize: 20,
                        pagination: true,
                        pageList: [20, 50, 100, 200, 300, 500],
                        search: false,
                        sidePagination: "server", //表示服务端请求
                        queryParamsType: "undefined",
                        queryParams: function queryParams(params) {   //设置查询参数
                            var param = {
                                page: params.pageNumber,
                                pageSize: params.pageSize,
                                roundId: roundId
                            };
                            return param;
                        },
                        url: '/admin/callBattle/listRoundDrawDetail',
                        onLoadSuccess: function () {  //加载成功时执行
                            $("#roundDetailModal").modal('show');
                        },
                        onLoadError: function () {  //加载失败时执行
                            console.log("load fail");
                        }
                    });
                });

                // 导出轮次明细
                $("#exportRoundDetail").on("click", function () {
                    window.location.href = `/admin/callBattle/exportRoundDrawDetail?roundId=${roundDetailRoundId}`
                });

                // 获取库存
                function getStock() {
                    $.ajax({
                        type: 'get',
                        url: "/admin/callBattle/getStock",
                        success: function (json) {
                            if (json.success) {
                                console.log('data', json.data)
                                $("#stock").html(json.data)
                            } else {
                                $("#stock").html('')
                            }
                        }
                    });
                }

                $('#editStock').on('click', function () {
                    $("#editStockModal").modal('show');
                });


                // 编辑库存
                $('#editStockModelBtn').on('click', function () {
                    $.ajax({
                        type: 'get',
                        url: "/admin/callBattle/editStock",
                        data: {
                            stock: $("#editedStock").val(),
                        },
                        dataType: 'json',
                        success: function (json) {
                            if (json.success) {
                                $('#tipMsg').text('编辑成功');
                                $('#tipModal').modal('show');
                                $("#editStockModal").modal('hide');
                            } else {
                                $('#tipMsg').text('编辑失败，错误信息：' + json.message);
                                $('#tipModal').modal('show');
                            }
                        }
                    });
                });

            });
        }
    },

};
</script>

<style scoped></style>