<template>
  <div class="email-registration">
    <div class="stepBar">
      <el-steps
        style="background-color: #f5f5f500"
        :active="active"
        finish-status="success"
        simple
      >
        <el-step title="账号信息" />
        <el-step title="邮箱验证" />
      </el-steps>
    </div>
    <!-- 账号信息 -->
    <div class="form" v-if="active === 0">
      <div class="title">找回密码</div>
      <div class="Email">
        <el-input
          type="text"
          size="large"
          class="input-item"
          v-model="Email"
          placeholder="请输入需要找回密码的邮箱"
          @keydown.enter.native="nextStep"
        />
      </div>
      <div class="btn">
        <div class="Return" @click="Return">返回</div>
        <div class="nextStep" @click="nextStep">下一步</div>
      </div>
    </div>
    <!-- 完成注册 -->
    <div class="form" v-if="active === 1">
      <div class="title">邮箱验证</div>
      <div class="Hint">
        已向您的{{ Email }}邮箱发送了一封验证邮件，请点击邮件中的链接完成找回。
      </div>
      <div class="ResendEmailbtn">
        <div class="Return" @click="previousStep">上一步</div>
        <div 
          class="ResendEmail" 
          @click="nextStep(1)"
          :style="isCounting ? 'background-image: linear-gradient(0deg, #cccccc, #dddddd); cursor: not-allowed;' : ''"
        >
          {{ isCounting ? `${countdown}秒后重发` : '重发邮件' }}
        </div>
        <div class="Return" @click="Return">返回登录</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {forgetPassword} from "@/api/account"; // 导入登录接口
import {
  ref, // 响应式基础
  watch, // 侦听器
  onMounted, // 组件挂载完成后执行
  onUpdated, // 组件更新后执行
  onUnmounted, // 组件销毁前执行
} from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
const active = ref(0); // 当前步骤
const Email = ref(""); // 邮箱
const countdown = ref(0); // 倒计时秒数
const isCounting = ref(false); // 是否正在倒计时
let timer = null; // 定时器

function startCountdown() {
  countdown.value = 60;
  isCounting.value = true;
  timer = setInterval(() => {
    countdown.value--;
    if (countdown.value <= 0) {
      clearInterval(timer);
      isCounting.value = false;
    }
  }, 1000);
}

//下一步
function nextStep(type) {
  if (isCounting.value) return;
  
  // 邮箱验证
  const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  if (!emailRegex.test(Email.value)) {
    ElMessage.error("请输入有效的邮箱地址");
    return;
  }
  
  forgetPassword({
    mailAddress: Email.value,
  }).then((res) => {
      startCountdown();
      if (type === 1) {
        
      }else{
        active.value = active.value + 1;
      }

      ElMessage.success("邮件发送成功");
  }).catch((err) => {
    ElMessage.error("邮件发送失败");
  });
}

function previousStep() {
    active.value = active.value - 1;
}


//返回登录页
function Return() {
  router.push("/");
}
// watch(refname, async (newQuestion, oldQuestion) => {
//   // 变化后执行
// });
onMounted(() => {
  // 组件挂载完成后执行
});
onUpdated(() => {
  // 组件更新后执行
});
onUnmounted(() => {
  if (timer) clearInterval(timer);
});
</script>

<style scoped>
.email-registration {
  padding: 0;
  margin: 0;
  width: 100vw;
  height: 100vh;
  background-image: url(../assets/bg.png);
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.stepBar {
  width: 900px;
}
.form {
  margin-top: 30px;
  width: 900px;
  height: 600px;
  background-image: linear-gradient(180deg, #dbf0f1, #ffffff);
  border-radius: 10px;
  border: 1px solid #4fcacd;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.title {
  font-size: 30px;
  color: #4fcacd;
  margin-top: 70px;
  text-align: center;
  font-weight: bold;
}
.Hint {
  width: 900px;
  height: 400px;
  color: #4fcacd;
  font-size: 20px;
  text-align: center;
  line-height: 500px;
  margin-top: -50px;
}
.ResendEmailbtn{
    width: 100%;
    margin-top: 50px;
    display: flex;
    justify-content:space-around;
    align-items: center;
}
.ResendEmail {
  width: 200px;
  height: 40px;
  background-image: linear-gradient(0deg, #4fcacd, #5fdbde);
  border-radius: 10px;
  color: #ffffff;
  font-size: 16px;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  transition: all 0.3s ease;
}
.ResendEmail:hover {
  box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);
  transform: scale(1.08);
  opacity: 0.8;
}
.Email {
  margin-top: 150px;
  width: 90%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.Password {
  margin-top: 30px;
  width: 90%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.ConfirmPassword {
  margin-top: 30px;
  width: 90%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.password-tip {
  font-size: 12px;
  color: #8c939d;
  margin-top: 10px;
}
.btn {
  width: 70%;
  margin-top: 150px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.Return {
  width: 200px;
  height: 40px;
  background-image: linear-gradient(0deg, #4fcacd, #5fdbde);
  border-radius: 10px;
  color: #ffffff;
  font-size: 16px;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  transition: all 0.3s ease;
}
.Return:hover {
  box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);
  transform: scale(1.08);
  opacity: 0.8;
}
.nextStep {
  width: 200px;
  height: 40px;
  background-image: linear-gradient(0deg, #4fcacd, #5fdbde);
  border-radius: 10px;
  color: #ffffff;
  font-size: 16px;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  transition: all 0.3s ease;
}
.nextStep:hover {
  box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);
  transform: scale(1.08);
  opacity: 0.8;
}
.avatar {
  width: 90%;
  margin-top: 30px;
  height: 178px;
}
.input {
  width: 90%;
  margin-top: 100px;
  height: 50px;
}
.input-item {
  border: none;
  width: 80%;
  height: 100%;
  background-color: #ffffff00;
  border-bottom: 1px solid #4fcacd;
}
:deep(.el-input__wrapper) {
  box-shadow: none !important;
  background-color: transparent !important;
}
:deep(.el-input__wrapper.is-focus) {
  box-shadow: none !important;
}
.avatar-uploader {
  width: 178px;
  height: 178px;
  border: 3px solid #4fcacd;
  border-radius: 10px;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.avatar-tip {
  width: 178px;
  text-align: center;
  font-size: 12px;
  color: #8c939d;
}
</style>
