<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('order.betOrder.quick Search Fields') })"
        ></TableHeader>

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

<script setup lang="ts">
import { onMounted, provide, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import { baTableApi } from '/@/api/common'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'

defineOptions({
    name: 'order/betOrder',
})

const { t } = useI18n()
const tableRef = useTemplateRef('tableRef')

function formatPickNumbers(_row: anyObj, _column: any, cellValue: unknown) {
    if (cellValue === null || cellValue === undefined) {
        return '-'
    }
    if (typeof cellValue === 'string') {
        return cellValue
    }
    try {
        return JSON.stringify(cellValue)
    } catch {
        return String(cellValue)
    }
}

function formatAmount(_row: anyObj, _column: any, cellValue: unknown) {
    if (cellValue === null || cellValue === undefined || cellValue === '') {
        return '-'
    }
    const s = String(cellValue).trim().replace(',', '.')
    const n = parseFloat(s)
    if (!Number.isFinite(n)) {
        return String(cellValue)
    }
    return n.toFixed(2)
}

const baTable = new baTableClass(
    new baTableApi('/admin/order.BetOrder/'),
    {
        pk: 'id',
        column: [
            { label: t('order.betOrder.id'), prop: 'id', align: 'center', width: 100, operator: 'RANGE', sortable: 'custom' },
            {
                label: t('order.betOrder.period_id'),
                prop: 'period_id',
                align: 'center',
                show: false,
                width: 100,
                operator: 'RANGE',
            },
            // {
            //     label: t('order.betOrder.period_no'),
            //     prop: 'period_no',
            //     align: 'center',
            //     minWidth: 160,
            //     operatorPlaceholder: t('Fuzzy query'),
            //     operator: 'LIKE',
            // },
            {
                label: t('order.betOrder.gameRecord_period_no'),
                prop: 'gameRecord.period_no',
                align: 'center',
                minWidth: 200,
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                render: 'tag',
            },
            {
                label: t('order.betOrder.gameRecord_status'),
                prop: 'gameRecord.status',
                align: 'center',
                width: 100,
                operator: 'eq',
                render: 'tag',
                effect: 'dark',
                custom: {
                    '0': 'success',
                    '1': 'warning',
                    '2': 'info',
                    '3': 'primary',
                    '4': 'warning',
                    '5': 'danger',
                },
                replaceValue: {
                    '0': t('order.betOrder.gameRecord_status 0'),
                    '1': t('order.betOrder.gameRecord_status 1'),
                    '2': t('order.betOrder.gameRecord_status 2'),
                    '3': t('order.betOrder.gameRecord_status 3'),
                    '4': t('order.betOrder.gameRecord_status 4'),
                    '5': t('order.betOrder.gameRecord_status 5'),
                },
            },
            {
                label: t('order.betOrder.user_id'),
                prop: 'user_id',
                align: 'center',
                show: false,
                width: 90,
                operator: 'RANGE',
            },
            {
                label: t('order.betOrder.user_username'),
                prop: 'user.username',
                align: 'center',
                minWidth: 120,
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                render: 'tags',
            },
            {
                label: t('order.betOrder.channel_name'),
                prop: 'channel.name',
                align: 'center',
                minWidth: 100,
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                render: 'tags',
            },
            {
                label: t('order.betOrder.pick_numbers'),
                prop: 'pick_numbers',
                align: 'center',
                minWidth: 120,
                operator: false,
                formatter: formatPickNumbers,
            },
            {
                label: t('order.betOrder.total_amount'),
                prop: 'total_amount',
                align: 'center',
                minWidth: 110,
                operator: 'RANGE',
                formatter: formatAmount,
            },
            { label: t('order.betOrder.streak_at_bet'), prop: 'streak_at_bet', align: 'center', width: 110, operator: 'RANGE' },
            {
                label: t('order.betOrder.is_auto'),
                prop: 'is_auto',
                align: 'center',
                width: 90,
                operator: 'eq',
                render: 'tag',
                custom: {
                    '0': 'info',
                    '1': 'primary',
                },
                replaceValue: {
                    '0': t('order.betOrder.is_auto 0'),
                    '1': t('order.betOrder.is_auto 1'),
                },
            },
            {
                label: t('order.betOrder.win_amount'),
                prop: 'win_amount',
                align: 'center',
                minWidth: 110,
                operator: 'RANGE',
                formatter: formatAmount,
            },
            {
                label: t('order.betOrder.jackpot_extra_amount'),
                prop: 'jackpot_extra_amount',
                align: 'center',
                minWidth: 120,
                operator: 'RANGE',
                formatter: formatAmount,
            },
            {
                label: t('order.betOrder.status'),
                prop: 'status',
                align: 'center',
                width: 100,
                operator: 'eq',
                render: 'tag',
                effect: 'dark',
                custom: {
                    '1': 'warning',
                    '2': 'success',
                    '3': 'danger',
                },
                replaceValue: {
                    '1': t('order.betOrder.status 1'),
                    '2': t('order.betOrder.status 2'),
                    '3': t('order.betOrder.status 3'),
                },
            },
            {
                label: t('order.betOrder.idempotency_key'),
                prop: 'idempotency_key',
                align: 'center',
                minWidth: 140,
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                showOverflowTooltip: true,
            },
            {
                label: t('order.betOrder.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('order.betOrder.update_time'),
                prop: 'update_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                sortable: 'custom',
                width: 170,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {}
)

provide('baTable', baTable)

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

<style scoped lang="scss"></style>
