<!-- 账号管理-资金账户管理-银行签约信息 -->
<template>
    <teleport to="#appPageTeleport">
        <app-view>
            <app-table :data="dataList" :columns="tableColumns" :loading="loading">
                <template #headerLeft>
                    <span>{{ t('account.fundacct.sign.accountId') }}{{ record.accountId }}</span>
                </template>
                <template #headerRight>
                    <div>
                        <el-button type="primary" @click="showComponent('Edit')">{{ t('operation.add') }}</el-button>
                        <el-button @click="emit('closed')">{{ t('operation.close') }}</el-button>
                    </div>
                </template>
                <!-- 操作 -->
                <template #operate="{ row }">
                    <el-button size="small" icon="Unlock" @click="showComponent('Cancel', 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="{ accountId: record.accountId, selectedItem }" @closed="closeComponent"
                v-if="componentId" />
        </app-view>
    </teleport>
</template>

<script lang="ts" setup>
import { shallowRef, PropType, defineAsyncComponent } from 'vue'
import { ElMessage } from 'element-plus'
import { decryptAES } from '@/services/crypto'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable'
import { useComponent } from '@/hooks/component'
import { queryAccountSign } from '@/services/api/account'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import { i18n } from '@/stores'

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

const componentMap = new Map<string, unknown>([
    ['Edit', defineAsyncComponent(() => import('./edit/index.vue'))], // 编辑
    ['Cancel', defineAsyncComponent(() => import('./cancel/index.vue'))], // 解约
])

const emit = defineEmits(['closed'])
const signstatusEnum = useEnum('signstatus') // 签约状态
const selectedItem = shallowRef<Model.AccountSignRsp>()

const { global: { t } } = i18n

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

const { filterOptons, getQueryParams } = useDataFilter<Model.TaAccountReq>()

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'accountName', label: 'account.fundacct.sign.accountName', formatValue: (val) => decryptAES(val) },
    { field: 'cusBankName', label: 'account.fundacct.sign.cusBankName' },
    { field: 'bankAccountNo', label: 'account.fundacct.sign.bankAccountNo', formatValue: (val) => decryptAES(val) },
    { field: 'bankChildAccount', label: 'account.fundacct.sign.bankChildAccount' },
    { field: 'bankAccountName', label: 'account.fundacct.sign.bankAccountName', formatValue: (val) => decryptAES(val) },
    { field: 'bankName', label: 'account.fundacct.sign.bankName' },
    { field: 'cardType', label: 'account.fundacct.sign.cardType' },
    { field: 'cardNo', label: 'account.fundacct.sign.cardNo', formatValue: (val) => decryptAES(val) },
    { field: 'currency', label: 'account.fundacct.sign.currency' },
    { field: 'signStatus', label: 'account.fundacct.sign.signStatus', formatValue: (val) => signstatusEnum.getEnumTypeName(val) },
    { field: 'operate', label: 'common.operate', width: 220, fixed: 'right' }
])

filterOptons.inputList = [
    {
        label: t('account.fundacct.sign.accountId1'), keys: ['accountId']
    }
]

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

const showComponent = (code: string, row?: Model.AccountSignRsp) => {
    selectedItem.value = row
    openComponent(code)
}

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