<!-- 会员机构管理-机构管理-机构资料管理-自营会员登录账号 -->
<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.terminal.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="Key" @click="showComponent('UpdatePassword', row)" circle plain />
                    <el-button size="small" :icon="row.loginstatus === 1 ? 'Lock' : ''"
                        @click="showComponent('UpdateStatus', 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="{ 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 { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useComponent } from '@/hooks/component'
import { queryselfsupport } 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>([
    ['UpdateStatus', defineAsyncComponent(() => import('./status/index.vue'))], // 锁定/解锁
    ['UpdatePassword', defineAsyncComponent(() => import('./password/index.vue'))], // 重置密码
])

const emit = defineEmits(['closed'])

// 帐号状态
const userstatusEnum = useEnum('userstatus')

const selectedItem = shallowRef<Member.SelfSupportRsp>()

const { global: { t } } = i18n

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'userid', label: 'member.institution.user.terminal.userid' },
    { field: 'loginid', label: 'member.institution.user.terminal.loginid' },
    { field: 'loginstatus', label: 'member.institution.user.terminal.loginstatus', formatValue: (val) => userstatusEnum.getEnumTypeName(val) },
    { field: 'modifytime', label: 'member.institution.user.terminal.modifytime', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, getQueryParams, resetFilters } = useDataFilter<Member.SelfSupportReq>({
    filters: [
        {
            field: 'loginid',
            label: t('member.institution.user.terminal.loginid')
        },
    ],
    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.SelfSupportRsp) => {
    selectedItem.value = row
    openComponent(code)
}

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