<!-- 交易商管理-交易商管理-交易商管理-登录账户 -->
<template>
    <teleport to="#appPageTeleport">
        <app-view>
            <template #header>
                <app-filter :option="filterOption" />
            </template>
            <app-table :data="dataList" :columns="tableColumns">
                <template #headerRight>
                    <div style="display: flex;">
                        <el-button @click="emit('closed', true)">{{ t('operation.close') }}</el-button>
                    </div>
                </template>
                <!-- 操作 -->
                <template #operate="{ row }">
                    <el-button size="small" icon="Document" @click="showComponent('Log', row)" circle plain />
                    <el-button size="small" icon="Key" @click="showComponent('Password', row)" circle plain
                        v-if="[1, 2].includes(row.loginstatus)" />
                    <template v-if="row.loginstatus === 1">
                        <el-button size="small" icon="Lock" @click="showComponent('Lock', row)" circle plain />
                        <el-button size="small" icon="SwitchButton" @click="showComponent('Delete', row)" circle plain
                            v-if="userStore.userInfo.releType === 1" />
                        <el-button size="small" icon="Iphone" @click="showComponent('Details', row)" circle plain />
                    </template>
                    <el-button size="small" icon="Unlock" @click="showComponent('Lock', row)" circle plain
                        v-if="row.loginstatus === 2" />
                    <el-button size="small" icon="RefreshLeft" @click="showComponent('Recover', row)" circle plain
                        v-if="row.loginstatus === 3" />
                </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 { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useComponent } from '@/hooks/component'
import { useRequest } from '@/hooks/request'
import { queryInvestorLoginList } from '@/services/api/investor'
import { useUserStore } from '@/stores'
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>([
    ['Log', defineAsyncComponent(() => import('./log/index.vue'))], // 登录日志
    ['Password', defineAsyncComponent(() => import('./password/index.vue'))], // 重置密码
    ['Lock', defineAsyncComponent(() => import('./lock/index.vue'))], // 锁定/解锁
    ['Delete', defineAsyncComponent(() => import('./delete/index.vue'))], // 注销停用
    ['Recover', defineAsyncComponent(() => import('./recover/index.vue'))], // 恢复
    ['Details', defineAsyncComponent(() => import('./details/index.vue'))], // 详情
])

const emit = defineEmits(['closed'])

const userStore = useUserStore()

const selectedItem = ref<Investor.InvestorLoginListRsp>()

const loginstatusEnum = useEnum('loginstatus') // 账号状态

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

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'loginid', label: 'investor.manage.user.login.loginid' },
    { field: 'loginstatus', label: 'investor.manage.user.login.loginstatus', formatValue: (val) => loginstatusEnum.getEnumTypeName(val) },
    { field: 'modifytime', label: 'investor.manage.user.login.modifytime', formatValue: (val) => formatDate(val) },
    { field: 'modifiername', label: 'investor.manage.user.login.modifiername' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Investor.InvestorLoginListReq>({
    filters: [
        {
            field: 'loginid',
            label: t('investor.manage.user.login.loginid'),
        },
        {
            field: 'loginstatus',
            label: t('investor.manage.user.login.loginstatus'),
            options: () => loginstatusEnum.getEnumOptions()
        },
    ],
    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.InvestorLoginListRsp) => {
    selectedItem.value = row
    openComponent(code)
}
</script>