<template>
  <div class="login">
    <div class="main">
      <div class="logo">
        <!--        <img-->
        <!--          src="/src/assets/images/public/logo.png"-->
        <!--          alt=""-->
        <!--          width="200"-->
        <!--          height="33"-->
        <!--        />-->
      </div>
      <div class="content">
        <div class="introduce_content"></div>
        <div class="form">
          <div class="title">账号登录</div>
          <el-form
            ref="formRef"
            :model="form"
            :rules="rules"
            @submit.prevent="fnLogin"
          >
            <el-form-item prop="username">
              <el-input
                v-model="form.username"
                placeholder="请输入用户名"
                tabindex="1"
              >
                <template #prepend>
                  <icon-people theme="filled" size="16" fill="#909399" />
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="form.password"
                type="password"
                placeholder="请输入密码"
                tabindex="2"
              >
                <template #prepend>
                  <icon-lock
                    theme="filled"
                    size="16"
                    fill="#909399"
                    :stroke-width="3"
                  />
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <verification v-model:verification-pass="verificationPass" />
            </el-form-item>
            <el-form-item class="button">
              <el-button native-type="submit">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import Verification from "@/components/verification/index";
import { useUserStore } from "@/pinia/user";
import { getUserInfo, Login } from "@/request/api";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import dayjs from "dayjs";
import { encrypt } from "@/assets/js/aes_secret.js";

const router = useRouter();
const { formRef, validate } = useFormValidate();
const verificationPass = ref(false);
const userStore = useUserStore();
const form = ref({
  username: "",
  password: "",
});

const rules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const fnLogin = debounce(
  1000,
  () => {
    if (import.meta.env.DEV) {
      fnSubmitLogin();
      return;
    }
    fnSubmitLogin();
  },
  { atBegin: true }
);
const fnSubmitLogin = async () => {
  await validate("请输入用户名密码");
  const { token } = await Login({
    username: encrypt(form.value.username),
    password: encrypt(form.value.password),
  });
  await userStore.setToken(token);
  await userStore.setTokenTime(dayjs().format("YYYY-MM-DD HH:mm:ss"));
  const { user } = await getUserInfo();
  userStore.setUserInfo({
    ...userStore.getUserInfo,
    ...user,
  });
  await router.replace("/index");
};
</script>

<style scoped lang="scss">
.login {
  width: 100%;
  max-width: 1920px;
  margin: 0 auto;
  height: 100vh;
  position: relative;
  background: url("/src/assets/images/public/bg.png") no-repeat top center;
  background-size: 100% 100%;

  .main {
    width: 1200px;
    padding-top: 70px;
    margin: 0 auto;

    .content {
      margin-top: 170px;
      display: flex;
      justify-content: space-between;

      .introduce_content {
        width: 600px;
      }

      .form {
        width: 400px;
        height: 380px;
        box-sizing: border-box;
        border-radius: 5px;
        background: #ffffff;
        padding-bottom: 50px;
        margin-right: 20px;
        box-shadow: 0 0 20px rgb(109 109 109 / 40%);
        margin-top: 20px;

        .title {
          text-align: center;
          color: #222;
          font-size: 24px;
          border-bottom: 1px solid #eee;
          line-height: 60px;
        }

        .el-form-item {
          width: 320px;
          margin: 20px auto;

          .el-input {
            height: 40px;
          }
        }
        .button {
          .el-button {
            background: #0a7dfe;
            height: 45px;
            width: 100%;
            color: #ffffff;
            margin-top: 10px;
          }
        }
      }
    }
  }
}

:deep {
  .el-input-group__prepend {
    padding-top: 5px !important;
  }
}
</style>
