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

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.CommissionRecord/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('agent.commissionRecord.id'), prop: 'id', align: 'center', width: 80, operator: 'RANGE', sortable: 'custom' },
            {
                label: t('agent.commissionRecord.settlement_period_id'),
                prop: 'settlement_period_id',
                align: 'center',
                width: 130,
                operator: 'RANGE',
                show: false,
            },
            {
                label: t('agent.commissionRecord.settlement_period_no'),
                prop: 'settlementPeriod.settlement_no',
                align: 'center',
                minWidth: 170,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'tags',
            },
            { label: t('agent.commissionRecord.channel_id'), prop: 'channel_id', align: 'center', width: 100, operator: 'RANGE', show: false },
            {
                label: t('agent.commissionRecord.channel_name'),
                prop: 'channel.name',
                align: 'center',
                minWidth: 120,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'tags',
            },
            { label: t('agent.commissionRecord.admin_id'), prop: 'admin_id', align: 'center', width: 100, operator: 'RANGE', show: false },
            {
                label: t('agent.commissionRecord.admin_username'),
                prop: 'admin.username',
                align: 'center',
                minWidth: 120,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                render: 'tags',
            },
            {
                label: t('agent.commissionRecord.commission_rate'),
                prop: 'commission_rate',
                align: 'center',
                minWidth: 110,
                operator: 'RANGE',
                formatter: formatAmount2,
            },
            {
                label: t('agent.commissionRecord.calc_base_amount'),
                prop: 'calc_base_amount',
                align: 'center',
                minWidth: 120,
                operator: 'RANGE',
                formatter: formatAmount2,
            },
            {
                label: t('agent.commissionRecord.commission_amount'),
                prop: 'commission_amount',
                align: 'center',
                minWidth: 120,
                operator: 'RANGE',
                formatter: formatAmount2,
            },
            {
                label: t('agent.commissionRecord.status'),
                prop: 'status',
                align: 'center',
                width: 100,
                effect: 'dark',
                custom: { 0: 'info', 1: 'warning', 2: 'success' },
                operator: 'eq',
                render: 'tag',
                replaceValue: {
                    '0': t('agent.commissionRecord.status 0'),
                    '1': t('agent.commissionRecord.status 1'),
                    '2': t('agent.commissionRecord.status 2'),
                },
            },
            {
                label: t('agent.commissionRecord.settled_at'),
                prop: 'settled_at',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                comSearchRender: 'datetime',
                width: 170,
                sortable: 'custom',
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('agent.commissionRecord.remark'),
                prop: 'remark',
                align: 'center',
                minWidth: 160,
                operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: true,
            },
            {
                label: t('agent.commissionRecord.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.commissionRecord.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, commission_rate: '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>
