<!-- 交易商管理-交易商管理-交易商管理-资金账户 -->
<template>
    <teleport to="#appPageTeleport">
        <app-view>
            <template #header>
                <app-filter :option="filterOption" />
            </template>
            <app-table :data="dataList" :columns="tableColumns">
                <template #headerLeft>
                    <span>[{{ record.accountName }}]{{ t('investor.manage.user.account.title') }}</span>
                </template>
                <template #headerRight>
                    <div style="display: flex;">
                        <el-button @click="emit('closed')">{{ t('operation.close') }}</el-button>
                    </div>
                </template>
                <!-- 操作 -->
                <template #operate="{ row }">
                    <el-button size="small" icon="Search" @click="showComponent('Details', row)" circle plain />
                    <el-button size="small" icon="Edit" @click="showComponent('Edit', row)" circle plain />
                </template>
                <template #footer>
                    <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                        @change="onSearch" />
                </template>
            </app-table>
            <component ref="componentRef" :is="componentMap.get(componentId)" v-bind="{ selectedItem }"
                @closed="closeComponent" v-if="componentId" />
        </app-view>
    </teleport>
</template>

<script lang="ts" setup>
import { ref, PropType, defineAsyncComponent } from 'vue'
import { ElMessage } from 'element-plus'
import { useEnum } from '@/hooks/enum'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useComponent } from '@/hooks/component'
import { useRequest } from '@/hooks/request'
import { showAccount } 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 { i18n } from '@/stores'

const { global: { t } } = i18n

const props = defineProps({
    record: {
        type: Object as PropType<Investor.InvestorListRsp>,
        required: true
    }
})

const componentMap = new Map<string, unknown>([
    ['Details', defineAsyncComponent(() => import('./details/index.vue'))], // 详情
    ['Edit', defineAsyncComponent(() => import('./edit/index.vue'))], // 修改
])

const emit = defineEmits(['closed'])
const selectedItem = ref<Investor.ShowAccountRsp>()

const accounttypeEnum = useEnum('accounttype') // 内/外部
const tradestatusEnum = useEnum('traderstatus') // 交易状态
const currencyEnum = useEnum('currency') // 币种

const { componentRef, componentId, openComponent, closeComponent } = useComponent(() => run())

const { dataList, total, pageSize, pageIndex, run } = useRequest(showAccount, {
    params: {
        pageNum: 1,
        pageSize: 20,
        userid: props.record.userId,
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'accountid', label: 'investor.manage.user.account.accountid' },
    { field: 'taaccounttype', label: 'investor.manage.user.account.taaccounttype', formatValue: (val) => accounttypeEnum.getEnumTypeName(val) },
    { field: 'relateduserid', label: 'investor.manage.user.account.relateduserid' },
    { field: 'tradestatus', label: 'investor.manage.user.account.tradestatus', formatValue: (val) => tradestatusEnum.getEnumTypeName(val) },
    { field: 'currencyid', label: 'investor.manage.user.account.currencyid', formatValue: (val) => currencyEnum.getEnumTypeName(val) },
    { field: 'cur_risk_rate', label: 'investor.manage.user.account.cur_risk_rate' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Investor.ShowAccountReq>({
    filters: [
        {
            field: 'accountid',
            label: t('investor.manage.user.account.accountid'),
            placeholder: t('investor.manage.user.account.tips1')
        }
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

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

const showComponent = (code: string, row?: Investor.ShowAccountRsp) => {
    selectedItem.value = row
    openComponent(code)
}
</script>