<template>
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="80">
        <el-row>
            <el-col :span="12">
                <el-form-item label="用户名" prop="userName">
                    <el-input v-model="formData.userName" placeholder="请输入用户名" :disabled="type === 'edit'"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="formData.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="角色" prop="role">
                    <el-select v-model="formData.role" placeholder="请选择角色">
                        <el-option v-for="item in roleOption" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="邮箱" prop="mailbox">
                    <el-input v-model="formData.mailbox" placeholder="请输入邮箱"></el-input>
                </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="formData.phone" placeholder="请输入电话"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="启用" prop="enable">
                    <el-radio-group v-model="formData.enable">
                        <el-radio :label="true" border>有效</el-radio>
                        <el-radio :label="false" border>禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-col>
        </el-row>

        <div style="text-align: right;padding-top: 80px;">
            <el-button class="f18" @click="close">取消</el-button>
            <el-button class="f18" type="primary" @click="saveData">确认</el-button>
        </div>
    </el-form>
</template>

<script lang='ts' setup>
import { onMounted, reactive, ref, toRefs, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const emit = defineEmits(['close', 'save'])

const props = defineProps({
    type: String
})

const roleOption = [
    { label: '普通用户', value: '普通用户' },
    { label: '中级管理员', value: '中级管理员' },
    { label: '高级管理员', value: '高级管理员' },
]
const rules = reactive({
    userName: [
        { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
    ],
    name: [
        { required: true, message: '请输入姓名', trigger: ['blur', 'change'] },
    ],
    role: [
        { required: true, message: '请选择角色', trigger: ['blur', 'change'] },
    ],
    phone: [
        { required: true, message: '请输入电话', trigger: ['blur', 'change'] },
    ],
})

const formRef = ref()
const formData = ref({
    id: '',
    userName: '',
    name: '',
    role: '',
    mailbox: '',
    phone: '',
    enable: true,
} as any)


onMounted(() => {
    resetData()
})

defineExpose({
    formData,
    resetData,
})
function close() {
    emit('close')
}
function resetData() {
    formRef.value.resetFields()
    formData.value = {
        id: '',
        userName: '',
        name: '',
        role: '',
        mailbox: '',
        phone: '',
        enable: true,
    }
}
const saveData = async () => {
    await formRef.value.validate((valid: any, fields: any) => {
        if (valid) {
            ElMessage.success('保存成功！')
            emit('save', formData.value, props.type)
            close()
        } else {
            // console.log('error submit!', fields)
        }
    })
}
</script>

<style lang='scss' scoped>
:deep(.el-form-item) {
    .el-form-item__label {
        display: block;
        text-align: justify;
        text-align-last: justify;
        padding: 0 10px 0 20px;
        &:before {
            display: none;
        }
    }
}
</style>
