<template>
  <div class="login">
    <!-- 登录框区域 -->
    <div class="login_box">
      <el-form ref="ruleFormRef" :model="formValue" :rules="formRules">
        <el-form-item prop="userName">
          <div class="input_title">请输入用户名</div>
          <div class="input_box">
            <el-input
              style="height: 100%"
              placeholder="请输入用户名"
              v-model="formValue.userName"
            ></el-input>
          </div>
        </el-form-item>
        <el-form-item prop="password">
          <div class="input_title mt105">请输入密码</div>
          <div class="input_box">
            <el-input
              style="height: 100%"
              show-password
              placeholder="请输入密码"
              v-model="formValue.password"
              @keyup.enter="handleLogin(ruleFormRef)"
            ></el-input>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button
            class="btn"
            :loading="loading"
            @click="handleLogin(ruleFormRef)"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import type { FormInstance, FormRules } from "element-plus";
import { ElNotification } from "element-plus";
import type { loginFormData } from "@/api/user/type";
//引入获取当前时间的函数
import { getTime } from "@/utils/time";
//引入用户相关的小仓库
import useUserStore from "@/store/modules/user";

const useStore = useUserStore();
// 登录表单DOM元素
const ruleFormRef = ref<FormInstance>();
// 登录表单数据
const formValue = ref<loginFormData>({
  // 用户名
  userName: "",
  // 密码
  password: "",
});
// 表单验证规则
const formRules = ref<FormRules<loginFormData>>({
  userName: [{ required: true, message: "请输入用户名", trigger: "change" }],
  password: [{ required: true, message: "请输入密码", trigger: "change" }],
});

// 路由器对象
const router = useRouter();
// 路由对象
const route = useRoute();
// 登录按钮加载效果控制
const loading = ref(false);

// 登录按钮回调
const handleLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  // 验证表单规则是否合法
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      //加载效果:开始加载
      loading.value = true;

      try {
        //保证登录成功
        await useStore.userLogin(formValue.value);
        //判断登录的时候,路由路径当中是否有query参数，如果有就往query参数挑战，没有跳转到首页
        let redirect: any = route.query.redirect;
        router.push({ path: redirect || "/" });
        //登录成功提示信息
        ElNotification({
          type: "success",
          message: "欢迎回来",
          title: `HI,${getTime()}好`,
        });
        //登录成功加载效果消失
        loading.value = false;
      } catch (error) {
        //登录失败加载效果消失
        loading.value = false;
        //登录失败的提示信息
        ElNotification({
          type: "error",
          message: (error as Error).message,
        });
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>

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

  .login_box {
    box-sizing: border-box;
    padding: 94px 105px 0;
    width: 1273px;
    height: 1014px;
    background-image: url(@/assets/images/login-background.png);
    background-size: 100% 100%;

    .input_title {
      display: flex;
      align-items: center;
      padding-left: 35px;
      width: 1056px;
      height: 112px;
      font-size: 35px;
      color: #a6dbff;
      border-radius: 20px 20px 0 0;
      border: 3.5px solid #70b7fa;
    }

    .input_box {
      display: flex;
      align-items: center;
      padding: 0 35px;
      width: 1056px;
      height: 140px;
      border: 3.5px solid #70b7fa;
    }

    .mt105 {
      margin-top: 105px;
    }

    .btn {
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 55px auto;
      width: 532px;
      height: 126px;
      color: #dbeaeaff;
      font-size: 56px;
      cursor: pointer;
      border: none;
      background-color: rgba(112, 183, 250, 0.4);
    }
  }
}

::v-deep(.el-input__wrapper) {
  background-color: transparent;
  font-size: 42px;
  box-shadow: none;
}
::v-deep(.el-input__inner) {
  height: 100%;
  color: #dbeaeaff;
}
::v-deep(.el-input__icon) {
  font-size: 42px;
}
::v-deep(.el-form-item__error) {
  padding-top: 15px;
  font-size: 35px;
  left: 52px;
}
::v-deep(.el-form-item.is-error .el-input__wrapper) {
  box-shadow: none;
}
</style>
