<template>
  <div :class="classObj" class="app-wrapper">
    <div v-if="device === 'mobile' && sidebar.opened" class="drawer-bg" @click="handleClickOutside" />

    <sidebar v-if="settingsStore.navigationType !== 'top-menu-type'" class="sidebar-container" />

    <navbar v-if="settingsStore.navigationType === 'top-menuMix-type' || settingsStore.navigationType === 'top-menu-type'" @setLayout="setLayout" />

    <div
      :class="{ hasTagsView: needTagsView, sidebarHide: sidebar.hide }"
      class="main-container"
      :style="{
        marginLeft: settingsStore.navigationType === 'top-menu-type' ? '0 !important' : '',
        height: `calc(100% - ${settingsStore.setMainHeight()})`,
        minHeight: `calc(100% - ${settingsStore.setMainHeight()})`,
      }"
    >
      <navbar v-if="settingsStore.navigationType === 'left-menu-type'" @setLayout="setLayout" />
      <tags-view v-if="needTagsView" />
      <app-main />
      <settings ref="settingRef" />
    </div>
  </div>
</template>

<script setup>
/*----------------------------------依赖引入-----------------------------------*/
import { useWindowSize } from "@vueuse/core";
/*----------------------------------接口引入-----------------------------------*/
/*----------------------------------store引入-----------------------------------*/
import { useSettingsStore, useAppStore } from "@/store/modules/index";
/*----------------------------------组件引入-----------------------------------*/
import Sidebar from "./components/Sidebar/index.vue";
import { AppMain, Navbar, Settings, TagsView } from "./components";
/*----------------------------------公共方法引入-----------------------------------*/
const settingsStore = useSettingsStore();

/*----------------------------------变量声明-----------------------------------*/
const theme = computed(() => settingsStore.theme);
const sideTheme = computed(() => settingsStore.sideTheme); //主题风格设置
const navigationType = ref(settingsStore.navigationType); //导航栏模式
const needTagsView = computed(() => settingsStore.tagsView); //导航面包屑
const sidebar = computed(() => useAppStore().sidebar);
const device = computed(() => useAppStore().device);

const { width, height } = useWindowSize();
const WIDTH = 992; // refer to Bootstrap's responsive design

const classObj = computed(() => ({
  hideSidebar: !sidebar.value.opened,
  openSidebar: sidebar.value.opened,
  withoutAnimation: sidebar.value.withoutAnimation,
  mobile: device.value === "mobile",
}));

watchEffect(() => {
  if (device.value === "mobile" && sidebar.value.opened) {
    useAppStore().closeSideBar({ withoutAnimation: false });
  }
  if (width.value - 1 < WIDTH) {
    useAppStore().toggleDevice("mobile");
    useAppStore().closeSideBar({ withoutAnimation: true });
  } else {
    useAppStore().toggleDevice("desktop");
  }
});

function handleClickOutside() {
  useAppStore().closeSideBar({ withoutAnimation: false });
}

const settingRef = ref(null);
function setLayout() {
  settingRef.value.openSetting();
}
</script>

<style lang="scss" scoped>
@import "@/assets/styles/mixin.scss";
@import "@/assets/styles/variables.module.scss";

.app-wrapper {
  @include clearfix;
  position: relative;
  height: 100%;
  width: 100%;

  &.mobile.openSidebar {
    position: fixed;
    top: 0;
  }
}

.drawer-bg {
  background: #000;
  opacity: 0.3;
  width: 100%;
  top: 0;
  height: 100%;
  position: absolute;
  z-index: 999;
}

.fixed-header {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 9;
  width: calc(100% - #{$base-sidebar-width});
  transition: width 0.28s;
}

.hideSidebar .fixed-header {
  width: calc(100% - 54px);
}

.sidebarHide .fixed-header {
  width: 100%;
}

.mobile .fixed-header {
  width: 100%;
}
</style>
