<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.withdrawOrder.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/withdrawOrder',
})

const { t } = useI18n()
const tableRef = useTemplateRef('tableRef')
const optButtons: OptButton[] = defaultOptButtons(['edit'])
const editBtn = optButtons.find((b) => b.name === 'edit')
if (editBtn) {
    editBtn.display = (row: TableRow) => Number(row.status) !== 0
}
optButtons.push({
    render: 'tipButton',
    name: 'review',
    title: 'order.withdrawOrder.review_btn',
    text: '',
    type: 'warning',
    icon: 'fa fa-check-square-o',
    display: (row: TableRow) => Number(row.status) === 0,
    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 n = Number(cellValue)
    if (!Number.isFinite(n)) {
        return String(cellValue)
    }
    return n.toFixed(2)
}

const baTable = new baTableClass(
    new baTableApi('/admin/order.WithdrawOrder/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('order.withdrawOrder.id'), prop: 'id', align: 'center', width: 80, operator: 'RANGE', sortable: 'custom' },
            {
                label: t('order.withdrawOrder.order_no'),
                prop: 'order_no',
                align: 'center',
                minWidth: 170,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
            },
            // { label: t('order.withdrawOrder.user_id'), prop: 'user_id', align: 'center', width: 90, operator: 'RANGE' },
            {
                label: t('order.withdrawOrder.user_username'),
                prop: 'user.username',
                align: 'center',
                minWidth: 120,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'tags',
            },
            {
                label: t('order.withdrawOrder.channel_name'),
                prop: 'channel.name',
                align: 'center',
                minWidth: 110,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'tags',
            },
            { label: t('order.withdrawOrder.amount'), prop: 'amount', align: 'center', minWidth: 110, operator: 'RANGE', formatter: formatAmount },
            { label: t('order.withdrawOrder.fee'), prop: 'fee', align: 'center', minWidth: 110, operator: 'RANGE', formatter: formatAmount },
            { label: t('order.withdrawOrder.actual_amount'), prop: 'actual_amount', align: 'center', minWidth: 110, operator: 'RANGE', formatter: formatAmount },
            {
                label: t('order.withdrawOrder.pay_channel'),
                prop: 'pay_channel',
                align: 'center',
                minWidth: 110,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
            },
            {
                label: t('order.withdrawOrder.receive_type'),
                prop: 'receive_type',
                align: 'center',
                minWidth: 120,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
            },
            {
                label: t('order.withdrawOrder.receive_account'),
                prop: 'receive_account',
                align: 'center',
                minWidth: 180,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
            },
            {
                label: t('order.withdrawOrder.receiver_name'),
                prop: 'ddpay_receiver_name',
                align: 'center',
                minWidth: 120,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
            },
            {
                label: t('order.withdrawOrder.receiver_email'),
                prop: 'receiver_email',
                align: 'center',
                minWidth: 180,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
            },
            {
                label: t('order.withdrawOrder.receiver_mobile'),
                prop: 'receiver_mobile',
                align: 'center',
                minWidth: 140,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
            },
            {
                label: t('order.withdrawOrder.idempotency_key'),
                prop: 'idempotency_key',
                align: 'center',
                minWidth: 170,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
                show: false,
            },
            {
                label: t('order.withdrawOrder.status'),
                prop: 'status',
                align: 'center',
                width: 100,
                operator: 'eq',
                render: 'tag',
                effect: 'dark',
                custom: {
                    '0': 'info',
                    '1': 'success',
                    '2': 'danger',
                    '3': 'success',
                },
                replaceValue: {
                    '0': t('order.withdrawOrder.status 0'),
                    '1': t('order.withdrawOrder.status 1'),
                    '2': t('order.withdrawOrder.status 2'),
                    '3': t('order.withdrawOrder.status 3'),
                },
            },
            {
                label: t('order.withdrawOrder.review_admin_username'),
                prop: 'reviewAdmin.username',
                align: 'center',
                minWidth: 100,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'tags',
            },
            {
                label: t('order.withdrawOrder.review_time'),
                prop: 'review_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                sortable: 'custom',
                width: 170,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('order.withdrawOrder.remark'),
                prop: 'remark',
                align: 'center',
                minWidth: 150,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
            },
            {
                label: t('order.withdrawOrder.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.withdrawOrder.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: 120, render: 'buttons', buttons: optButtons, operator: false, fixed: 'right' },
        ],
    },
    {
        defaultItems: { status: 0, amount: '0.00', fee: '0.00', actual_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>
