<!-- 管理员管理-管理员管理-编辑 -->
<template>
    <app-drawer :title="t('admin.user.edit.title')" width="480" v-model:show="show" :refresh="refresh"
        :loading="loading">
        <el-form ref="formRef" class="el-form--vertical" label-width="80px" :model="formData" :rules="formRules" :show-message="false">
            <el-form-item :label="t('admin.user.edit.logincode')" prop="logincode">
                <el-input v-model="formData.logincode" maxlength="50" :placeholder="t('common.pleaseenter')"
                    :disabled="!!record?.autoid" />
            </el-form-item>
            <el-form-item :label="t('admin.user.edit.password')" prop="password" v-if="!record?.autoid">
                <el-input v-model="formData.password" :placeholder="t('common.pleaseenter')" disabled />
            </el-form-item>
            <el-form-item :label="t('admin.user.edit.username')" prop="username">
                <el-input v-model="formData.username" maxlength="50" :placeholder="t('common.pleaseenter')" />
            </el-form-item>
            <el-form-item :label="t('admin.user.edit.roleId')" prop="roleId">
                <el-select v-model="formData.roleId" :placeholder="t('common.pleasechoice')">
                    <template v-for="item in dataList" :key="item.autoid">
                        <el-option :label="item.rolename" :value="item.autoid" />
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item :label="t('admin.user.edit.remark')" prop="remark">
                <el-input type="textarea" v-model="formData.remark" maxlength="200" :rows="3"
                    :placeholder="t('common.pleaseenter')" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit" :disabled="!!record?.autoid && !formData.logincode">{{
                t('operation.submit') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, PropType, toRaw, onMounted } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useRequest } from '@/hooks/request'
import { dealUserMsg, querySystemmanagerDetail, queryRoleByAdd } from '@/services/api/admin'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

const props = defineProps({
    record: {
        type: Object as PropType<Model.SystemmanagerRsp>
    }
})

const { global: { t } } = i18n
const formRef = ref<FormInstance>()
const show = ref(true)
const refresh = ref(false)

const formData = ref<Partial<Model.DealUserMsgReq>>({
    password: '123456'
})

const { dataList } = useRequest(queryRoleByAdd)

const { loading, run } = useRequest(querySystemmanagerDetail, {
    manual: true,
    onSuccess: ((res) => {
        formData.value = {
            autoid: res.data.autoid,
            logincode: res.data.logincode,
            username: res.data.username,
            roleId: Number(res.data.roleId),
            remark: res.data.remark
        }
    }),
    onError: (err) => {
        ElMessage.error(err)
    }
})

// 表单验证规则
const formRules: FormRules = {
    logincode: [{ required: true }],
    username: [{ required: true }],
    roleId: [{ required: true }],
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            const rawData = toRaw(formData.value)
            loading.value = true
            dealUserMsg({
                data: rawData
            }).then(() => {
                ElMessage.success(t('common.tips3'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips4') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}

onMounted(() => {
    const autoId = props.record?.autoid
    if (autoId) {
        run({ autoId })
    }
})
</script>