<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <TableHeader
            :buttons="['refresh', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('game.record.quick Search Fields') })"
        >
        </TableHeader>

        <Table ref="tableRef"></Table>

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

<script setup lang="ts">
import { onMounted, provide, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import { routePush } from '/@/utils/router'
defineOptions({
    name: 'game/record',
})

const { t } = useI18n()
const tableRef = useTemplateRef('tableRef')
const optButtons: OptButton[] = defaultOptButtons(['edit'])

const formatCoin = (_row: any, _column: any, cellValue: number | string | null | undefined) => {
    if (cellValue === null || cellValue === undefined || cellValue === '') return '-'
    const n = Number(cellValue)
    if (Number.isNaN(n)) return '-'
    return n.toFixed(2)
}

optButtons.unshift({
    render: 'tipButton',
    name: 'view-play-record',
    title: 'game.record.view_play_records',
    text: '',
    type: 'success',
    icon: 'fa fa-list',
    class: 'table-row-view-play-record',
    disabledTip: false,
    click: (row: any) => {
        const pidRaw = row?.id
        const pid = typeof pidRaw === 'number' ? pidRaw : Number(pidRaw)
        if (!Number.isFinite(pid) || pid <= 0) {
            ElMessage.error(t('Parameter error'))
            return
        }
        void routePush({
            path: '/admin/game/playRecord',
            query: { period_id: String(pid) },
        })
    },
})

const baTable = new baTableClass(
    new baTableApi('/admin/game.Record/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('game.record.id'), prop: 'id', align: 'center', width: 100, operator: 'RANGE', sortable: 'custom' },
            {
                label: t('game.record.period_no'),
                prop: 'period_no',
                align: 'center',
                minWidth: 180,
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
            },
            {
                label: t('game.record.period_start_at'),
                prop: 'period_start_at',
                align: 'center',
                width: 170,
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                sortable: 'custom',
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('game.record.status'),
                prop: 'status',
                align: 'center',
                width: 110,
                operator: 'eq',
                render: 'tag',
                effect: 'dark',
                custom: { '0': 'success', '1': 'warning', '2': 'info', '3': 'primary', '4': 'warning', '5': 'danger', '6': 'danger' },
                replaceValue: {
                    '0': t('game.record.status 0'),
                    '1': t('game.record.status 1'),
                    '2': t('game.record.status 2'),
                    '3': t('game.record.status 3'),
                    '4': t('game.record.status 4'),
                    '5': t('game.record.status 5'),
                    '6': t('game.record.status 6'),
                },
            },
            {
                label: t('game.record.draw_mode'),
                prop: 'draw_mode',
                align: 'center',
                width: 110,
                operator: 'eq',
                render: 'tag',
                custom: { '0': 'info', '1': 'warning' },
                replaceValue: { '0': t('game.record.draw_mode 0'), '1': t('game.record.draw_mode 1') },
            },
            { label: t('game.record.result_number'), prop: 'result_number', align: 'center', width: 100, operator: 'RANGE' },
            {
                label: t('game.record.platform_profit_amount'),
                prop: 'platform_profit_amount',
                align: 'center',
                minWidth: 130,
                operator: 'RANGE',
                sortable: false,
                formatter: formatCoin,
            },
            {
                label: t('game.record.winner_user_count'),
                prop: 'winner_user_count',
                align: 'center',
                width: 110,
                operator: 'RANGE',
                sortable: false,
            },
            {
                label: t('game.record.void_reason'),
                prop: 'void_reason',
                align: 'center',
                minWidth: 140,
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                showOverflowTooltip: true,
            },
            {
                label: t('game.record.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                sortable: 'custom',
                width: 170,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('game.record.update_time'),
                prop: 'update_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                sortable: 'custom',
                width: 170,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false, fixed: 'right' },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: { status: 0, draw_mode: 0, void_reason: '' },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getData()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>
<style scoped lang="scss"></style>
