<template>
  <div>
    <!-- <div class="topbar">
      <div class="logo">
        <img src="~@/assets/img/logo.png" />
      </div>
      <div class="title">
        <img src="~@/assets/img/top_title.png" />
      </div>
    </div> -->
    <div class="contnet">
      <div class="login_form">
        <img src="~@/assets/img/top_title.png" alt="" />
        <el-form :model="loginForm" :rules="loginRules" ref="form">
          <el-row>
            <el-col :span="24">
              <div class="login_title">欢迎登陆系统</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
                />
              </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
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-form-item prop="code">
            <el-row>
              <el-col :span="16">
                <el-input v-model="loginForm.code" placeholder="请输入验证码">
                  <template #prefix>
                    <el-icon><Picture /></el-icon>
                  </template>
                </el-input>
              </el-col>
              <el-col :span="8">
                <div class="login-code">
                  <span v-if="verificationCode.type == 'text'" class="login-code-img" @click="refreshCode">
                    {{ verificationCode.value }}
                  </span>
                  <img v-else :src="verificationCode.src" class="login-code-img" @click="refreshCode" />
                </div>
              </el-col>
            </el-row>
          </el-form-item> -->
          <el-row class="login_btn">
            <el-button type="primary" @click="loginSubmit(form)">登录</el-button>
          </el-row>
        </el-form>
      </div>
    </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: "1890",
  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.type === "text"
    ? (verificationCode.value.value = randomLenNum(verificationCode.value.lenth))
    : (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((err) => {
          ElMessage.warning("请检查用户名/密码后再次尝试");
        });
    }
  });
};

refreshCode();
</script>
<style lang="scss" scoped>
.topbar {
  height: 90px;
  background: #020066;
  display: flex;
  align-items: center;
  .logo {
    width: 80px;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 50px;
  }
  .title {
    img {
      height: 40px;
    }
  }
}
.contnet {
  width: 100%;
  // height: calc(100vh - 90px);
  height: 100vh;
  overflow: hidden;
  background-image: url(../../assets/img/bj2.jpg);
  background-size: 100% 100%;
  .login_form {
    position: fixed;
    top: 50%;
    left: 35%;
    transform: translate(-50%, -50%);
    width: 30%;
    box-sizing: border-box;
    img {
      width: 100%;
    }
    .el-form {
      width: 100%;
      height: 330px;
      background-color: #fff;
      border: 1px solid #c0c4cc;
      border-top: 3px blue solid;
    }
    .login_title {
      font-size: 20px;
      color: #999;
      height: 60px;
      line-height: 60px;
      text-align: center;
      border-bottom: 1px solid #c0c4cc;
      margin-bottom: 20px;
    }
    .el-form-item {
      display: block;
      padding: 0 20px;
      &__label {
        color: #000;
        font-size: 16px;
      }
    }
    .el-button {
      width: 100%;
      margin: 30px 20px 0 20px;
      background-color: #4a4996;
      border-color: #4a4996;
    }
    .login-code {
      display: flex;
      align-items: center;
      justify-content: space-around;
      margin: 0 0 0 10px;
      .login-code-img {
        margin-top: 2px;
        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;
      }
    }
  }
}
</style>
