<template>
  <div class="logincontainer">
    <el-form
      class="loginForm"
      :model="loginModel"
      ref="loginFormRef"
      :rules="rules"
      :inline="false"
    >
      <el-form-item>
        <div class="loginTitle">Lic管理系统</div>
      </el-form-item>
      <el-form-item prop="username">
        <el-input
          placeholder="请输入账户"
          size="large"
          v-model="loginModel.username"
        >
          <template #prefix>
            <el-icon class="el-input__icon">
              <component :is="'avatar'" />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          size="large"
          placeholder="请输入密码"
          v-model="loginModel.password"
        >
          <template #prefix>
            <el-icon class="el-input__icon">
              <component :is="'lock'" />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <div class="code">
          <el-input
            placeholder="请输入验证码"
            size="large"
            v-model="loginModel.code"
            maxlength="4"
            @keyup.enter.native="toLogin"
            spellcheck="false"
            style="width: 249px !important"
          >
            <template #prefix>
              <el-icon class="el-input__icon">
                <circle-check />
              </el-icon>
            </template>
          </el-input>
          <img :src="imgSrc" @click="getImage" />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button
          class="mybtn"
          @click="login"
          type="primary"
          size="large"
          :loading="loginState"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import { watch } from "vue";
import useImage from "@/services/business/login/useImage";
import useBaseLogin from "@/services/business/login/useBaseLogin";
import useLogin from "@/services/business/login/useLogin";

//基础数据
const { loginModel, rules, loginFormRef } = useBaseLogin();

//验证码
const { imgSrc, getImage } = useImage();

//登录
const { login, loginState, num } = useLogin(loginModel);

const toLogin = () => {
  if (!loginState.value) login();
};

watch(num, () => getImage(), { deep: true });
</script>
<style scoped lang="scss">
.logincontainer {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("../../assets/login-background.jpg");

  .loginForm {
    height: 320px;
    width: 400px;
    border-radius: 10px;
    padding: 20px 35px;
    background-color: white;
    box-shadow: 0 0 25px #cac6c6;

    .loginTitle {
      margin: 0 auto;
      font-size: 24px;
      font-weight: 600;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .mybtn {
      width: 100%;
    }

    .el-icon {
      height: 2.8em;
      margin-left: -5px;
    }

    .code {
      width: 100%;
      padding: 0 6px;
      display: flex;
      justify-content: space-between;
      margin-bottom: 6px;
    }
  }
}
</style>
