<template>
  <div class="auth-page">
    <div class="auth-card">
      <div class="auth-logo">
        <div class="logo-icon">
          <svg viewBox="0 0 24 24" fill="currentColor" width="24" height="24">
            <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"/>
          </svg>
        </div>
        <h2>{{ step === 1 ? '忘记密码' : '重置密码' }}</h2>
      </div>

      <el-steps :active="step - 1" simple style="margin-bottom: 24px">
        <el-step title="发送验证码" />
        <el-step title="重置密码" />
      </el-steps>

      <el-form v-if="step === 1" :model="emailForm" :rules="emailRules" ref="emailFormRef" @submit.prevent="handleSendCode">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="emailForm.email" type="email" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" native-type="submit" class="submit-btn">发送验证码</el-button>
        </el-form-item>
      </el-form>

      <el-form v-else :model="resetForm" :rules="resetRules" ref="resetFormRef" @submit.prevent="handleResetPassword">
        <el-form-item label="验证码" prop="code">
          <el-input v-model="resetForm.code" placeholder="请输入邮箱收到的验证码" />
        </el-form-item>
        <el-form-item label="新密码" prop="new_password">
          <el-input v-model="resetForm.new_password" type="password" show-password placeholder="至少6位" />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirm_password">
          <el-input v-model="resetForm.confirm_password" type="password" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" native-type="submit" class="submit-btn">重置密码</el-button>
        </el-form-item>
      </el-form>

      <div class="auth-links">
        <router-link to="/login">返回登录</router-link>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { authApi } from '../../api'

const router = useRouter()
const emailFormRef = ref()
const resetFormRef = ref()
const loading = ref(false)
const step = ref(1)

const emailForm = reactive({ email: '' })
const emailRules = {
  email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
}

const resetForm = reactive({ code: '', new_password: '', confirm_password: '' })
const resetRules = {
  code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
  new_password: [{ required: true, message: '请输入新密码', trigger: 'blur' }, { min: 6, message: '密码至少6位', trigger: 'blur' }],
  confirm_password: [{ required: true, message: '请确认密码', trigger: 'blur' }],
}

async function handleSendCode() {
  await emailFormRef.value?.validate()
  loading.value = true
  try {
    await authApi.forgotPassword(emailForm)
    ElMessage.success('验证码已发送到您的邮箱')
    step.value = 2
  } catch (err: any) {
    ElMessage.error(err.response?.data?.error || '发送失败')
  } finally {
    loading.value = false
  }
}

async function handleResetPassword() {
  await resetFormRef.value?.validate()
  if (resetForm.new_password !== resetForm.confirm_password) {
    ElMessage.warning('两次密码不一致')
    return
  }
  loading.value = true
  try {
    await authApi.resetPassword({ email: emailForm.email, code: resetForm.code, new_password: resetForm.new_password })
    ElMessage.success('密码重置成功，请登录')
    router.push('/login')
  } catch (err: any) {
    ElMessage.error(err.response?.data?.error || '重置失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.auth-page {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-card {
  background: #2d2d44;
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 16px;
  padding: 40px;
  width: 100%;
  max-width: 440px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.auth-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 32px;
}

.logo-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: linear-gradient(135deg, #4e6ef2, #7c5cfc);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  margin-bottom: 16px;
}

.auth-logo h2 {
  font-size: 22px;
  font-weight: 600;
  color: #fff;
}

.submit-btn {
  width: 100%;
  height: 44px;
  font-size: 15px;
  font-weight: 600;
}

.auth-links {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  font-size: 14px;
}

.auth-links a {
  color: rgba(255, 255, 255, 0.5);
  transition: color 0.15s;
}

.auth-links a:hover {
  color: #4e6ef2;
}
</style>
