<template>
  <div class="system-layer" :style="{ backgroundImage: `url(${appConstant.banner})` }">
    <div class="content">
      <div class="login-layer">
        <div class="content">
          <div class="info">
            <img src="@/app/xmfire/images/banner@2x.png" />
            <span>{{ dateFormat(currentDate, "yyyy-MM-dd hh:mm:ss") }}</span>
          </div>
          <el-form class="panel login" :model="form" @submit.stop>
            <img :src="appConstant.ico" />
            <h2>{{ appConstant.title }}</h2>
            <el-form-item class="panel-form-item">
              <p class="err-info">{{ verification.phone }}</p>
              <el-input
                :maxlength="11"
                v-model.trim="form.phone"
                placeholder="手机号"
                @keydown.enter="submitClick"
              ></el-input>
            </el-form-item>
            <el-form-item class="panel-form-item">
              <p class="err-info">{{ verification.psw }}</p>
              <el-input
                v-model="form.psw"
                :maxlength="16"
                placeholder="密码"
                :type="flag ? 'password' : 'text'"
                @keydown.enter="submitClick"
              >
                <template v-slot:suffix>
                  <img
                    v-if="flag"
                    @click="flag = !flag"
                    style="width: 20px; margin: 15px"
                    src="@/assets/image/pasword.png"
                    alt=""
                  />
                  <el-icon :size="20" @click="flag = !flag" class="icon-style" v-else>
                    <View />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item class="panel-form-item code-form-item">
              <p class="err-info">{{ verification.code }}</p>
              <el-input
                v-model="form.code"
                placeholder="验证码"
                @keydown.enter="submitClick"
                class="code-input"
              >
                <template v-slot:append>
                  <img :src="codeImg" class="code-img" @click="refer" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item class="panel-form-item">
              <el-button type="primary" class="fill" @click="submitClick">登录</el-button>
            </el-form-item>
            <div class="more">
              <a @click="$router.push({ name: 'forget' })">忘记密码</a>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, watch, ref, computed, onUnmounted } from "vue";
import { openErrorMsg, baseURL, getCode } from "@/request";
import { PHONE } from "@/constant/REG";
import { guid, strToParams, dateFormat } from "@/util";
import { RouteName, router } from "@/router";
import { login } from "@/store/system";
import { appConstant } from "@/app";
import { user } from "@/store/user";

const currentDate = ref(new Date());
const interval = setInterval(() => {
  currentDate.value = new Date();
});
onUnmounted(() => clearInterval(interval));

// 是否显示明文密码
const flag = ref(true);
// 表单
const form = reactive({
  phone: localStorage.getItem("userName") || "",
  psw: localStorage.getItem("password") || "",
  code: "",
  remember: import.meta.env.DEV || localStorage.getItem("remember") === "1",
});
const verification = reactive({ phone: "", psw: "", code: "" });
// 验证
watch(
  form,
  () => {
    console.log("form", form);
    if (!form.phone) {
      verification.phone = "请输入手机号";
    } else if (form.phone == "88888888888") {
      verification.phone = "";
    } else {
      verification.phone = PHONE.REG.test(form.phone) ? "" : PHONE.tip;
    }
    if (!form.psw) {
      verification.psw = "请输入密码";
    } else {
      verification.psw = "";
    }
    if (!form.code.trim()) {
      verification.code = "请输入验证码";
    } else {
      verification.code = "";
    }
  },
  { immediate: true }
);

// 图片验证码
const imgKey = ref(guid());
const refer = () => (imgKey.value = guid());
const codeImg = computed(() => baseURL + getCode + "?key=" + imgKey.value);

// 表单提交
const submitClick = async () => {
  if (verification.phone && verification.phone !== "88888888888") {
    return openErrorMsg(verification.phone);
  }
  if (verification.psw) return openErrorMsg(verification.psw);
  if (verification.code) return openErrorMsg(verification.code);

  try {
    await login({ phoneNum: form.phone, code: form.code, password: form.psw });

    if (form.remember) {
      localStorage.setItem("userName", form.phone);
      localStorage.setItem("password", form.psw);
      localStorage.setItem("remember", "1");
    } else {
      localStorage.setItem("userName", "");
      localStorage.setItem("password", "");
      localStorage.setItem("remember", "0");
    }

    const params = strToParams(window.location.search);
    if ("redirect" in params) {
      const url = new URL(unescape(params.redirect));
      url.searchParams.delete("token");
      url.searchParams.append("token", user.value.token);
      window.location.replace(url);
    } else {
      router.replace({ name: RouteName.scene, params: { caseId: 360 } });
    }
  } catch (e) {
    console.error(e);
    return refer();
  }
};
</script>

<style lang="scss" scoped>
.system-layer {
  width: 100%;
  min-height: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  background: no-repeat left bottom;
  background-size: cover;
}

.content {
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.login-layer {
  text-align: right;
}
.content {
  display: flex;
  justify-content: center;
  align-items: flex-start;
}
.info {
  color: #fff;
  flex: none;
  text-align: left;
  position: relative;
  height: 100%;
  pointer-events: none;
  span {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 18px;
    color: #fff;
  }

  img {
    height: 562px;
  }
  h1 {
    font-size: 2.8rem;
    line-height: 3.7rem;
    margin-bottom: 0.7rem;
  }
  p {
    font-size: 2rem;
    line-height: 2.2rem;
  }
}

.top-text {
  margin-bottom: 50px;
  pointer-events: none;
  height: 153px;
  min-width: 1200px;
  img {
    position: absolute;
    right: 0;
  }
}
.login {
  width: 380px;
  padding: 40px 40px 30px;
  position: relative;
  display: inline-block;

  h2 {
    padding-left: 0;
    padding-bottom: 0;
    border-bottom: none;
    margin-bottom: 2.14rem;

    span {
      color: #646566;
      font-size: 1.33rem;
      margin-top: 0.71rem;
      display: block;
    }
  }

  .panel-form-item {
    padding-left: 0;
    padding-right: 0;
    .icon-style {
      margin-right: 14px;
      font-size: 20px;
      line-height: 50px;
    }
  }

  .more a:first-child::after {
    content: "";
    position: absolute;
    right: -5px;
    width: 1px;
    height: 8px;
    background: #dcdee0;
    top: 50%;
    transform: translateY(-50%);
  }
}

.code-img {
  width: 100%;
  height: 100%;
  // object-fit: cover;
}
</style>

<style lang="scss">
.system-layer .panel {
  background: rgba(255, 255, 255, 0.7);
  box-shadow: 0px 2px 20px 0px rgba(5, 38, 38, 0.15);
  width: 380px;
  padding: 40px 40px 30px;
  text-align: initial;
  text-align: center;
  border-radius: 0;

  img {
    width: 100px;
  }

  h2 {
    color: #323233;
    font-size: 1.4rem;
    margin-bottom: 2.14rem;
    font-weight: normal;
    padding-left: 0;
    border-bottom: 0;
    text-align: center;
  }

  .panel-form-item {
    position: relative;
    padding-bottom: 2.14rem;
    margin: 0;
    padding-left: 0;
    padding-right: 0;

    &.remember {
      padding: 0;
    }

    .err-info {
      position: absolute;
      top: 100%;
      left: 0;
      font-size: 1rem;
      line-height: 2.14rem;
      color: #fa5555;
    }
  }

  .more {
    text-align: center;

    a {
      color: #323233;
      line-height: 21px;
      font-size: 16px;
      margin: 0 5px;
      position: relative;
      text-decoration: none;
      cursor: pointer;
    }
  }
}

.panel-form-item .el-select {
  width: 100%;
}

.panel-form-item .el-button,
.panel-form-item .el-input__inner {
  height: 50px;
  font-size: 1.14rem;
}

.panel-form-item .el-button {
  line-height: 26px;
  font-weight: bold;
  font-size: 16px;
}

.panel-form-item .el-form-item__label {
  line-height: 50px;
}
.login .code-form-item .el-input {
  display: flex;
}

.login .code-form-item .el-input-group__append {
  flex: none;
  margin-left: 10px;
  width: 95px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.login .code-form-item .el-input__inner {
  flex: 1;
}
.login .code-form-item .el-input-group__append,
.login .code-form-item .el-input__inner {
  border-radius: 4px;
}
input[type="password"]::-ms-reveal {
  display: none;
}
</style>
