<template>
  <div class="login">
    <div class="main">
      <div class="title">安全生产信息化平台</div>
      <div class="form">
        <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 theme="filled" size="16" fill="#909399" />
              </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
                  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>
</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";

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("../../assets/images/public/loginbg.jpg") no-repeat top center;
  background-size: 100% 100%;

  .main {
    width: 600px;
    padding-top: 280px;
    margin: 0 auto;

    .title {
      width: 100%;
      text-align: center;
      font-size: 36px;
      color: #ffffff;
      font-weight: bold;
    }

    .form {
      width: 478px;
      min-height: 290px;
      padding: 20px 40px;
      border-radius: 5px;
      background: rgba(8, 22, 59, 0.36);
      border: 1px solid rgba(31, 58, 136, 0.9);
      margin: 60px auto 0;
      box-shadow: 0 0 20px rgba(47, 85, 124, 0.2) inset;

      .title {
        color: #666;
        text-align: center;
        font-weight: 700;
        font-size: 18px;
        margin-bottom: 20px;
      }

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

        .el-input {
          height: 40px;
        }
      }

      .button {
        .el-button {
          background: #0948d5;
          border: 1px solid #276aff;
          height: 45px;
          width: 100%;
          color: #ffffff;
          margin-top: 10px;
        }
      }
    }
  }
}

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