<template>
  <div class="reset-password-container">
    <el-card class="reset-password-card">
      <div class="card-header">
        <h2>重置密码</h2>
        <p>请输入您的验证码和新密码</p>
      </div>
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-position="top"
        hide-required-asterisk
        @submit.prevent="handleSubmit"
        autocomplete="off"
      >
        <el-form-item label="邮箱地址" prop="email">
          <el-input
            v-model="form.email"
            placeholder="请输入您的邮箱地址"
            size="large"
            autocomplete="email"
            disabled
          />
        </el-form-item>

        <el-form-item label="验证码" prop="code">
          <el-input
            v-model="form.code"
            placeholder="请输入6位邮箱验证码"
            size="large"
          />
        </el-form-item>

        <el-form-item label="新密码" prop="password">
          <el-input
            type="password"
            v-model="form.password"
            placeholder="请输入您的新密码"
            show-password
            size="large"
            autocomplete="new-password"
          />
        </el-form-item>

        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
            type="password"
            v-model="form.confirmPassword"
            placeholder="请再次输入新密码"
            show-password
            size="large"
            autocomplete="new-password"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            :loading="isLoading"
            class="submit-button"
            size="large"
          >
            确认重置
          </el-button>
        </el-form-item>
      </el-form>
      <div class="card-footer">
        <el-link @click="$router.push('/login')">返回登录</el-link>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter, useRoute } from 'vue-router';
import type { FormInstance, FormRules } from 'element-plus';
import { resetPasswordWithCode } from '@/api/auth';

const router = useRouter();
const route = useRoute();
const formRef = ref<FormInstance>();
const isLoading = ref(false);

const form = reactive({
  email: '',
  code: '',
  password: '',
  confirmPassword: '',
});

onMounted(() => {
  if (route.query.email) {
    form.email = route.query.email as string;
  } else {
    ElMessage.error('无效的重置链接，请返回重试。');
    router.push('/forgot-password');
  }
});

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== form.password) {
    callback(new Error("两次输入的密码不一致!"));
  } else {
    callback();
  }
};

const rules = reactive<FormRules>({
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: ['blur', 'change'] }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 6, message: '验证码必须是6位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' },
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
});

const handleSubmit = () => {
  if (!formRef.value) return;
  formRef.value.validate(async (valid) => {
    if (valid) {
      isLoading.value = true;
      try {
        await resetPasswordWithCode({
          email: form.email,
          code: form.code,
          newPassword: form.password,
        });
        ElMessage.success('密码重置成功！即将跳转到登录页...');
        setTimeout(() => router.push('/login'), 2000);
      } catch (error) {
        ElMessage.error('密码重置失败，请检查验证码是否正确。');
        console.error(error);
      } finally {
        isLoading.value = false;
      }
    }
  });
};
</script>

<style scoped>
.reset-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('/reset-password-bg.avif');
  background-size: cover;
  background-position: center;
}

.reset-password-card {
  width: 480px;
  border-radius: 25px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(15px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
  padding: 20px;
}

.card-header {
  text-align: center;
  margin-bottom: 2rem;
}

.card-header h2 {
  color: #004d40;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.card-header p {
  color: #607d8b;
  font-size: 0.9rem;
}

.el-form-item {
  margin-bottom: 1.8rem;
}

.submit-button {
  width: 100%;
  border-radius: 10px;
  background-color: #004d40;
  border-color: #004d40;
  transition: background-color 0.3s ease;
  font-size: 1rem;
  padding: 1.2rem 0;
}

.submit-button:hover {
  background-color: #00695c;
  border-color: #00695c;
}

.card-footer {
  margin-top: 1rem;
  text-align: center;
}
</style> 
