<script setup lang="ts">
import { computed, watchEffect } from 'vue';
import { useWindowSize } from '@vueuse/core';
import { AppMain, Navbar, Settings, TagsView } from './components/index';
import Sidebar from './components/Sidebar/index.vue';
import RightPanel from '@/components/RightPanel/index.vue';
import Hamburger from '@/components/Hamburger/index.vue';

import { useAppStore } from '@/store/modules/app';
import { useSettingsStore } from '@/store/modules/settings';

const { width } = useWindowSize();

/**
 * 响应式布局容器固定宽度
 *
 * 大屏（>=1200px）
 * 中屏（>=992px）
 * 小屏（>=768px）
 */
const WIDTH = 992;

const appStore = useAppStore();
const settingsStore = useSettingsStore();

const fixedHeader = computed(() => settingsStore.fixedHeader);
const showTagsView = computed(() => settingsStore.tagsView);
const showSettings = computed(() => settingsStore.showSettings);

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

watchEffect(() => {
  if (width.value < WIDTH) {
    appStore.toggleDevice('mobile');
    appStore.closeSideBar(true);
  } else {
    appStore.toggleDevice('desktop');

    if (width.value >= 1200) {
      //大屏
      appStore.openSideBar(true);
    } else {
      appStore.closeSideBar(true);
    }
  }
});

function handleOutsideClick() {
  appStore.closeSideBar(false);
}

function toggleSideBar() {
  appStore.toggleSidebar(true);
}

</script>

<template>
  <div :class="classObj" class="app-wrapper">
    <!-- 手机设备 && 侧边栏 → 显示遮罩层 -->
    <div
      class="drawer-bg"
      v-if="classObj.mobile && classObj.openSidebar"
      @click="handleOutsideClick"
    ></div>

    <Sidebar class="sidebar-container" />
    <navbar :class="{ 'fixed-header': fixedHeader }" />
    <div v-show="fixedHeader" style="height:60px"></div>
    <hamburger
      :is-active="appStore.sidebar.opened"
      @toggleClick="toggleSideBar"
    />
    <div :class="{ hasTagsView: showTagsView }" class="main-container">
      <tags-view v-if="showTagsView" />

      <!--主页面-->
      <app-main />

      <!-- 设置面板 -->
      <RightPanel v-if="showSettings">
        <settings />
      </RightPanel>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/mixin.scss';
@import '@/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: 55;
}

.fixed-header {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 98;
  width: 100%;
  transition: width 0.28s;
  background: #ffffff;
}
.hideSidebar .fixed-header {
  // width: calc(100% - 54px);
  width:100%;
}
.mobile .fixed-header {
  width: 100%;
}
</style>
