<!-- 账号管理-资金账户管理 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons()"
                    @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 { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useOperation } from '@/hooks/operation'
import { queryTaAccount } from '@/services/api/account'
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 mainaccounttypeEnum = useEnum('mainaccounttype')
// 内/外部
const accounttypeEnum = useEnum('accounttype')
// 交易状态
const traderstatusEnum = useEnum('traderstatus')

const { global: { t } } = i18n

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'accountId', label: 'account.fundacct.accountId' },
    { field: 'isMain', label: 'account.fundacct.isMain', formatValue: (val) => mainaccounttypeEnum.getEnumTypeName(val) },
    { field: 'taAccountType', label: 'account.fundacct.taAccountType', formatValue: (val) => accounttypeEnum.getEnumTypeName(val) },
    { field: 'relatedName', label: 'account.fundacct.relatedName' },
    { field: 'parentAccountId', label: 'account.fundacct.parentAccountId' },
    { field: 'tradeStatus', label: 'account.fundacct.tradeStatus', formatValue: (val) => traderstatusEnum.getEnumTypeName(val) },
    { field: 'currency', label: 'account.fundacct.currency' },
    { field: 'accountName', label: 'account.fundacct.accountName' },
    { field: 'operate', label: 'common.operate', width: 240, fixed: 'right' }
])

const { filterOption, getQueryParams } = useDataFilter<Model.TaAccountReq>({
    filters: [
        { label: t('account.fundacct.accountId'), field: 'accountId' }
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', onClick: () => onSearch(true) }
    ]
})

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