<template>
  <!-- fake fields are a workaround for chrome autofill getting the wrong fields -->
  <input style="display: none" type="text" name="fakeusernameremembered" />
  <input style="display: none" type="password" name="fakepasswordremembered" />
  <syTraceLoginPage
    :set-pwd="setPwd"
    :handle-login="handleLogin"
    :send-verification-code="sendVerificationCode"
    :logo-url="logo"
  ></syTraceLoginPage>
</template>
<script lang="ts" setup>
import { syTraceLoginPage, GO_URLS } from "cqk-sy-ui";
import { ElMessage } from "element-plus";
import { $ajax } from "@/service/index";
import * as storage from "@/service/storage";
import md5 from "js-md5";
import { router } from "@/router";

const logo = import.meta.env.VITE_loginIconSrc as string;

const setPwd: any = async (
  setPwdType: string,
  accountType: string,
  account: any,
  code: any,
  pwd: string | any[]
) => {
  let data = {};
  if (accountType === "email") {
    data = {
      email: account,
      pwd: md5(String(pwd) + md5(String(pwd.length))),
      code: code,
      code_type: 1,
    };
  } else {
    data = {
      phone: account,
      pwd: md5(String(pwd) + md5(String(pwd.length))),
      code: code,
      code_type: 0,
    };
  }
  const res = await $ajax({
    type: "post",
    url: GO_URLS.setPwd,
    params: data,
  });

  if (res?.data) {
    var message = "";
    if (setPwdType === "setPwd") {
      message = "密码设置成功";
    } else if (setPwdType === "resetPwd") {
      message = "密码重置成功";
    }
    ElMessage({
      message: message,
      type: "success",
    });
    return true;
  } else {
    return res?.data.msg;
  }
};
const handleLogin: any = async (
  accountType: string,
  account: any,
  verificationMethod: string,
  verificationStr: any,
  remember: any,
  setError: any
) => {
  if (accountType === "email") {
    let params = {};
    if (verificationMethod === "code") {
      params = {
        login_type: 3,
        email: account,
        code: verificationStr,
      };
    } else {
      params = {
        login_type: 2,
        email: account,
        pwd: md5(String(verificationStr) + md5(String(verificationStr.length))),
      };
    }
    const res = await $ajax({
      type: "post",
      url: GO_URLS.login,
      params: params,
    });

    if (res && res.data) {
      sessionStorage.setItem("login_type", "2");
      if (accountType === "email") {
        storage.setLogin(
          res.data,
          {
            login_type: 2,
            email: account,
            pwd: verificationMethod,
          },
          remember || false
        );
      }
      router.push({ path: "/tracingManagement" });
      return true;
    } else {
      return res?.data.msg;
    }
  } else if (accountType === "phone") {
    let params = {};
    if (verificationMethod === "code") {
      params = {
        login_type: 1,
        phone: account,
        code: verificationStr,
      };
    } else {
      params = {
        login_type: 0,
        phone: account,
        pwd: md5(String(verificationStr) + md5(String(verificationStr.length))),
      };
    }
    const res = await $ajax({
      type: "post",
      url: GO_URLS.login,
      params: params,
    });

    if (res && res.data) {
      sessionStorage.setItem("login_type", "1");
      storage.setLogin(
        res.data,
        {
          login_type: 0,
          phone: account,
          pwd: verificationStr,
        },
        remember || false
      );
      router.push({ path: "/tracingManagement" });
      return true;
    } else {
      ElMessage.error(res?.msg);
      return res?.data.msg;
    }
  } else {
    return false;
  }
};

const sendVerificationCode = async (
  accountType: string,
  account: any,
  usedFor: string
) => {
  let res = null as any;
  if (usedFor === "login") {
    if (accountType === "phone") {
      res = await $ajax({
        type: "post",
        url: GO_URLS.sendSms,
        params: {
          phone: account,
          template_id: 0,
        },
      });
    } else if (accountType === "email") {
      res = await $ajax({
        url: GO_URLS.sendEmail,
        type: "post",
        params: {
          email: account,
          template_id: 0,
        },
      });
    }
  } else if (usedFor === "setpwd") {
    if (accountType === "phone") {
      res = await $ajax({
        type: "post",
        url: GO_URLS.sendSms,
        params: {
          phone: account,
          template_id: 1,
        },
      });
    } else if (accountType === "email") {
      res = await $ajax({
        url: GO_URLS.sendEmail,
        type: "post",
        params: {
          email: account,
          template_id: 1,
        },
      });
    }
  }
  if (res) {
    ElMessage({
      message: "短信已发送",
      type: "success",
    });
    return true;
  } else return false;
};
</script>
