<template>
  <div class="login-layer">
    <div class="content">
      <div class="info">
        <img src="@/assets/image/police.ico" alt="" />
        <h1>{{ appConstant.title }}</h1>
      </div>
      <el-form class="panel login" :model="form" @submit.stop>
        <h2>欢 迎 登 录</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: 10px 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" style="margin-top: 18px">
          <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>

    <p class="desc">
      公安部鉴定中心 & 珠海市四维时代网络科技有限公司 |
      公安部科技强警基础工作计划（2022JC13）
    </p>
  </div>
</template>

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

// 是否显示明文密码
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 });
    }
  } catch (e) {
    console.error(e);
    return refer();
  }
};
</script>

<style lang="sass">
@import "./style.scss"
</style>

<style lang="scss" scoped>
.login-layer {
  width: 100%;
  height: 100%;
  background: #eceff2 url("@/assets/image/policeBanner.png") no-repeat left bottom;
  background-size: cover;
  position: inherit;

  .desc {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 30px;
    font-size: 14px;
    color: #999999;
    line-height: 30px;
    letter-spacing: 1px;
  }
}
.content {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  height: 100%;
}
.info {
  color: #fff;
  margin-right: 143px;
  padding-top: 40px;
  padding-left: 44px;
  flex: none;
  text-align: left;
  display: flex;
  align-items: center;

  img {
    width: 40px;
    height: 40px;
    margin-right: 20px;
  }
  h1 {
    font-size: 2.2rem;
  }
}

.login {
  width: 400px;
  margin-right: 12.5%;
  position: relative;
  display: inline-block;
  background: none;
  box-shadow: none;
  align-self: center;

  h2 {
    padding-left: 0;
    padding-bottom: 0;
    border-bottom: none;
    margin-bottom: 2.14rem;
    text-align: center;
    font-weight: bold;
    font-size: 36px;

    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 {
  height: 40px;
}
</style>

<style lang="scss">
.login-layer .el-input {
  --el-input-bg-color: #f6f8fb;
  /* var(--el-fill-color-blank) */
}
.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;
}

.login {
  .panel-form-item .el-button,
  .panel-form-item .el-input__inner {
    height: 40px !important;
    font-size: 1.14rem;
  }
  .panel-form-item .el-form-item__label {
    line-height: 40px !important;
  }
}
</style>
