<!-- 交易商管理-交易商管理-交易商销户审核 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <!-- 交易商名称 -->
            <template #userid="{ row }">
                {{ `(${row.userid})${row.accountname}` }}
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="handleOperateButtons(row)"
                    @click="(code: string) => openComponent(code, row)" circle />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useOperation } from '@/hooks/operation'
import { investorUserCancelApply } from '@/services/api/investor'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n

// 申请状态
const ucapplystatusEnum = useEnum('ucapplystatus')
// 处理状态
const handlestatusEnum = useEnum('handlestatus')

const { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Investor.InvestorUserCancelApplyRsp>({
    onClose: () => onSearch()
})

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(investorUserCancelApply, {
    params: {
        pageNum: 1,
        pageSize: 20
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'userid', label: 'investor.manage.cancelapply.userid' },
    { field: 'applystatus', label: 'investor.manage.cancelapply.applystatus', formatValue: (val) => ucapplystatusEnum.getEnumTypeName(val) },
    { field: 'applytime', label: 'investor.manage.cancelapply.applytime', formatValue: (val) => formatDate(val) },
    { field: 'handlestatus', label: 'investor.manage.cancelapply.handlestatus', formatValue: (val) => handlestatusEnum.getEnumTypeName(val) },
    { field: 'auditlogincode', label: 'investor.manage.cancelapply.auditlogincode' },
    { field: 'audittime', label: 'investor.manage.cancelapply.audittime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Investor.InvestorUserCancelApplyReq>({
    filters: [
        {
            field: 'accountname',
            label: t('investor.manage.cancelapply.accountname')
        },
        {
            field: 'handlestatus',
            label: t('investor.manage.cancelapply.handlestatus1'),
            options: () => handlestatusEnum.getEnumOptions()
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

// 处理操作按钮
const handleOperateButtons = (row: Investor.InvestorUserCancelApplyRsp) => {
    const buttons = ['investor_manage_cancelapply_details']
    if (!row.handlestatus) {
        buttons.push('investor_manage_cancelapply_audit')
    }
    return getActionButtons(buttons)
}

const onSearch = (clear = false) => {
    const qs = getQueryParams(clear)
    run(qs)
}
</script>