<style lang='scss'>
.mobile {
  .page-login {
    .container {
      background-image: linear-gradient(
        180deg,
        rgba(255, 255, 255, 0) 20%,
        var(--el-bg-color) 0
      );
    }
  }
}

.page-login {
  height: 100vh;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  background-position: center;
  overflow: hidden;

  .container {
    background-image: linear-gradient(
      70deg,
      rgba(255, 255, 255, 0) 50%,
      var(--el-bg-color) 0
    );
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }

  .login-code {
    width: 33%;
    height: 40px;
    float: right;
    border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
    margin-left: 5px;

    img {
      height: 100%;
      width: 100%;
      cursor: pointer;
      vertical-align: middle;
    }
  }
}
</style>


<template>
  <div class="page-login flex">
    <div class="flex-sub container flex" :class="$store.state.app.device == 'desktop' ? '' : 'flex-direction'">
      <div class="flex-treble" v-if="$store.state.app.device == 'desktop'"></div>
      <div class="flex-treble flex justify-center align-center" style="overflow: hidden;">
        <!-- <div class="bg-el-color"  style="height: 300vh;width:30vw;position: absolute;right: 0;top:-50%;"></div> -->
        <div style="min-width:250px">
          <div class="text-center padding-top-lg">
            <!-- <div class="margin-top-lg" v-if="$store.state.app.device == 'desktop'">
				本系统由船队综合管理平台、网络系统 (VSAT 网络+4G)、智慧安防管理子系统、能耗管理子系统、货物管理子系统、证书管理子系统六大部分组成。
															</div> -->
          </div>
          <div class="text-bold text-xl padding-tb text-el-primary" style="color: var(--el-input-text-color,var(--el-text-color-regular));">智能船舶管理系统</div>
          <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="form">
            <el-form-item prop="username">
              <el-input autofocus v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号" @keyup.enter="handleLogin">
                <template #prefix>
                  <el-icon>
                    <User />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码" @keyup.enter="handleLogin">
                <template #prefix>
                  <el-icon>
                    <Lock />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaOnOff">
              <div class="flex justify-between" style="width: 100%;">
                <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter="handleLogin" class="flex-sub">
                  <template #prefix>
                    <svg-icon icon-class="validCode" class="el-input__icon input-icon" />
                  </template>
                </el-input>
                <div class="login-code">
                  <img :src="codeUrl" @click="getCode" class="login-code-img" />
                </div>
              </div>
            </el-form-item>
            <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px;background-color: rgba(0, 0, 0, 0);color: var(--el-color-primary);">
              记住密码
            </el-checkbox>
            <el-form-item style="width: 100%">
              <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click.prevent="handleLogin" @keyup.enter="handleLogin">
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right" v-if="register">
                <router-link class="link-type" :to="'/register'">立即注册</router-link>
              </div>
            </el-form-item>
          </el-form>

        </div>
      </div>
    </div>

  </div>
</template>

<script setup name="Login">
import AES from "@/utils/crypto";
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";

const store = useStore();
const router = useRouter();
const { proxy } = getCurrentInstance();

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

const loginRules = {
  username: [
    {
      required: true,
      trigger: "blur",
      message: "请输入您的账号",
    },
  ],
  password: [
    {
      required: true,
      trigger: "blur",
      message: "请输入您的密码",
    },
  ],
  code: [
    {
      required: true,
      trigger: "change",
      message: "请输入验证码",
    },
  ],
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaOnOff = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(router.currentRoute.value.query.redirect);

// console.log(redirect.value)

let key = "Yzkj20220512jhsj";

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在cookie中设置记住用户明和名命
      if (loginForm.value.rememberMe) {
        Cookies.set("username", AES.encrypt(loginForm.value.username, key), {
          expires: 30,
        });
        Cookies.set("password", AES.encrypt(loginForm.value.password, key), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, {
          expires: 30,
        });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }

      // 调用action的登录方法
      store
        .dispatch("Login", {
          code: loginForm.value.code,
          password: AES.encrypt(loginForm.value.password, key),
          username: AES.encrypt(loginForm.value.username, key),
          // password: loginForm.value.password,
          // username: loginForm.value.username,
          uuid: loginForm.value.uuid,
        })
        .then(() => {
          console.log(redirect.value || "/");
          router.push({
            path: redirect.value || "/",
          });
        })
        .catch(() => {
          loading.value = false;
          // 重新获取验证码
          if (captchaOnOff.value) {
            getCode();
          }
        });
    }
  });
}

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

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

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