<template>
  <div class="login-layout">
    <div class="login-content">
      <div class="header">
        <!-- <img src="/favicon.ico" /> -->
        <p>登录多元融合</p>
      </div>

      <div class="body">
        <ui-input
          type="text"
          placeholder="请输入账号"
          v-model="username"
          style="width: 100%"
        />
        <br />
        <ui-input
          :type="showPwd ? 'text' : 'password'"
          placeholder="请输入密码"
          v-model="password"
          style="width: 100%; margin-top: 20px"
        >
          <template #icon>
            <ui-icon
              style="cursor: pointer"
              class="ctrl-pwd"
              :type="showPwd ? 'eye-s' : 'eye-n'"
              @click="showPwd = !showPwd"
            ></ui-icon>
          </template>
        </ui-input>
        <br />
        <div class="code-row">
          <ui-input
            class="code-input"
            type="text"
            placeholder="请输入验证码"
            v-model="code"
          />
          <img :src="codeImg" class="code-img" @click="refer" />
        </div>
        <br />
        <ui-input
          type="checkbox"
          @click.stop
          label="记住密码"
          style="margin-top: 20px"
          :modelValue="mark"
          @update:modelValue="(select: any) => mark = select"
        />
      </div>
      <div class="bottom">
        <ui-button type="submit" @click="login(username, password, code)">登录</ui-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from "vue";
import { encodePwd } from "@/utils";
import GAxios from "axios";
import { setToken } from "@/api";
import { Message } from "bill/expose-common";
import { params } from "@/env";
import { GET_SETTING, getCode } from "@/api/constant";
import { currentLayout, RoutesName } from "@/router";

const username = ref(localStorage.getItem("fuse-username") || "");
const password = ref(localStorage.getItem("fuse-password") || "");
const code = ref("");
const mark = ref(!!localStorage.getItem("fuse-mark"));
const showPwd = ref(false);
const guid = () => {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    let r = (Math.random() * 16) | 0,
      v = c == "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
};
// 图片验证码
let baseURL = "https://test-mix3d.4dkankan.com";
const imgKey = ref(guid());
const refer = () => (imgKey.value = guid());
if (window.location.href.indexOf("localhost") !== -1) {
  baseURL = "https://test-mix3d.4dkankan.com";
} else if (window.location.href.indexOf("test") !== -1) {
  baseURL = "https://test-mix3d.4dkankan.com";
} else {
  baseURL = "https://mix3d.4dkankan.com";
}
const codeImg = computed(() => baseURL + getCode + "?key=" + imgKey.value);
const getDeptId = () => {
  const fromRoute = params.fromRoute;
  if (fromRoute === "fire") return "1";
  if (fromRoute === "cjzfire") return "DEP000011734134901809483776";
  if (fromRoute === "xmfire") return "3";
  if (fromRoute === "criminal") return "2";
  return "";
};
const login = (username: string, password: string, code: string) => {
  if (!username) {
    return Message.error("账号不能为空");
  }
  if (!password) {
    return Message.error("密码不能为空");
  }
  if (!code && !import.meta.env.DEV) {
    return Message.error("验证码不能为空");
  }

  const isView = [RoutesName.show, RoutesName.signModel, RoutesName.error].includes(
    currentLayout.value!
  );
  const type = isView ? "view" : "edit";
  const headers = { fusionId: params.caseId, "page-type": type };

  GAxios.post(
    "/fusion/fdLogin",
    {
      password: encodePwd(password),
      userName: username,
      phoneNum: username,
      code: code,
      deptId: getDeptId(),
    },
    { headers }
  ).then(async (res) => {
    if (res.data.code !== 0) {
      return Message.error(res.data.message);
    }
    const res1 = await GAxios.get(GET_SETTING, {
      params: { fusionId: params.caseId },
      headers: { ...headers, token: res.data.data.token },
    });
    if (res1.data.code === 40111) {
      return Message.error("您没有权限，请联系管理员开通");
    }
    setToken(res.data.data.token);

    setTimeout(() => {
      const p = new URLSearchParams(location.search);
      p.delete("token");
      location.search = "?" + p.toString();
      setTimeout(() => location.reload(), 100);
    }, 100);

    if (mark.value) {
      localStorage.setItem("fuse-username", username);
      localStorage.setItem("fuse-password", password);
      localStorage.setItem("fuse-mark", "1");
    } else {
      localStorage.removeItem("fuse-username");
      localStorage.removeItem("fuse-password");
      localStorage.removeItem("fuse-mark");
    }
  });
};

// if (import.meta.env.DEV) {
//   login("super-admin", "Aa123456", "");
// }
</script>

<style lang="scss" scoped>
.login-layout {
  position: fixed;
  inset: 0;
  z-index: 99;
  background: url("/images/login-backimage.png") no-repeat center center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-content {
  width: 400px;

  .header {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin-bottom: 64px;

    img {
      width: 80px;
      height: 80px;
      margin-bottom: 10px;
    }

    p {
      font-weight: bold;
      font-size: 32px;
      color: #000000;
      line-height: 48px;
      letter-spacing: 12px;
      text-align: center;
      font-style: normal;
      text-transform: none;
    }
  }

  .body {
    margin-bottom: 48px;
  }
}
</style>

<style lang="scss" scoped>
.login-content {
  .ui-input,
  .ui-button {
    --colors-color: #d9d9d9;
    --base-border-color: #d9d9d9;
    --colors-content-color: #000000;
    --colors-primary-base: #d9d9d9;
    --colors-normal-back: #ffffff;
    --colors-primary-base: #0960bd;
    --color-main-normal: #0960bd;
  }

  .ui-button {
    color: #fff !important;
    border: 1px solid var(--color-main-normal) !important;
    background: var(--color-main-normal) !important;
  }

  .code-row {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 20px;
  }

  .code-input {
    flex: 1;
  }

  .code-img {
    width: 120px;
    height: 40px;
    cursor: pointer;
    user-select: none;
    flex: none;
  }
}
</style>
