<!-- 账号管理-交易账号管理 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['account_tradingacct_add'])" @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-v2'
import { useOperation } from '@/hooks/operation'
import { queryTradingAcc } 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 loginstatusEnum = useEnum('loginstatus') // 账号状态

const { global: { t } } = i18n

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'loginid', label: 'account.tradingacct.loginid' },
    { field: 'loginstatus', label: 'account.tradingacct.loginstatus', formatValue: (val) => loginstatusEnum.getEnumTypeName(val) },
    { field: 'modifytime', label: 'account.tradingacct.modifytime', formatValue: (val) => formatDate(val) },
    { field: 'modifiername', label: 'account.tradingacct.modifiername' },
    { field: 'operate', label: 'common.operate', width: 220, fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Model.TradingAccReq>({
    filters: [
        { label: t('account.tradingacct.loginid'), field: 'loginid' },
        { label: t('account.tradingacct.loginstatus'), field: 'loginstatus', options: () => loginstatusEnum.getEnumOptions() }
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', onClick: () => resetFilters() }
    ]
})

// 处理操作按钮
const handleOperateButtons = (row: Model.TradingAccRsp) => {
    const buttons = ['account_tradingacct_details']
    if (row.loginstatus === 3) {
        buttons.push('account_tradingacct_recover')
    } else {
        buttons.push('account_tradingacct_modify', 'account_tradingacct_password')
        if (row.loginstatus === 1) {
            buttons.push('account_tradingacct_lock')
        } else {
            buttons.push('account_tradingacct_unlock')
        }
        buttons.push('account_tradingacct_close')
    }
    return getActionButtons(buttons)
}

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