<template>
  <view :class="['page-wrapper', { dark: isDark }]">
    <cwg-match-media :min-width="991" v-if="!isLoginPage">
      <view class="left-sidebar">
        <cwg-sidebar />
        <cwg-submenu v-if="sidebarVisible" @submenu-click="onSubmenuClick" />
      </view>
    </cwg-match-media>
    <cwg-progress />
    <view class="page-content">
      <cwg-match-media :min-width="991" v-if="!isLoginPage">
        <cwg-pc-header @toggle-sidebar="toggleSidebar" />
      </cwg-match-media>
      <cwg-match-media :max-width="991">
        <cwg-header v-if="!isHeaderFixed" />
      </cwg-match-media>
      <view class="content-wrapper">
        <slot />
      </view>
      <view :style="{ height: isTabBarPage ? '60px' : '0px' }" />
    </view>
    <cwg-match-media :max-width="991">
      <cwg-tab-bar v-model:isTabBarPage="isTabBarPage" />
    </cwg-match-media>
  </view>
</template>

<script setup lang="ts">
import { ref, watch, computed } from "vue";
import { onLoad, onShow } from '@dcloudio/uni-app'
import { updateRoute } from '@/hooks/useRoute'
import useGlobalStore from '@/stores/use-global-store'
const globalStore = useGlobalStore()
interface MenuItem {
  key: string;
  label: string;
  icon?: string;
  children?: MenuItem[];
}

const props = defineProps({
  // 是否固定顶部导航栏
  isHeaderFixed: {
    type: Boolean,
    default: false
  },
  // 是否登录页，登录页不显示侧边栏和顶部导航,注册忘记密码等页面
  isLoginPage: {
    type: Boolean,
    default: false
  }
})
const isDark = computed(() => globalStore.theme === 'dark')
const isTabBarPage = ref(false)


const sidebarVisible = ref(true)

function toggleSidebar() {
  sidebarVisible.value = !sidebarVisible.value;
}

onLoad(() => {
  updateRoute()
})

onShow(() => {
  updateRoute()
})

</script>

<style lang="scss" scoped>
@import "@/uni.scss";

.page-wrapper {
  display: flex;
  flex-direction: row;
  height: 100vh;
  overflow: hidden;
}

.left-sidebar {
  display: flex;
  flex-direction: row;
}

.page-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.content-wrapper {
  flex: 1;
  // padding: 0 px2rpx(24) px2rpx(0) px2rpx(24);
  box-sizing: border-box;
  overflow-y: auto;
}
</style>
