<template>
  <div class="login">
    <div class="main">
      <div class="midbg">
        <div class="logo">
          <img src="/src/assets/images/login/logo.png" alt="" />
        </div>
        <div class="title">
          <div class="chinese">交通运输企业智慧安全管理平台</div>
          <div class="english">
            INTELLIGENT SAFETY MANAGEMENT PLATFORM FOR TRANSPORTATION
            ENTERPRISES
          </div>
        </div>
      </div>
      <div class="form">
        <div class="title">
          欢迎登录系统
          <div class="line" />
        </div>
        <el-form
          ref="formRef"
          :model="data.form"
          :rules="data.rules"
          @submit.prevent="fnLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="data.form.username"
              placeholder="请输入用户名"
              tabindex="1"
            >
              <template #prepend>
                <icon-people size="16" fill="#9ba2a8" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="data.form.password"
              type="password"
              placeholder="请输入密码"
              tabindex="2"
            >
              <template #prepend>
                <icon-lock size="16" fill="#9ba2a8" :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>
        <el-popover
          placement="bottom"
          title="扫码下载APP"
          :width="150"
          trigger="click"
        >
          <div>
            <layout-qr-code
              src="111111"
              :width="120"
              :height="120"
              :margin="false"
            />
          </div>
          <template #reference>
            <div class="tip">企业安全APP</div>
          </template>
        </el-popover>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import Verification from "@/components/verification/index";
import { useUserStore } from "@/pinia/user";
import { Login } from "@/request/api";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import LayoutQrCode from "@/components/qr_code/index.vue";

const router = useRouter();
const formRef = ref(null);
const verificationPass = ref(false);
const userStore = useUserStore();
const data = reactive({
  form: {
    username: "",
    password: "",
  },
  rules: {
    username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  },
});

const fnLogin = debounce(
  1000,
  () => {
    if (import.meta.env.DEV) {
      fnSubmitLogin();
      return;
    }
    if (verificationPass.value) {
      fnSubmitLogin();
    } else {
      ElMessage.warning("请进行登录验证");
    }
  },
  { atBegin: true }
);
const fnSubmitLogin = async () => {
  await useFormValidate(formRef, "请输入用户名密码");
  // eslint-disable-next-line no-undef
  const jsencrypt = new JSEncrypt();
  jsencrypt.setPublicKey(
    "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDUoHAavCikaZxjlDM6Km8cX+ye78F4oF39AcEfnE1p2Yn9pJ9WFxYZ4Vkh6F8SKMi7k4nYsKceqB1RwG996SvHQ5C3pM3nbXCP4K15ad6QhN4a7lzlbLhiJcyIKszvvK8ncUDw8mVQ0j/2mwxv05yH6LN9OKU6Hzm1ninpWeE+awIDAQAB"
  );
  const KEYDATA = jsencrypt.encrypt(
    "zcloudchina" + data.form.username + ",zy," + data.form.password
  );

  const resData = await Login({
    KEYDATA,
    SOURCE: 1,
  });
  await userStore.setUserInfo({
    ...userStore.getUserInfo,
    ...resData,
  });
  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/login/loginbg.png") no-repeat top center;
  background-size: 100% 100%;

  .main {
    width: 90%;
    height: 90%;
    background: url("/src/assets/images/login/midbg.png") no-repeat top center;
    background-size: 100% 100%;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);

    .midbg {
      width: calc(100% - 540px);
      height: 100%;
      position: relative;

      .logo {
        img {
          width: 206px;
          height: 50px;
          margin-top: 35px;
          margin-left: 35px;
        }
      }

      .title {
        width: max-content;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);

        .chinese {
          font-size: 48px;
          font-weight: bold;
          color: #fff;
        }

        .english {
          margin-top: 15px;
          font-size: 16px;
          color: #fff;
        }
      }
    }

    .form {
      width: 540px;
      height: 100%;
      background-color: #fff;
      position: absolute;
      right: 0;
      top: 0;
      padding: 250px 80px 0 80px;

      .title {
        color: #444;
        font-weight: bold;
        font-size: 22px;
        display: flex;
        align-items: center;
        flex-direction: column;
        margin-bottom: 50px;

        .line {
          margin-top: 10px;
          background-color: #2490fb;
          height: 5px;
          width: 80px;
          border-radius: 5px;
        }
      }

      .el-input {
        --el-input-bg-color: #f2f4f9;
        --el-input-text-color: #000;
        --el-fill-color-light: #f2f4f9;
        --el-input-border-color: #dde0eb;
      }

      .button {
        .el-button {
          width: 100%;
          height: 50px;
          font-size: 16px;
          background-color: #265feb;
          border-color: #265feb;
        }
      }
    }

    .tip {
      width: max-content;
      cursor: pointer;
      position: absolute;
      right: 80px;
    }
  }
}

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

:global(.el-popover.el-popper) {
  --el-popover-bg-color: #fff;
  --el-popover-border-color: #fff;
  --el-fill-color-blank: #fff;
  --el-border-color: #fff;
  --el-text-color-regular: #000;
  --el-popover-title-text-color: #000;
  --el-popover-title-font-size: 12px;
}

:global(.el-popover__title) {
  text-align: center;
}
</style>
