<!-- 会员机构管理-机构管理-机构资料管理-登录账户 -->
<template>
    <teleport to="#appPageTeleport">
        <app-view>
            <app-table :data="dataList" :columns="tableColumns" :loading="loading">
                <template #headerLeft>
                    <span>[{{ record.accountname }}]{{ t('member.institution.user.login.accountname') }}</span>
                </template>
                <template #headerRight>
                    <div>
                        <el-button type="primary" @click="updateStatuses(1)">{{ t('member.institution.user.login.allstart') }}</el-button>
                        <el-button type="primary" @click="updateStatuses(2)">{{ t('member.institution.user.login.allstop') }}</el-button>
                        <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.managerstatus === 1 ? 'SwitchButton' : 'Refresh'"
                        @click="showComponent('UpdateStatus', row)" circle plain />
                </template>
                <template #footer>
                    <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                        @change="run" />
                </template>
            </app-table>
            <component ref="componentRef" :is="componentMap.get(componentId)"
                v-bind="{ statuses, 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 { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useRequest } from '@/hooks/request'
import { useComponent } from '@/hooks/component'
import { queryorgan2User } from '@/services/api/member'
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<Member.OrganDetailListRsp>,
        required: true
    }
})

const componentMap = new Map<string, unknown>([
    ['UpdateStatuses', defineAsyncComponent(() => import('./statuses/index.vue'))], // 全部账户状态
    ['UpdateStatus', defineAsyncComponent(() => import('./status/index.vue'))], // 账户状态
    ['UpdatePassword', defineAsyncComponent(() => import('./password/index.vue'))], // 重置密码
])

const emit = defineEmits(['closed'])

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

const { global: { t } } = i18n
const statuses = shallowRef(0) // 所有状态
const selectedItem = shallowRef<Member.OrganTaaccountRsp>()

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

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

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

const updateStatuses = (value: number) => {
    statuses.value = value
    openComponent('UpdateStatuses')
}

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