<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', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('agent.settlementPeriod.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 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'

defineOptions({
    name: 'agent/settlementPeriod',
})

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

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

const baTable = new baTableClass(
    new baTableApi('/admin/agent.SettlementPeriod/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('agent.settlementPeriod.id'), prop: 'id', align: 'center', width: 80, operator: 'RANGE', sortable: 'custom' },
            {
                label: t('agent.settlementPeriod.settlement_no'),
                prop: 'settlement_no',
                align: 'center',
                minWidth: 160,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
            },
            {
                label: t('agent.settlementPeriod.period_start_at'),
                prop: 'period_start_at',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                width: 170,
                sortable: 'custom',
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('agent.settlementPeriod.period_end_at'),
                prop: 'period_end_at',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                width: 170,
                sortable: 'custom',
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('agent.settlementPeriod.total_bet_amount'),
                prop: 'total_bet_amount',
                align: 'center',
                operator: 'RANGE',
                minWidth: 120,
                formatter: formatAmount2,
            },
            {
                label: t('agent.settlementPeriod.total_payout_amount'),
                prop: 'total_payout_amount',
                align: 'center',
                operator: 'RANGE',
                minWidth: 120,
                formatter: formatAmount2,
            },
            {
                label: t('agent.settlementPeriod.platform_profit_amount'),
                prop: 'platform_profit_amount',
                align: 'center',
                operator: 'RANGE',
                minWidth: 120,
                formatter: formatAmount2,
            },
            {
                label: t('agent.settlementPeriod.status'),
                prop: 'status',
                align: 'center',
                width: 100,
                effect: 'dark',
                custom: { 0: 'info', 1: 'warning', 2: 'success', 3: 'danger' },
                operator: 'eq',
                render: 'tag',
                replaceValue: {
                    '0': t('agent.settlementPeriod.status 0'),
                    '1': t('agent.settlementPeriod.status 1'),
                    '2': t('agent.settlementPeriod.status 2'),
                    '3': t('agent.settlementPeriod.status 3'),
                },
            },
            {
                label: t('agent.settlementPeriod.remark'),
                prop: 'remark',
                align: 'center',
                minWidth: 160,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
            },
            {
                label: t('agent.settlementPeriod.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                width: 170,
                sortable: 'custom',
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('agent.settlementPeriod.update_time'),
                prop: 'update_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                width: 170,
                sortable: 'custom',
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            { label: t('Operate'), align: 'center', minWidth: 80, render: 'buttons', buttons: optButtons, operator: false, fixed: 'right' },
        ],
    },
    {
        defaultItems: { status: 0 },
    }
)

provide('baTable', baTable)

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

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