<template>
  <div class="fit">
    <q-ajax-bar position="top" color="accent" size="5px" skip-hijack />
    <q-menu context-menu />
    <router-view />
  </div>
</template>
<script lang="ts">
import { SessionStorage, Cookies, useQuasar } from "quasar";
import { defineComponent } from "vue";
import { useI18n } from "vue-i18n";
import EventBus, { EventNamesDefine } from "src/common/EventBus";
import { useStore } from "src/store";

import VConsole from "vconsole";

// 根据条件加载 vConsole
{
  if (window.location.href.indexOf("__showDebug__=_1025_") != -1) {
    new VConsole();
  }
}

export default defineComponent({
  name: "App",

  setup() {
    const $t = useI18n();
    const $store = useStore();
    const $q = useQuasar();

    document.title = $t.t("title");

    window.onresize = (evt: any) =>
      EventBus.getInstance().emit(EventNamesDefine.WindowResize, evt);

    window.onmousedown = (evt: MouseEvent) => {
      EventBus.getInstance().emit(EventNamesDefine.WindowMouseDown, evt);
    };
    window.onmousemove = (evt: any) => {
      EventBus.getInstance().emit(EventNamesDefine.WindowMouseMove, evt);
    };
    window.onmouseup = (evt: any) => {
      EventBus.getInstance().emit(EventNamesDefine.WindowMouseUp, evt);
    };
    window.onmouseout = (evt: any) => {
      EventBus.getInstance().emit(EventNamesDefine.WindowMouseOut, evt);
    };

    window.document.body.onclick = (evt: any) => {
      if ((<any>window).touchPriority) {
        return;
      }
      EventBus.getInstance().emit(EventNamesDefine.DocumentBodyClick, evt);
    };

    if ("ontouchstart" in document.documentElement === true) {
      window.document.body.ontouchstart = (evt: any) => {
        if ((<any>window).touchPriority) {
          EventBus.getInstance().emit(EventNamesDefine.DocumentBodyClick, evt);
        }
      };
    }

    document.body.classList.add("overflow-hidden");

    (window as any).controlLogout = () => {
      Cookies.remove("auto_login");
      SessionStorage.clear();
      window.location.reload();
    };

    const landspace = () => {
      let landspace = false;
      if ($q.platform.is.desktop) {
        landspace = window.innerWidth > innerHeight;
      } else {
        landspace = window.screen.width > window.screen.height;
      }
      if (Math.abs(window.orientation) == 90) {
        landspace = !landspace;
      }
      return landspace;
    };
    (<any>window).landspace = landspace;

    window.onorientationchange = () => {
      $store.commit("updateLandspace", landspace());
    };

    $store.commit("updateLandspace", landspace());

    return {};
  },
});
</script>
