<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue"
import { useRouter } from "vue-router"
import { useUserStore } from "@/store/modules/user"
import Logo from "@/assets/login/logo.svg?component"
import Title from "@/assets/login/title.svg?component"
import TitleLft from "@/assets/login/title-left.svg?component"
import TitleRight from "@/assets/login/title-right.svg?component"
import Name from "@/assets/login/name.svg?component"

const router = useRouter()

const isShow = ref(false)

/** 登录表单元素的引用 */
const loginFormRef = ref(null)

/** 登录按钮 Loading */
const loading = ref(false)
/** 登录表单数据 */
const loginFormData = ref({
  account: "",
  password: ""
})
/** 记住密码 */
const rememberPassword = ref(false)
/** 登录表单校验规则 */
const loginFormRules = {
  account: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
  ]
}
/** 登录逻辑 */
const handleLogin = () => {
  loginFormRef.value?.validate((valid, fields) => {
    if (valid) {
      loading.value = true
      useUserStore()
        .login(loginFormData.value)
        .then(() => {
          if (rememberPassword.value) {
            localStorage.setItem("rememberPassword", true)
            localStorage.setItem("loginFormData", JSON.stringify(loginFormData.value))
          } else {
            localStorage.removeItem("rememberPassword")
            localStorage.removeItem("loginFormData")
          }
          router.push({ path: "/" })
        })
        .catch(() => {
          createCode()
          loginFormData.value.password = ""
        })
        .finally(() => {
          loading.value = false
        })
    } else {
      console.error("表单校验不通过", fields)
    }
  })
}
const preventDefaultActions = (e) => {
  e.preventDefault()
}

const preventZoom = (e) => {
  if (e.ctrlKey || e.metaKey) {
    e.preventDefault()
  }
}

const preventTextSelection = (e) => {
  e.preventDefault()
}

onMounted(() => {
  rememberPassword.value = localStorage.getItem("rememberPassword") ? true : false

  loginFormData.value = localStorage.getItem("loginFormData") ? JSON.parse(localStorage.getItem("loginFormData")) : {}

  document.addEventListener("contextmenu", preventDefaultActions)
  document.addEventListener("keydown", (e) => {
    if (e.ctrlKey && (e.key === "A" || e.key === "a")) {
      preventDefaultActions(e)
    }
    if (e.ctrlKey && (e.key === "=" || e.key === "-")) {
      preventDefaultActions(e)
    }
    if (e.ctrlKey && e.shiftKey && (e.key === "+" || e.key === "_")) {
      preventDefaultActions(e)
    }
  })
  document.addEventListener("wheel", preventZoom, { passive: false })
  document.addEventListener("mousedown", preventTextSelection)
})

onBeforeUnmount(() => {
  document.removeEventListener("contextmenu", preventDefaultActions)
  document.removeEventListener("keydown", preventDefaultActions)
  document.removeEventListener("wheel", preventZoom)
  document.removeEventListener("mousedown", preventTextSelection)
})
</script>

<template>
  <Name class="name" />
  <div class="login-container">
    <div class="left">
      <div class="title">
        <TitleLft />
        <Title />
        <TitleRight />
      </div>
      <Logo class="logo" />
    </div>
    <div class="right">
      <div class="title">
        <span>账号登录</span>
      </div>
      <div class="content">
        <el-form ref="loginFormRef" :model="loginFormData" :rules="loginFormRules" @keyup.enter="handleLogin">
          <el-form-item prop="account">
            <el-input
              v-model.trim="loginFormData.account"
              placeholder="用户名"
              type="text"
              tabindex="1"
              size="large"
              clearable
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="loginFormData.password"
              placeholder="密码"
              type="text"
              tabindex="2"
              size="large"
              clearable
              :class="!isShow ? 'no_autofill_pwd' : ''"
            />
            <div class="pwd_icon" @click="isShow = !isShow">
              <el-icon v-if="isShow" color="#a8abb2" size="22"><View /></el-icon>
              <el-icon v-else color="#a8abb2" size="22"><Hide /></el-icon>
            </div>
          </el-form-item>
          <el-checkbox v-model="rememberPassword" label="记住密码" />
          <el-button :loading="loading" type="primary" size="large" @click.prevent="handleLogin">登 录</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.name {
  position: fixed;
  left: 91px;
  top: 70px;
}

.el-form-item {
  margin-bottom: 31px;
}

.el-input {
  height: 64px;
  font-size: 20px;
  :deep(.el-input__password) {
    font-size: 20px;
  }
}

.el-checkbox {
  position: relative;
  top: -15px;
  :deep(.el-checkbox__input) {
    --el-checkbox-input-height: 20px;
    --el-checkbox-input-width: 20px;
    .el-checkbox__inner:after {
      height: 10px;
      left: 5.5px;
      top: 1.5px;
      width: 4px;
    }
  }
  :deep(.el-checkbox__label) {
    font-size: 20px;
    font-weight: 400;
  }
}

.login-container {
  overflow: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 200px;
  min-height: 100%;
  background: linear-gradient(90deg, rgba(54, 111, 255, 1) 0%, rgba(93, 160, 252, 1) 100%);
  .left {
    display: flex;
    flex-direction: column;
    align-items: center;
    .title {
      display: flex;
      gap: 25px;
    }
    .logo {
      width: 592px;
      margin-top: 63px;
    }
  }
  .right {
    position: relative;
    left: 100px;
    min-width: 534px;
    border-radius: 10px;
    box-shadow: 0px 2px 16px rgba(92, 5, 5, 0.25);
    background-color: var(--el-bg-color);
    .title {
      display: flex;
      justify-content: center;
      margin-top: 68px;
      font-size: 32px;
      font-weight: 400;
    }
    .content {
      padding: 48px 40px 138px 40px;
      .el-button {
        width: 100%;
        height: 66px;
        margin-top: 80px;
        font-size: 28px;
        font-weight: 500;
        background: rgba(60, 120, 221, 1);
      }
    }
  }
}

.no_autofill_pwd {
  ::v-deep(.el-input__inner) {
    -webkit-text-security: disc !important;
  }
}

.pwd_icon {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  top: 12px;
  right: 5px;
  width: 40px;
  height: 40px;
  cursor: pointer;
}

::v-deep(.el-input__wrapper) {
  padding-right: 50px;
}

::v-deep(.el-input__suffix .el-input__clear) {
  font-size: 24px;
}

// 媒体查询 1680px
@media screen and (max-width: 1680px) {
  .login-container {
    gap: 50px;
  }
}

// 媒体查询 1440px
@media screen and (max-width: 1440px) {
  .left {
    display: none !important;
  }
  .right {
    left: 0 !important;
  }
}
</style>
