<!-- @format -->

<template>
  <div class="login">
    <div class="reset-password-box">
      <el-form
        class="login-form"
        :model="resetPasswordForm"
        ref="resetPasswrodRef"
        :rules="resetPasswordRules"
      >
        <h3 class="title">密码重置</h3>
        <el-form-item prop="userName">
          <el-input
            readonly
            v-model="resetPasswordForm.userName"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="用户名"
          >
            <template #prefix
              ><svg-icon icon-class="user" class="el-input__icon input-icon"
            /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="eMail">
          <el-input
            readonly
            v-model="resetPasswordForm.eMail"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="邮箱"
          >
            <template #prefix
              ><svg-icon icon-class="user" class="el-input__icon input-icon"
            /></template>
          </el-input>
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input
            v-model="resetPasswordForm.passWord"
            type="password"
            size="large"
            auto-complete="off"
            placeholder="新密码"
          >
            <template #prefix
              ><svg-icon icon-class="user" class="el-input__icon input-icon"
            /></template>
          </el-input>
          <span class="remark">
            8-20位英文（区分大小写）、数字、特殊字符的组合，特殊字符包括 " ! ""
            " * " " _ "
          </span>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="resetPasswordForm.confirmPassword"
            type="password"
            size="large"
            auto-complete="off"
            placeholder="再次输入新密码"
          >
            <template #prefix
              ><svg-icon icon-class="user" class="el-input__icon input-icon"
            /></template>
          </el-input>
          <span class="remark">
            8-20位英文（区分大小写）、数字、特殊字符的组合，特殊字符包括 " ! ""
            " * " " _ "
          </span>
        </el-form-item>
        <el-form-item>
          <el-button
            size="large"
            type="primary"
            style="width: 100%; margin-top: 15px"
            round
            @click="onSubmit(resetPasswrodRef)"
          >
            <span>重置密码</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import { useRoute, useRouter } from "vue-router";
import { isLose, resetPassWord } from "@/api/login";
import { ElMessage } from "element-plus";
const route = useRoute();
const router = useRouter();

isLose({ userName: route.query.userName, eMail: route.query.eMail }).catch(
  (res) => {
    setTimeout(() => {
      console.log(router);
      router.push({ path: "/login" });
    }, 4000);
  }
);

const resetPasswordForm = reactive({
  userName: route.query.userName,
  eMail: route.query.eMail,
  passWord: "",
  confirmPassword: "",
});

const equalToPassword = (rule, value, callback) => {
  if (resetPasswordForm.passWord !== value) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};
const resetPasswordRules = reactive({
  passWord: [
    {
      required: true,
      trigger: "blur",
      message: "请输入重置的新密码",
    },
  ],
  confirmPassword: [
    {
      required: true,
      trigger: "blur",
      message: "重复输入重置的新密码",
    },
    { required: true, validator: equalToPassword, trigger: "blur" },
  ],
});

const resetPasswrodRef = reactive(null);

const onSubmit = (formRef) => {
  formRef.validate((vaild) => {
    if (vaild) {
      resetPassWord(resetPasswordForm).then((res) => {
        ElMessage({
          type: "scuccess",
          message: "重置成功，即将返回登录页。",
        });
        setTimeout(() => {
          router.push({ path: "/login" });
        }, 2000);
      });
    }
  });
};
</script>
<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-bg.jpg");
  background-size: cover;
}
.reset-password-box {
  width: 550px;
  height: 56%;
  background: #fff;
  box-shadow: 1.5px 3.99px 27px 0px rgba(0, 0, 0, 0.1);
  transition: 0.3s;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  font-family: "montserrat";
}
.login-form {
  padding: 60px 50px;
  border-left: 1px solid #cccccc5e;
  margin: auto;

  background-color: rgba(255, 255, 255, 0.72);
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 40%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.remark {
  color: #999;
  font-size: 12px;
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
