<template>
  <div class="login-layer">
    <div class="content">
      <div class="info">
        <img src="@/assets/image/logo.png" alt="" />
        <h1  style="margin-bottom: 34px">登录案件</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.account"
            placeholder="请输入账号"
            @keydown.enter="submitClick"
          ></el-input>
        </el-form-item>
        <el-form-item class="panel-form-item" style="padding: 0">
          <p class="err-info">{{ verification.psw }}</p>
          <el-input
            v-model="form.password"
            :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="padding: 12px 0 34px 0">
            <el-checkbox v-model="form.remember" label="记住密码" size="large" />
          </el-form-item>
        <el-form-item class="panel-form-item" style="margin-top: 0px">
          <el-button color="#0960BD" 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 caseId = computed(() => {
  const caseId = router.currentRoute.value.params.caseId;
  if (caseId) {
    return Number(caseId);
  }
});
// 是否显示明文密码
const flag = ref(true);
// 表单
const form = reactive({
  account: localStorage.getItem("username") || "",
  password: localStorage.getItem("password") || "",
  code: "",
  remember: import.meta.env.DEV || localStorage.getItem("remember") === "1",
});
const verification = reactive({ phone: "", psw: "", code: "" });
// 验证
watch(
  form,
  () => {
    if (!form.account) {
      verification.phone = "请输入账号";
    } else {
      verification.phone = "";
    }
    if (!form.password) {
      verification.psw = "请输入密码";
    } else {
      verification.psw = "";
    }
  },
  { 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) {
    return openErrorMsg(verification.phone);
  }
  if (verification.psw) return openErrorMsg(verification.psw);
  if (verification.code) return openErrorMsg(verification.code);

  try {
    await login({ username: form.account, code: form.code, password: form.password, caseId: caseId.value });

    if (form.remember) {
      localStorage.setItem("username", form.account);
      localStorage.setItem("password", form.password);
      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.abstract });
    }
  } 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/criminalBanner.png") no-repeat center center;
  background-size: cover;
  position: inherit;

  .desc {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 30px;
    font-size: 14px;
    color: #999999;
    line-height: 30px;
    width: 100%;
    padding: 0 20px;
    text-align: center;
    letter-spacing: 1px;
  }
}
.content {
  width: 400px;
  height: 430px;
  margin: 0 auto;
  text-align: center;
  position: absolute;
  top: calc(50% - 215px);
  left: calc(50% - 200px);
  // 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;
  // display: inline-flex;
  img {
    width: 80px;
    height: 80px;
    margin: 0px auto 10px auto;
  }
  h1 {
    font-weight: bold;
    font-size: 32px;
    color: #000000;
    line-height: 48px;
    letter-spacing: 12px;
    text-align: center;
  }
}

.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>
.login-layer .el-input {
  --el-input-bg-color: #fff;
  /* 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;
}
</style>
