<!-- 会员机构管理-机构管理-机构资料管理-资金账户 -->
<template>
    <teleport to="#appPageTeleport">
        <app-view>
            <template #header>
                <app-filter :option="filterOption" />
            </template>
            <app-table :data="dataList" :columns="tableColumns" :loading="loading">
                <template #headerLeft>
                    <span>[{{ record.accountname }}]{{ t('member.institution.user.account.accountname') }}</span>
                </template>
                <template #headerRight>
                    <div>
                        <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="Document" @click="showComponent('SignStatement', row)" circle plain />
                    <el-button size="small" icon="Edit" @click="showComponent('Edit', row)" circle plain />
                    <el-button size="small" icon="Key" @click="showComponent('UpdatePassword', 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="{ record: 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 { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useComponent } from '@/hooks/component'
import { organTaaccount } from '@/services/api/member'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import { i18n } from '@/stores'

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

const componentMap = new Map<string, unknown>([
    ['Details', defineAsyncComponent(() => import('@pc/views/account/fundacct/components/details/index.vue'))], // 详情
    ['SignStatement', defineAsyncComponent(() => import('./signstatement/index.vue'))], // 签解约流水
    ['Edit', defineAsyncComponent(() => import('./edit/index.vue'))], // 修改
    ['UpdatePassword', defineAsyncComponent(() => import('./password/index.vue'))], // 重置密码
])

const emit = defineEmits(['closed'])

// 账户类型
const mainaccounttypeEnum = useEnum('mainaccounttype')
// 内/外部
const accounttypeEnum = useEnum('accounttype')
// 交易状态
const traderstatusEnum = useEnum('traderstatus')
// 币种
const currencyEnum = useEnum('currency')

const selectedItem = shallowRef<Member.OrganTaaccountRsp>()

const { global: { t } } = i18n

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'accountId', label: 'member.institution.user.account.accountId' },
    { field: 'isMain', label: 'member.institution.user.account.isMain', formatValue: (val) => mainaccounttypeEnum.getEnumTypeName(val) },
    { field: 'taAccountType', label: 'member.institution.user.account.taAccountType', formatValue: (val) => accounttypeEnum.getEnumTypeName(val) },
    { field: 'relatedName', label: 'member.institution.user.account.relatedName' },
    { field: 'parentAccountId', label: 'member.institution.user.account.parentAccountId' },
    { field: 'tradeStatus', label: 'member.institution.user.account.tradeStatus', formatValue: (val) => traderstatusEnum.getEnumTypeName(val) },
    { field: 'currencyid', label: 'member.institution.user.account.currency', formatValue: (val) => currencyEnum.getEnumTypeName(val) },
    { field: 'curRiskRate', label: 'member.institution.user.account.curRiskRate' },
    { field: 'operate', label: 'common.operate', width: 180, fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Member.OrganTaaccountReq>({
    filters: [
        {
            field: 'accountId',
            label: t('member.institution.user.account.accountId')
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

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

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