<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('user.userWalletRecord.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: 'user/userWalletRecord',
})

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

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 bizReplace = {
    deposit: t('user.userWalletRecord.biz deposit'),
    withdraw: t('user.userWalletRecord.biz withdraw'),
    withdraw_freeze: t('user.userWalletRecord.biz withdraw_freeze'),
    withdraw_unfreeze: t('user.userWalletRecord.biz withdraw_unfreeze'),
    platform_in: t('user.userWalletRecord.biz platform_in'),
    platform_out: t('user.userWalletRecord.biz platform_out'),
    admin_credit: t('user.userWalletRecord.biz admin_credit'),
    admin_deduct: t('user.userWalletRecord.biz admin_deduct'),
    bet: t('user.userWalletRecord.biz bet'),
    payout: t('user.userWalletRecord.biz payout'),
    fee: t('user.userWalletRecord.biz fee'),
    void_refund: t('user.userWalletRecord.biz void_refund'),
    adjust: t('user.userWalletRecord.biz adjust'),
}

const bizTypeTagCustom = {
    deposit: 'success',
    withdraw: 'warning',
    withdraw_freeze: 'info',
    withdraw_unfreeze: 'info',
    platform_in: 'success',
    platform_out: 'danger',
    admin_credit: 'success',
    admin_deduct: 'danger',
    bet: 'primary',
    payout: 'success',
    fee: 'warning',
    void_refund: 'info',
    adjust: 'warning',
}

const dirReplace = {
    '1': t('user.userWalletRecord.direction in'),
    '2': t('user.userWalletRecord.direction out'),
}

const baTable = new baTableClass(
    new baTableApi('/admin/user.UserWalletRecord/'),
    {
        pk: 'id',
        column: [
            { label: t('user.userWalletRecord.id'), prop: 'id', align: 'center', width: 100, operator: 'RANGE', sortable: 'custom' },
            {
                label: t('user.userWalletRecord.user_id'),
                prop: 'user_id',
                align: 'center',
                width: 90,
                show: false,
                operator: 'RANGE',
                sortable: false,
            },
            {
                label: t('user.userWalletRecord.user__username'),
                prop: 'user.username',
                align: 'center',
                minWidth: 110,
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                render: 'tags',
                comSearchRender: 'string',
            },
            {
                label: t('user.userWalletRecord.channel__name'),
                prop: 'channel.name',
                align: 'center',
                minWidth: 100,
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                render: 'tags',
                comSearchRender: 'string',
            },
            {
                label: t('user.userWalletRecord.biz_type'),
                prop: 'biz_type',
                align: 'center',
                minWidth: 120,
                operator: 'eq',
                render: 'tag',
                custom: bizTypeTagCustom,
                replaceValue: bizReplace,
            },
            {
                label: t('user.userWalletRecord.direction'),
                prop: 'direction',
                align: 'center',
                width: 90,
                operator: 'eq',
                render: 'tag',
                custom: {
                    '1': 'success',
                    '2': 'danger',
                },
                replaceValue: dirReplace,
            },
            {
                label: t('user.userWalletRecord.amount'),
                prop: 'amount',
                align: 'center',
                minWidth: 110,
                operator: 'RANGE',
                formatter: formatAmount,
            },
            {
                label: t('user.userWalletRecord.balance_before'),
                prop: 'balance_before',
                align: 'center',
                minWidth: 110,
                operator: 'RANGE',
                formatter: formatAmount,
            },
            {
                label: t('user.userWalletRecord.balance_after'),
                prop: 'balance_after',
                align: 'center',
                minWidth: 110,
                operator: 'RANGE',
                formatter: formatAmount,
            },
            {
                label: t('user.userWalletRecord.ref_type'),
                prop: 'ref_type',
                align: 'center',
                minWidth: 100,
                showOverflowTooltip: true,
                operator: 'LIKE',
            },
            { label: t('user.userWalletRecord.ref_id'), prop: 'ref_id', align: 'center', width: 100, operator: 'RANGE' },
            {
                label: t('user.userWalletRecord.idempotency_key'),
                prop: 'idempotency_key',
                align: 'center',
                minWidth: 120,
                showOverflowTooltip: true,
                operator: 'LIKE',
            },
            {
                label: t('user.userWalletRecord.operator_admin__username'),
                prop: 'operatorAdmin.username',
                align: 'center',
                minWidth: 100,
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                render: 'tags',
                comSearchRender: 'string',
            },
            {
                label: t('user.userWalletRecord.remark'),
                prop: 'remark',
                align: 'center',
                minWidth: 140,
                showOverflowTooltip: true,
                operator: 'LIKE',
            },
            {
                label: t('user.userWalletRecord.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
        ],
    },
    {}
)

provide('baTable', baTable)

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

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