<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.depositOrder.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: 'order/depositOrder',
})

const { t } = useI18n()
const tableRef = useTemplateRef('tableRef')
const optButtons: OptButton[] = defaultOptButtons(['edit'])
const depositEditBtn = optButtons.find((b) => b.name === 'edit')
if (depositEditBtn) {
    depositEditBtn.display = (row: TableRow) => Number(row.status) !== 3
}
optButtons.push({
    render: 'tipButton',
    name: 'depositReview',
    title: 'order.depositOrder.review_btn',
    text: '',
    type: 'warning',
    icon: 'fa fa-check-square-o',
    display: (row: TableRow) => Number(row.status) === 3,
    click: (row: TableRow) => {
        baTable.toggleForm('Edit', [row[baTable.table.pk!]])
    },
})

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.DepositOrder/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('order.depositOrder.id'), prop: 'id', align: 'center', width: 80, operator: 'RANGE', sortable: 'custom' },
            {
                label: t('order.depositOrder.order_no'),
                prop: 'order_no',
                align: 'center',
                minWidth: 170,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
            },
            {
                label: t('order.depositOrder.user_username'),
                prop: 'user.username',
                align: 'center',
                minWidth: 120,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'tags',
            },
            {
                label: t('order.depositOrder.channel_name'),
                prop: 'channel.name',
                align: 'center',
                minWidth: 110,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'tags',
            },
            {
                label: t('order.depositOrder.amount'),
                prop: 'amount',
                align: 'center',
                minWidth: 110,
                operator: 'RANGE',
                formatter: formatAmount,
            },
            {
                label: t('order.depositOrder.bonus_amount'),
                prop: 'bonus_amount',
                align: 'center',
                minWidth: 110,
                operator: 'RANGE',
                formatter: formatAmount,
            },
            {
                label: t('order.depositOrder.status'),
                prop: 'status',
                align: 'center',
                width: 100,
                operator: 'eq',
                render: 'tag',
                effect: 'dark',
                custom: {
                    '0': 'info',
                    '1': 'success',
                    '2': 'danger',
                    '3': 'warning',
                },
                replaceValue: {
                    '0': t('order.depositOrder.status 0'),
                    '1': t('order.depositOrder.status 1'),
                    '2': t('order.depositOrder.status 2'),
                    '3': t('order.depositOrder.status 3'),
                },
            },
            {
                label: t('order.depositOrder.pay_channel'),
                prop: 'pay_channel',
                align: 'center',
                minWidth: 130,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
            },
            {
                label: t('order.depositOrder.deposit_tier_id'),
                prop: 'deposit_tier_id',
                align: 'center',
                minWidth: 120,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                show: false,
            },
            {
                label: t('order.depositOrder.pay_time'),
                prop: 'pay_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                sortable: 'custom',
                width: 170,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('order.depositOrder.idempotency_key'),
                prop: 'idempotency_key',
                align: 'center',
                minWidth: 170,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
                show: false,
            },
            {
                label: t('order.depositOrder.remark'),
                prop: 'remark',
                align: 'center',
                minWidth: 150,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
            },
            {
                label: t('order.depositOrder.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.depositOrder.update_time'),
                prop: 'update_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                sortable: 'custom',
                width: 170,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
                show: false,
            },
            {
                label: t('Operate'),
                align: 'center',
                width: 120,
                render: 'buttons',
                buttons: optButtons,
                operator: false,
                fixed: 'right',
            },
        ],
    },
    {
        defaultItems: { status: 0, amount: '0.00', bonus_amount: '0.00' },
    }
)

provide('baTable', baTable)

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

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