<!-- 管理员管理-管理员管理-变更状态 -->
<template>
    <app-drawer :title="t('common.alert')" v-model:show="show" :loading="loading" :refresh="refresh">
        <div class="g-text-message">{{ status.message }}</div>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.confirm') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef, PropType, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { dealUserStatus } from '@/services/api/admin'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

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

const { global: { t } } = i18n
const show = shallowRef(true)
const refresh = shallowRef(false)
const loading = shallowRef(false)

const status = computed(() => {
    switch (props.componentId) {
        case 'admin_user_reset':
            return {
                message: t('admin.user.status1.tips1'),
                type: 0
            }
        case 'admin_user_disable':
            return {
                message: t('admin.user.status1.tips2'),
                type: 1
            }
        case 'admin_user_enable':
            return {
                message: t('admin.user.status1.tips3'),
                type: 2
            }
        case 'admin_user_unlock':
            return {
                message: t('admin.user.status1.tips4'),
                type: 3
            }
        default:
            return {
                message: t('admin.user.status1.tips5'),
                type: -1
            }
    }
})

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

const onSubmit = () => {
    if (status.value.type > -1) {
        loading.value = true
        dealUserStatus({
            data: {
                autoid: props.record.autoid,
                type: status.value.type
            }
        }).then(() => {
            ElMessage.success(t('common.tips7'))
            onCancel(true)
        }).catch((err) => {
            ElMessage.error(t('common.tips8') + err)
            onCancel()
        }).finally(() => {
            loading.value = false
        })
    } else {
        onCancel()
    }
}
</script>