<script lang="ts" setup>
import { reactive, ref } from "vue"
import { useRouter } from "vue-router"
import { useUserStore } from "@/store/modules/user"
import { User, Lock } from "@element-plus/icons-vue"
import ThemeSwitch from "@/components/ThemeSwitch/index.vue"
import { ElMessage, type FormInstance, FormRules } from "element-plus"
import SlideVerify, { SlideVerifyInstance } from "vue3-slide-verify"
import "vue3-slide-verify/dist/style.css"
import { type ILoginRequestData } from "@/api/login/types/login"
const isShowVerifyDialog = ref<boolean>(false)

const router = useRouter()
const loginFormRef = ref<FormInstance | null>(null)

/** 登录按钮 Loading */
const loading = ref(false)
/** 登录表单数据 */
const loginForm: ILoginRequestData = reactive({
  username: "",
  password: ""
})
/** 登录表单校验规则 */
const loginFormRules: FormRules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
    { pattern: "[A-Za-z0-9_-一-龥]+", message: "用户名不符合要求" }
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 8, max: 16, message: "长度在 8 到 16 个字符", trigger: "blur" }
  ]
}
/** 登录逻辑 */
const handleLogin = () => {
  loginFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      sliderVerify.value?.refresh()
      isShowVerifyDialog.value = true
    } else {
      return false
    }
  })
}
const sliderVerify = ref<SlideVerifyInstance>()
const onVerifyRetry = () => {
  ElMessage.warning("检测到非人为操作的哦！ try again")
}

const onVerifySuccess = (_: number) => {
  isShowVerifyDialog.value = false
  loading.value = true
  useUserStore()
    .login({
      username: loginForm.username,
      password: loginForm.password
    })
    .then(() => {
      router.push({ path: "/" })
      ElMessage.success("登录成功!")
    })
    .catch(() => {
      loginForm.password = ""
      ElMessage.error("登录失败，用户名或密码错误")
    })
    .finally(() => {
      loading.value = false
    })
}

const onVerifyFail = () => {
  ElMessage.error("验证失败，请重试！")
}
</script>

<template>
  <el-dialog v-model="isShowVerifyDialog" title="滑动验证" width="350px" align-center>
    <slide-verify
      ref="sliderVerify"
      slider-text="向右滑动->"
      :accuracy="4"
      @again="onVerifyRetry"
      @success="onVerifySuccess"
      @fail="onVerifyFail"
    />
  </el-dialog>
  <div class="login-container">
    <ThemeSwitch class="theme-switch" />
    <div class="login-card">
      <div class="title">
        <img src="@/assets/layout/logo-text-2.png" />
      </div>
      <div class="content">
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" @keyup.enter="handleLogin">
          <el-form-item prop="username">
            <el-input
              v-model.trim="loginForm.username"
              placeholder="用户名"
              type="text"
              tabindex="1"
              :prefix-icon="User"
              size="large"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="loginForm.password"
              placeholder="密码"
              type="password"
              tabindex="2"
              :prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          <el-button :loading="loading" type="primary" size="large" @click.prevent="handleLogin"> 登 录 </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100%;
  .theme-switch {
    position: fixed;
    top: 5%;
    right: 5%;
    cursor: pointer;
  }
  .login-card {
    width: 480px;
    border-radius: 20px;
    box-shadow: 0 0 10px #dcdfe6;
    background-color: #fff;
    overflow: hidden;
    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 150px;
      img {
        height: 100%;
      }
    }
    .content {
      padding: 20px 50px 50px 50px;
      :deep(.el-input-group__append) {
        padding: 0;
        overflow: hidden;
        .el-image {
          width: 100px;
          height: 40px;
          border-left: 0px;
          user-select: none;
          cursor: pointer;
          text-align: center;
        }
      }
      .el-button {
        width: 100%;
        margin-top: 10px;
      }
    }
  }
}
</style>
