<template>
  <div class="w-login-content">

    <div class="w-login-outer">
      <div></div>
      <div class="w-login-form">
        <div>你好!<br />欢迎登录{{ title }}</div>
        <div><input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号" /></div>
        <div>
          <input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
            @keyup.enter="handleLogin" />
        </div>
        <div class="verify" v-if="captchaEnabled">
          <input v-model="loginForm.code" placeholder="验证码" auto-complete="off" @keyup.enter="handleLogin" />
          <img :src="codeUrl" @click="getCode" />
        </div>
        <div class="h2">
          <div><label><input type="checkbox" v-model="loginForm.rememberMe" />记住我</label></div>
          <div v-if="false"><a href="javascript:void(0);" @click="ElMessageBox.alert('请联系管理员');">忘记密码?</a></div>
        </div>
        <el-button :loading="loading" class="btn" size="large" type="primary" @click.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div class="copy">{{ copy }} </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import "@/assets/css/login.scss"
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from '@/store/modules/user'
import WBg1 from "../components/bg/WBg1.vue";
import { useDynamicTitle } from "@/utils/dynamicTitle"
import { onMounted } from "vue";
import { useSettingsStore, useLocalStore, useSessionStore } from '@/store'
import { ElMessage, ElMessageBox } from "element-plus";

const title = import.meta.env.VITE_APP_TITLE;
const copy = import.meta.env.VITE_APP_COPY;
const color = import.meta.env.VITE_APP_COLOR;

onMounted(() => {
  useSettingsStore().title = "登录"
  useDynamicTitle()
})

const userStore = useUserStore()
const router = useRouter();

const loginForm = ref({
  username: "",
  password: "",
  rememberMe: true,
  code: "",
  uuid: ""
});



const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(false);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

function login() {
  loading.value = true;
  // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
  if (loginForm.value.rememberMe) {
    Cookies.set("username", loginForm.value.username, { expires: 30 });
    Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
    Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
  } else {
    // 否则移除
    Cookies.remove("username");
    Cookies.remove("password");
    Cookies.remove("rememberMe");
  }
  // 调用action的登录方法
  userStore.login(loginForm.value).then(() => {
    router.push({ path: redirect.value || "/" });
  }).catch(() => {
    loading.value = false;
    // 重新获取验证码
    if (captchaEnabled.value) {
      getCode();
    }
  });
}

function handleLogin() {
  if(!loginForm.value.username){
    ElMessage.error("请输入帐号");
    return;
  }
  if(!loginForm.value.password){
    ElMessage.error("请输入帐号");
    return;
  }
  if(captchaEnabled.value){
    if(!loginForm.value.code) {
      ElMessage.error("请输入验证码");
      return;
    }
  }
  login();
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.data.img;
      loginForm.value.uuid = res.data.uuid;
    }
  });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
}

getCode();
getCookie();
</script>
