<template>
  <div :class="classObj" class="app-wrapper" :style="{ '--current-color': theme }">
    <div v-if="device === 'mobile' && sidebar.opened" class="drawer-bg" @click="handleClickOutside"/>
    <sidebar v-if="!sidebar.hide" class="sidebar-container" />
    <div :class="{ hasTagsView: needTagsView, sidebarHide: sidebar.hide }" class="main-container">
      <!-- 头部区域始终固定 -->
      <div class="header-wrapper">
        <navbar @setLayout="setLayout" />
        <tags-view v-if="needTagsView" />
      </div>
      <!-- 主内容区域可滚动 -->
      <div class="content-wrapper">
        <app-main />
      </div>
      <settings ref="settingRef" />
    </div>
  </div>
</template>

<script setup>
import { useWindowSize } from '@vueuse/core'
import Sidebar from './components/Sidebar/index.vue'
import { AppMain, Navbar, Settings, TagsView } from './components'
import defaultSettings from '@/settings'

import useAppStore from '@/store/modules/app'
import useSettingsStore from '@/store/modules/settings'

const settingsStore = useSettingsStore()
const theme = computed(() => settingsStore.theme);
const sideTheme = computed(() => settingsStore.sideTheme);
const sidebar = computed(() => useAppStore().sidebar);
const device = computed(() => useAppStore().device);
const needTagsView = computed(() => settingsStore.tagsView);
const fixedHeader = computed(() => settingsStore.fixedHeader);

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

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

watch(() => device.value, () => {
  if (device.value === 'mobile' && sidebar.value.opened) {
    useAppStore().closeSideBar({ withoutAnimation: false })
  }
})

watchEffect(() => {
  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();
}
// 计算头部实际高度
const headerHeight = ref(0);

onMounted(() => {
  nextTick(() => {
    const headerEl = document.querySelector('.header-wrapper');
    if (headerEl) {
      headerHeight.value = headerEl.offsetHeight;
    }
  });
});

// 监听 needTagsView 变化重新计算高度
watch(needTagsView, () => {
  nextTick(() => {
    const headerEl = document.querySelector('.header-wrapper');
    if (headerEl) {
      headerHeight.value = headerEl.offsetHeight;
    }
  });
});
</script>

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

.app-wrapper {
  @include clearfix;
  position: relative;
  height: 100vh; // 使用视口高度
  width: 100%;
  overflow: hidden; // 防止整体页面滚动

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

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

.main-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  margin-left: $base-sidebar-width;
  transition: margin-left 0.28s;

  &.sidebarHide {
    margin-left: 0;
  }
}

// 头部固定区域
.header-wrapper {
  position: sticky;
  top: 0;
  z-index: 1000;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  flex-shrink: 0; // 防止被压缩
}

// 内容可滚动区域
.content-wrapper {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  height: 0; // 配合 flex: 1 使用
}

// 侧边栏隐藏时的样式调整
.hideSidebar .main-container {
  margin-left: 54px;
}

.sidebarHide .main-container {
  margin-left: 0;
}

// 移动端样式
.mobile .main-container {
  margin-left: 0;
}

// 如果你仍然需要原来的 fixed-header 功能
.fixed-header .header-wrapper {
  position: fixed;
  top: 0;
  right: 0;
  width: calc(100% - #{$base-sidebar-width});
  z-index: 1000;
}

.fixed-header .content-wrapper {
  padding-top: 84px; // 根据实际头部高度调整
}

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

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

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