<template>
    <div class="forgot-password">
        <el-card class="card">
            <template #header>
                <h2>忘记密码</h2>
            </template>
            <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
                <!-- 手机号输入 -->
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="form.mobile" placeholder="请输入手机号" />
                </el-form-item>

                <!-- 验证码输入 -->
                <el-form-item label="验证码" prop="captcha">
                    <el-row>
                        <el-col :span="16">
                            <el-input v-model="form.captcha" placeholder="请输入验证码" />
                        </el-col>
                        <el-col :span="8">
                            <el-button :loading="loading" @click="getCaptcha" class="captcha-btn">获取验证码</el-button>
                        </el-col>
                    </el-row>
                </el-form-item>

                <!-- 新密码输入 -->
                <el-form-item label="新密码" prop="newPassword">
                    <el-input type="password" v-model="form.newPassword" placeholder="请输入新密码" />
                </el-form-item>

                <!-- 确认密码输入 -->
                <el-form-item label="确认密码" prop="confirmPassword">
                    <el-input type="password" v-model="form.confirmPassword" placeholder="请确认密码" />
                </el-form-item>

                <!-- 提交按钮 -->
                <el-form-item>
                    <el-button type="primary" @click="resetPassword">提交</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';

const form = reactive({
    mobile: '',
    captcha: '',
    newPassword: '',
    confirmPassword: '',
});

const loading = ref(false);
const rules = reactive({
    mobile: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^[1][3-9][0-9]{9}$/, message: '请输入有效的手机号', trigger: 'blur' }
    ],
    captcha: [
        { required: true, message: '请输入验证码', trigger: 'blur' },
    ],
    newPassword: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        { min: 6, max: 16, message: '密码长度应在6-16字符之间', trigger: 'blur' }
    ],
    confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        { validator: (rule, value, callback) => {
            if (value !== form.newPassword) {
                callback(new Error('两次输入的密码不一致'));
            } else {
                callback();
            }
        }, trigger: 'blur' }
    ]
});

// 获取验证码（模拟）
const getCaptcha = async () => {
    if (!form.mobile) {
        ElMessage.error('请输入手机号');
        return;
    }

    loading.value = true;
    setTimeout(() => {
        loading.value = false;
        ElMessage.success('验证码已发送');
    }, 2000); // 模拟发送验证码的延时
};

// 提交重置密码
const resetPassword = () => {
    // 表单验证
    if (!form.mobile || !form.captcha || !form.newPassword || !form.confirmPassword) {
        ElMessage.error('请填写完整的表单信息');
        return;
    }

    // 如果表单验证通过
    ElMessage.success('密码重置成功！');
    // 执行密码重置操作（这里可以调用API）
};
</script>

<style scoped>
.forgot-password {
    max-width: 500px;
    margin: 0 auto;
    padding: 20px;
}

.card {
    padding: 20px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.captcha-btn {
    width: 100%;
    background-color: #409EFF;
    border-color: #409EFF;
}
</style>
