<template>
  <div class="contnet">
    <img src="~@/assets/img/logo.png" alt="" />
    <div class="login_form">
      <img src="~@/assets/img/login_form_bj.png" alt="" />
      <el-form :model="loginForm" :rules="loginRules" ref="form">
        <el-row>
          <el-col :span="24">
            <div class="login_title">
              <span>欢迎登录系统</span>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item prop="username">
              <el-input
                :prefix-icon="UserFilled"
                v-model="loginForm.username"
                placeholder="请输入您的账号"
                clearable
                @keyup.enter="loginSubmit(form)"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item prop="password">
              <el-input
                :prefix-icon="Lock"
                type="password"
                show-password
                v-model="loginForm.password"
                placeholder="请输入密码"
                clearable
                @keyup.enter="loginSubmit(form)"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="login-code">
          <el-col :span="16">
            <el-form-item prop="code">
              <el-input
                :prefix-icon="Picture"
                v-model="loginForm.code"
                placeholder="请输入验证码"
                @keyup.enter="loginSubmit(form)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <img :src="verificationCode.src" class="login-code-img" @click="refreshCode" />
          </el-col>
        </el-row>
        <el-row class="login_btn">
          <el-button type="primary" @click="loginSubmit(form)">登录</el-button>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { randomLenNum } from "@/utils/util";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import { ILogin, IVerificationCode } from "@/const/interface_type";
import { UserFilled, Lock, Picture } from "@element-plus/icons-vue";

const loginForm = ref<ILogin>({
  username: "",
  password: "",
  code: "",
  randomStr: ""
});
const form = ref<FormInstance>();
const loginRules = ref<FormRules>({
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }]
});
const verificationCode = ref<IVerificationCode>({
  src: "/code",
  value: "",
  lenth: 4,
  type: "image"
});

const store = useStore();
const router = useRouter();

const refreshCode = () => {
  loginForm.value.randomStr = randomLenNum(verificationCode.value.lenth, true);
  verificationCode.value.src = `${process.env.VUE_APP_BASE_URL}/admin/api/code?randomStr=${loginForm.value.randomStr}`;
};

const loginSubmit = async (formRef?: FormInstance) => {
  if (!formRef) return;
  formRef.validate((valid: boolean) => {
    if (valid) {
      store
        .dispatch("LoginByUsername", loginForm.value)
        .then(() => {
          router.push(store.getters.tagWel.value);
        })
        .catch(() => {
          refreshCode();
        });
    }
  });
};

refreshCode();
</script>
<style lang="scss" scoped>
.contnet {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  background-image: url(~@/assets/img/login_bj.png);
  background-size: 100% 100%;
  position: relative;
  & > img {
    position: absolute;
    top: 10%;
    left: 50%;
    transform: translateX(-50%);
    width: 50%;
  }
  .login_form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-sizing: border-box;
    width: 60%;
    height: 60%;
    display: flex;
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
    & > img {
      width: 65%;
    }
    .el-form {
      height: 100%;
      width: 35%;
      background-color: #fff;
    }
    .login_title {
      font-size: 30px;
      color: #000;
      height: 120px;
      font-weight: bolder;
      display: flex;
      align-items: center;
      justify-content: center;
      user-select: none;
      &::after {
        content: "";
        position: absolute;
        top: 50%;
        left: 0;
        transform: translateY(-50%);
        height: 2px;
        width: 100%;
        background-color: #e1e1e1;
      }
      & > span {
        background-color: #fff;
        padding: 10px;
        z-index: 999;
      }
    }
    .el-form-item {
      padding: 10px 20px;
      .el-input {
        height: 40px;
      }
    }
    .el-button {
      width: 100%;
      height: 40px;
      border-radius: 20px;
      margin: 30px 20px 0 20px;
      background-color: linear-gradient(270deg, #2fa1ff 0%, #174df5 100%);
      border-color: linear-gradient(270deg, #2fa1ff 0%, #174df5 100%);
    }
    .login-code {
      display: flex;
      align-items: center;
      justify-content: space-around;
      .login-code-img {
        width: 100px;
        height: 38px;
        background-color: #fdfdfd;
        border: 1px solid #f0f0f0;
        color: #333;
        font-size: 14px;
        font-weight: bold;
        letter-spacing: 5px;
        line-height: 38px;
        text-indent: 5px;
        text-align: center;
        margin-bottom: 16px;
      }
    }
  }
}
</style>
