<!-- 交易商管理-交易商管理-交易商管理 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['investor_manage_user_export'])"
                    @click="openComponent" />
            </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'
import { useOperation } from '@/hooks/operation'
import { queryInvestorList } from '@/services/api/investor'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter/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 hasauthEnum = useEnum('hasauth')
// 状态
const areastatusEnum = useEnum('areastatus')
// 变更状态
const modifystatusEnum = useEnum('modifystatus')

const { filterOptons, getQueryParams } = useDataFilter<Investor.InvestorListReq>()

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'accountName', label: 'investor.manage.user.accountName' },
    { field: 'loginId', label: 'investor.manage.user.loginId' },
    { field: 'accountId', label: 'investor.manage.user.accountId' },
    { field: 'memberUserName', label: 'investor.manage.user.memberUserName' },
    { field: 'parentName', label: 'investor.manage.user.parentName' },
    { field: 'refereeName', label: 'investor.manage.user.refereeName' },
    { field: 'hasAuth', label: 'investor.manage.user.isAuth', formatValue: (val) => hasauthEnum.getEnumTypeName(val) },
    { field: 'accountStatus', label: 'investor.manage.user.accountStatus', formatValue: (val) => areastatusEnum.getEnumTypeName(val) },
    { field: 'modifyStatus', label: 'investor.manage.user.modifyStatus', formatValue: (val) => modifystatusEnum.getEnumTypeName(val) },
    { field: 'createTime', label: 'investor.manage.user.createTime', formatValue: (val) => formatDate(val) },
    { field: 'modifyTime', label: 'investor.manage.user.modifyTime', formatValue: (val) => formatDate(val) },
    { field: 'auditTime', label: 'investor.manage.user.auditTime', formatValue: (val) => formatDate(val) },
    { field: 'cancelTime', label: 'investor.manage.user.cancelTime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right', width: 260 }
])

filterOptons.inputList = [
    { label: t('investor.manage.user.memberUserId'), keys: ['memberUserId'] },
    { label: t('investor.manage.user.accountName'), keys: ['accountName'] }
]

filterOptons.selectList = [
    {
        key: 'accountStatus',
        label: t('investor.manage.user.accountStatus'),
        options: () => areastatusEnum.getEnumOptions(),
    },
    {
        key: 'hasAuth',
        label: t('investor.manage.user.isAuth'),
        options: () => hasauthEnum.getEnumOptions(),
    }
]

filterOptons.buttonList = [
    { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
    { label: t('operation.reset'), className: 'el-button--primary', onClick: () => onSearch(true) }
]

// 处理操作按钮
const handleOperateButtons = (row: Investor.InvestorListRsp) => {
    const buttons = ['investor_manage_user_details']
    if (row.accountStatus === 6) {
        buttons.push('investor_manage_user_recover')
    } else {
        if ([3, 4].includes(row.modifyStatus)) {
            buttons.push('investor_manage_user_cancel')
        } else {
            buttons.push('investor_manage_user_modify')
        }
        buttons.push('investor_manage_user_config', 'investor_manage_user_account', 'investor_manage_user_login', 'investor_manage_user_close')
    }
    return getActionButtons(buttons)
}

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