<template>
  <div class="page-login flex justify-center align-center">
    <div class="mask bg-el-color"></div>
    <div class="box flex shadow">
      <div class="flex align-center padding-lg left text-white" :style="`background:${$store.state.settings.theme}`">
        <!-- :style="`background:#7171C6`" -->
        <div>
          <div class="title text-bold">智能能效管理系统</div>
          <div class="margin-top-lg" v-if="$store.state.app.device == 'desktop'">
             扬海航运业务涉及货物装卸搬运、港澳航线货物运输、内贸普通货物运输、商品批发零售贸易、物业管理。
          </div>
          <div class="margin-top">
            <!-- <a href="http://www.chinagsi.com/">http://www.chinagsi.com/</a> -->
          </div>
        </div>
      </div>
      <div class="
          flex-sub flex
          align-center
          justify-center
          padding-lg
          right
          bg-el-color
        ">
        <div style="width:80%">
          <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="form">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
                <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">
                <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%"
                  @keyup.enter="handleLogin">
                  <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">记住密码</el-checkbox>
            <el-form-item style="width: 100%">
              <el-button :loading="loading" size="large" type="primary" style="width: 100%" @click.prevent="handleLogin"
                @keyup.enter.native="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>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { parseTime } from "@/utils/ruoyi";
import AES from "@/utils/crypto";

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

const loginForm = ref({
  // username: "admin",
  // password: "admin123",
  username: "",
  password: "",
  rememberMe: false,
  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(undefined);
const route = useRoute();
let key;
function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      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");
      }
      // key = parseTime(new Date(), '{yyyy}-{mm}-{dd} {hh}hsj')

      key = "Yzkj20220512jhsj";
      // 调用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(() => {
			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");
  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();
console.log("outer.currentRoute",route.query, route.query.redirect)
</script>

<style lang='scss' scoped>
.page-login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  background-position: center;

  .box {
    border-radius: 5px;
    width: 90%;
    max-width: 1100px;
    height: 90%;
    max-height: 700px;
    opacity: 0.97;
    overflow: hidden;

    .title {
      font-size: 1.5rem;
    }
  }

  .mask {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    opacity: 0.2;
  }

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

  .login-code {
    width: 33%;
    height: 40px;
    float: right;

    img {
      height: 100%;
      cursor: pointer;
      vertical-align: middle;
    }
  }
}

.mobile .page-login {
  .box {
    flex-direction: column;
  }
}

.desktop .page-login .box {
  .left {
    flex: 1;
  }

  .right {
    .form {
      // margin-left:20px;
    }
  }
}
</style>
