import { h } from "vue";
import { ElMessageBox } from "element-plus";
import { useUserStoreHook } from "/@/store/modules/user";
import router from "/@/router";
import { removeAction, removeRelaComNo, removeToken } from "/@/utils/auth";
import { storageSession } from "@pureadmin/utils";

const toWelcomePage = () => {
  const isWelcomePage = window.location.hash.includes("#/welcome");
  if (!isWelcomePage) router.push("/welcome");
};

function roleException() {
  const company = useUserStoreHook();
  const { currentCompany } = company;

  setTimeout(() => {
    ElMessageBox({
      title: "公司角色异常",
      message: h("p", null, [
        h("span", null, "公司 "),
        h("strong", null, currentCompany ? currentCompany.companyName : ""),
        h("span", null, " 角色异常或该公司禁用,请检查账号或者角色设置")
      ]),
      boxType: "confirm",
      callback: () => toWelcomePage()
    });
  }, 500);
}

function userExcption() {
  ElMessageBox({
    title: "账号异常",
    message: h("p", null, [
      h("span", null, " 角色异常或该公司禁用,请检查账号或者角色设置")
    ]),
    boxType: "confirm",
    callback: () => toWelcomePage()
  });
}

function userLogout() {
  const userStore = useUserStoreHook();
  userStore.token = "";
  userStore.name = "";
  userStore.level = "0";
  removeToken();
  removeAction();
  storageSession.clear();
  this.currentComapny = "";
  this.companyList = [];
  removeRelaComNo();
  router.push("/login");
}

const TriggerMap = {
  roleException,
  userExcption,
  userLogout
} as const;

type TriggerMapKey = keyof typeof TriggerMap;

export const trigger = (type: TriggerMapKey) => TriggerMap[type]();
