<template>
  <div class="login-container">
    <div ref="formContainer" class="form-container">

      <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
      >
        <el-form-item class="form-item" prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
          <el-input
              ref="username"
              v-model:value="loginForm.username"
              placeholder="请输入账号"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="off"
          />
        </el-form-item>

        <el-form-item class="form-item" prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
          <el-input
              :key="pwdType"
              ref="password"
              v-model:value="loginForm.password"
              :type="pwdType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              auto-complete="off"
              @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="pwdType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-checkbox size="large" style="margin-top: 12px;" v-model="isRemember">记住密码</el-checkbox>

        <el-button
            :loading="loading"
            type="primary"
            round
            style="
                margin-top: 24px;
                font-size: 15px;
                letter-spacing: 4px;
                border-radius: 50px;
                height: 38px;
                width: 100%;
                background-color: #3d7af6;
                filter: drop-shadow(0px 4px 9px rgba(72, 93, 237, 0.63));
              "
            @click.native.prevent="handleLogin"
        >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script setup>

import { init } from "@/hooks/resize";
import { ipcRenderer, Notification } from "electron";
import { isvalidUsername } from "@/utils/validate";
import { useUserStore } from "@/store/user";
import { ref } from "vue"
import { useRouter } from "@/hooks/use-router";

const userStore = useUserStore()

const validateUsername = (rule, value, callback) => {
  if (!isvalidUsername(value)) {
    callback(new Error("请输入正确的用户名"));
  } else {
    callback();
  }
};
const validatePass = (rule, value, callback) => {
  if (value.length < 5) {
    callback(new Error("密码不能小于5位"));
  } else {
    callback();
  }
};
const isRemember = ref(false);
const loginForm = ref({
  username: "",
  password: "",
});
let lgifStr = window.localStorage.getItem('lgif')
if (lgifStr) {
  try {
    isRemember.value = true;
    loginForm.value = JSON.parse(window.atob(lgifStr))
  } catch (e) {
    loginForm.value = {
      username: "",
      password: "",
    }
  }
}
const loginFormRef = ref()
const loginRules = ref({
  username: [
    { required: true, trigger: "blur", validator: validateUsername },
  ],
  password: [
    { required: true, trigger: "blur", validator: validatePass },
  ],
});
const loading = ref(false);
const pwdType = ref("password");

const showPwd = () => {
  if (pwdType.value === "password") {
    pwdType.value = "";
  } else {
    pwdType.value = "password";
  }
};
const router = useRouter()
const toAdmin = () => {
  ipcRenderer.invoke("create-admin-window")
}
const handleLogout = () => {
  userStore.logOut()
}
const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;

      userStore.login(loginForm.value).then(res => {
        loading.value = false;
        ipcRenderer.invoke("toggle-main-window-resizable",true)
        ipcRenderer.invoke("main-window-resize",{
          width: 1200,
          height: 800
        })
        router.push('/')
        if (isRemember.value) {
          window.localStorage.setItem('lgif',window.btoa(JSON.stringify(loginForm.value)))
        } else {
          window.localStorage.removeItem('lgif')
        }
      }).catch(() => {
        loading.value = false;
      })
    } else {
      return false;
    }
  });
};

</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #4c79ee;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  width: 100vw;
  height: 100vh;
  .el-input {

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #666;
      height: 100%;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

}
</style>

<style lang="scss" scoped>
::v-deep .el-checkbox__inner {}
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url("~@/assets/imgs/login-bkg.png") no-repeat;
  background-size: cover;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;


  .form-container {
    padding: 20px 20px 24px 20px;
    background: #fff;
    border-radius: 10px;
    box-shadow: 0 2px 16px 0 rgba(0,0,0,.2);
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 0;
    color: $dark_gray;
    vertical-align: middle;
    width: 25px;
    display: inline-block;

    margin-left: 15px;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

::v-deep .el-form-item__content {
  display: flex;
  line-height: 34px;
  align-items: center;
  position: relative;
  border-bottom: 1px #aaa6 solid;
}
.form-item {
  margin-bottom: 10px;
}
</style>
