<template>
  <div v-if="hideLayout">
    <router-view />
  </div>
  <div v-else class="app-container">
    <Sidebar />
    <div class="main-content">
      <TopBar />
      <div class="content-area">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import Sidebar from '@/components/layout/Sidebar.vue'
import TopBar from '@/components/layout/TopBar.vue'

const route = useRoute()
const router = useRouter()

// 有时打开新标签页时，客户端路由解析可能在应用挂载后完成，
// 导致短暂渲染出默认布局再切换。这里在计算 hideLayout 时
// 额外使用 router.resolve(window.location.pathname) 来确保
// 初始加载时也能根据 URL 正确隐藏布局，避免闪烁。
const hideLayout = computed(() => {
  if (route.meta && route.meta.hideLayout === true) return true
  try {
    const resolved = router.resolve(window.location.pathname || window.location.href)
    return resolved && resolved.meta && resolved.meta.hideLayout === true
  } catch (e) {
    return false
  }
})
</script>

<style scoped>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
