<template>
    <ItemWrap class="adduser-items" title="添加用户">
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" status-icon :rules="rules"
            label-width="auto" class="demo-ruleForm">
            <el-form-item label="用户名" prop="pass">
                <el-input v-model="ruleForm.pass" type="text" autocomplete="off" />
            </el-form-item>
            <el-form-item label="密码" prop="checkPass">
                <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item label="菜单权限" prop="age">
                <el-input v-model.number="ruleForm.age" />
            </el-form-item>
            <el-form-item class="button-group">
                <el-button type="primary" @click="submitForm(ruleFormRef)" class="btn">
                    提交
                </el-button>
                <el-button @click="resetForm(ruleFormRef)" class="btn">重置</el-button>
            </el-form-item>
        </el-form>
    </ItemWrap>
    <!-- <el-button @click="$emit('close')">关闭</el-button> -->
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'

const ruleFormRef = ref<FormInstance>()

const checkAge = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('Please input the age'))
    }
    setTimeout(() => {
        if (!Number.isInteger(value)) {
            callback(new Error('Please input digits'))
        } else {
            if (value < 18) {
                callback(new Error('Age must be greater than 18'))
            } else {
                callback()
            }
        }
    }, 1000)
}

const validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('Please input the password'))
    } else {
        if (ruleForm.checkPass !== '') {
            if (!ruleFormRef.value) return
            ruleFormRef.value.validateField('checkPass')
        }
        callback()
    }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('Please input the password again'))
    } else if (value !== ruleForm.pass) {
        callback(new Error("Two inputs don't match!"))
    } else {
        callback()
    }
}

const ruleForm = reactive({
    pass: '',
    checkPass: '',
    age: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
    pass: [{ validator: validatePass, trigger: 'blur' }],
    checkPass: [{ validator: validatePass2, trigger: 'blur' }],
    age: [{ validator: checkAge, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            console.log('submit!')
        } else {
            console.log('error submit!')
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style scoped>
::v-deep .el-form * {
    background-color: transparent !important;
}

.demo-ruleForm {
    margin: auto;
    text-align: center;
    font-size: 20px;
    margin-top: 120px;
}

.adduser-items {
    font-size: 25px;
    width: 100%;
    height: 500px;
}

.button-group {
    position: absolute;
    bottom: 0;
    right: 10px;
    display: flex;
    justify-content: flex-end;
}

.btn {
    margin-top: 100px;
}

::v-deep .el-input{
    font-size: 30px;
    width: 100%;
    height: 50px;
    color: rgb(255, 255, 255);
    border-color: rgb(0, 0, 0);
}
::v-deep .el-form-item__label {
    font-size: 20px; /* 修改字体大小 */
    color: #765ff8; /* 修改字体颜色 */
}
::v-deep .el-button {
    font-size: 16px;
    color: #ffffff;
}
</style>