<template>
  <div class="login">
    <h1>三梵</h1>
    <div class="content">
      <div class="warp-bg">
        <div class="left-bg">
          <img src="../assets/images/login-img.png" alt="" />

          <div class="swiper-text">
            <el-carousel height="100px" direction="vertical" indicator-position="none" :autoplay="true">
              <el-carousel-item>
                <h3>{{ $t("login.li1H3") }}</h3>
                <p>{{ $t("login.li1P") }}</p>
              </el-carousel-item>
              <el-carousel-item>
                <h3>{{ $t("login.li2H3") }}</h3>
                <p>{{ $t("login.li2P") }}</p>
              </el-carousel-item>
              <el-carousel-item>
                <h3>{{ $t("login.li3H3") }}</h3>
                <p>{{ $t("login.li3P") }}</p>
              </el-carousel-item>
            </el-carousel>
          </div>
        </div>
        <div class="right-bg">
          <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
            <div class="form" style="margin-top: 300px">
              <div class="">{{ $t("login.welcomeToLogin") }}~</div>
              <h2>三梵 {{ $t("login.managementSystem") }}</h2>
              <el-form-item prop="tenantId" style="margin-top: 30px">
                <!-- <el-input :placeholder="$t('login.pleaseEnterTheTenantId')" prefix-icon="user" @keyup.enter="handleLogin" autocomplete="email"
                          v-model="loginForm.tenantId">
                </el-input> -->
                <div style="display:flex;width:100%;align-items:center">
                  <div style="width:23px;text-align:right;margin-top:2px">
                    <el-icon color="#a8abb2">
                      <User />
                    </el-icon>
                  </div>
                  <el-select v-model="loginForm.tenantId" placeholder="请选择公司" style="width:calc(100% - 23px)" filterable>
                    <el-option v-for="item in tenantList" :key="item.tenantId" :label="item.enterpriseName" :value="item.tenantId" />
                  </el-select>
                </div>

              </el-form-item>
              <el-form-item prop="username" style="margin-top: 30px">
                <el-input :placeholder="$t('login.pleaseEnterYourAccountNumber')" prefix-icon="UserFilled" @keyup.enter="handleLogin"
                          autocomplete="username" v-model="loginForm.username">
                </el-input>
              </el-form-item>
              <el-form-item prop="password" style="margin-top: 30px">
                <el-input :placeholder="$t('login.pleaseEnterYourPassword')" prefix-icon="View" type="password" v-model="loginForm.password"
                          autocomplete="current-password" @keyup.enter="handleLogin">
                </el-input>
              </el-form-item>
              <!-- <el-form-item prop="code" class="code" style="margin-top:30px">
								<el-input
									placeholder="请输入验证码"
									prefix-icon="ChatSquare"
									v-model="loginForm.code"
									@keyup.enter="handleLogin">
								</el-input>
								<img :src="codeUrl" @click="getCode" class="login-code-img"/>
							</el-form-item> -->
              <div style="margin-top: 30px">
                <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px">{{ $t("login.rememberPassword") }}</el-checkbox>
              </div>
              <el-button type="primary" @click="handleLogin" style="width: 100%; margin-top: 30px">{{ $t("login.login") }}</el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

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

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

const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  tenantId: "",
  code: "",
  uuid: "",
});
const tenantList = ref([]);
const env = import.meta.env.VITE_APP_ENV;
if (env == "production") {
  loginForm.tenantId = "EHSD";
} else {
  loginForm.tenantId = "ehtest";
}

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 captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      Cookies.set("tenantId", loginForm.value.tenantId, {
        expires: 30,
      });
      // 勾选了需要记住密码设置在 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 || "/" });
          // 登录刷新
          // setTimeout(() => {
          //   location.reload();
          // }, 1000);
        })
        .catch(() => {
          loading.value = false;
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode();
          }
        });
    }
  });
}

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

const getTenantData = () => {
  getTenantList().then((res) => {
    if (res && res.data) {
      tenantList.value = res.data;
    }
  });
};
getTenantData();

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

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

<style lang="scss">
.login {
  .warp-bg {
    background: #fff;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
    .left-bg {
      width: calc(50vw + 200px);
      background: #0084ff;
      height: 100vh;
      float: left;
      overflow: hidden;
      position: relative;
      img {
        float: right;
        margin-right: 75px;
        margin-top: 120px;
      }

      .swiper-text {
        position: absolute;
        right: 164px;
        top: 120px;
        color: #fff;
        overflow: hidden;
        height: 200px;
        width: 500px;
        .el-carousel {
          width: 100%;
          height: 100%;
        }
        h3 {
          font-size: 40px;
          font-weight: 400;
          margin-bottom: 20px;
          margin-top: 0;
        }
        p {
          font-size: 26px;
          margin: 0;
        }
        p::before {
          content: " ";
          border-bottom: 4px solid #fff;
          display: inline-block;
          width: 60px;
          margin-right: 20px;
          position: relative;
          top: -5px;
        }
      }
    }
    .right-bg {
      width: calc(50vw - 200px);
      background: url(../assets/images/login-right-bg.png) no-repeat;
      background-size: cover;
      height: 100vh;
      float: right;
      overflow: hidden;
      .form {
        width: 500px;
        padding: 0 75px;
        box-sizing: border-box;
        .el-input {
          input {
            border: none;
            border-bottom: 1px solid #dddddd;
          }
        }
        .el-input__wrapper {
          box-shadow: 0 0 0 0 !important;
        }
        .code {
          position: relative;
          img {
            position: absolute;
            right: 0;
            bottom: 0;
          }
        }
      }
    }
  }
  h1 {
    position: fixed;
    left: 40px;
    top: 0;
    height: 96px;
    line-height: 96px;
    font-size: 26px;
    color: #fff;
  }
}
</style>
