<template>
  <div class="login">
    <div class="lg_left">
        <img src="@/assets/zgyd.png" class="lg_logo" alt="Image">
        <div class="lg_h2">君营直聘</div>
    </div>
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form" label-position="top">
      <h2 class="title">欢迎登录 <span style="color: #346DF2;">君营直聘</span></h2>
      <el-form-item prop="account" label="用户名">
        <el-input
          v-model="loginForm.account"
          type="text"
          size="large"
          auto-complete="off"
          placeholder="输入后台账号"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input
          v-model="loginForm.password"
          type="password"
          size="large"
          auto-complete="off"
          placeholder="输入登录密码"
          @keyup.enter="handleLogin"
        >
        </el-input>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width:100%;"
          @click.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from '@/store/modules/user'

const userStore = useUserStore()
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  account: "",
  password: "",
  rememberMe: false,
});
const loginRules = {
  account: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
};

const loading = ref(false);
const redirect = ref(undefined);

watch(route, (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect;
}, { immediate: true });
function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("account", loginForm.value.account, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("account");
        Cookies.remove("password");
      }
      // 调用action的登录方法
      userStore.login(loginForm.value).then((res) => {
        if (res.code == "200") {
          const query = route.query;
          const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
            if (cur !== "redirect") {
              acc[cur] = query[cur];
            }
            return acc;
          }, {});
          router.push({ path: "/", query: otherQueryParams });
        }else {
          proxy.$modal.msgError(res.message);
          loading.value = false;
        }
      }).catch(() => {
        loading.value = false;
      });
    }
  });
}

function getCookie() {
  const account = Cookies.get("account");
  const password = Cookies.get("password");
  loginForm.value = {
    account: account === undefined ? loginForm.value.account : account,
    password: password === undefined ? loginForm.value.password : decrypt(password),
  };
}
getCookie();
</script>

<style lang='scss' scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/bj.png");
  background-size: cover;
  position: relative;
  .lg_left {
    position: absolute;
    left: 200px;
    top: 10px;
    display: flex;
    .lg_logo {
        width: 70px;
        height: 70px;
        margin-right: 10px;
        margin-top: 25px;
    }
    .lg_h2 {
        color: #000;
        font-size: 40px;
        font-weight: 600;
        line-height: 120px;
        font-style: italic;
    }
  }
  .login-form {
    background: #ffffff;
    width: 380px;
    height: 471px;
    padding: 30px;
    position: absolute;
    border-radius: 20px;
    right: 24%;
    top: 22%;
    box-shadow: 0px 0px 12px rgba(0,0,0,.2);
    overflow: hidden;
    .title {
      padding: 30px 0;
      text-align: left;
      color: #707070;
    }
    .el-input {
      height: 40px;
      input {
        height: 40px;
      }
    }
    .el-button{
      height: 34px;
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 0px;
    }
  }
  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
}

</style>
