<template>
  <div class="flex h-screen w-full bg-slate-950 overflow-hidden">
    <!-- 全局导航侧边栏 -->
    <GlobalSidebar />

    <!-- 主体区域 -->
    <main class="flex-1 flex overflow-hidden relative">
      <!-- 主页 -->
      <HomePage v-if="currentPage === 'home'" />

      <!-- 持久化布局面板 -->
      <template v-else>
        <!-- 左侧/中间配置侧边栏 -->
        <WriterPanel v-if="currentPage === 'writer'" />
        <AnalysisPanel v-else-if="currentPage === 'analysis'" />
        <ParadigmWriterPanel v-else-if="currentPage === 'paradigmWriter'" />
        <ComparePanel v-else-if="currentPage === 'compare'" />
        <ArticleRewritePanel v-else-if="currentPage === 'rewrite'" />
        <MimicWriterPanel v-else-if="currentPage === 'mimicWriter'" />
        <DiffAnnotationPanel v-else-if="currentPage === 'diffAnnotation'" />
        <!-- 提纲写作页面 -->
        <template v-else-if="currentPage === 'outlineWriter'">
          <OutlineWriterPanel />
          <OutlineResultPanel />
        </template>
        <!-- 文章融合页面 -->
        <template v-else-if="currentPage === 'articleFusion'">
          <ArticleFusionPanel />
          <FusionResultPanel />
        </template>
        <!-- 文章修改页面 -->
        <ArticleEditorPanel v-else-if="currentPage === 'articleEditor'" />
        <DocumentsPanel 
          v-else-if="currentPage === 'documents'" 
          @toggle-version-panel="toggleVersionPanel"
          @document-selected="handleDocumentSelected"
        />
        <MaterialsPanel v-else-if="currentPage === 'materials'" />
        <SettingsPanel v-else-if="currentPage === 'settings'" />
        
        <!-- 右侧核心内容区（compare、rewrite、diffAnnotation、articleFusion、outlineWriter 和 articleEditor 页面使用自己的内部布局） -->
        <MainContent v-if="currentPage !== 'compare' && currentPage !== 'rewrite' && currentPage !== 'diffAnnotation' && currentPage !== 'articleFusion' && currentPage !== 'outlineWriter' && currentPage !== 'articleEditor'" />
        
        <!-- 侧滑浮层面板 (仅文稿页) -->
        <DocumentVersionPanel
          v-if="currentPage === 'documents'"
          :visible="showVersionPanel"
          :document-id="selectedDocumentId"
          :current-content="selectedDocumentContent"
          @close="showVersionPanel = false"
          @restore="handleVersionRestore"
        />
      </template>
    </main>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useAppStore } from './stores/app'
import GlobalSidebar from './components/layout/GlobalSidebar.vue'
import HomePage from './components/layout/HomePage.vue'
import WriterPanel from './components/panels/WriterPanel.vue'
import AnalysisPanel from './components/panels/AnalysisPanel.vue'
import DocumentsPanel from './components/panels/DocumentsPanel.vue'
import MaterialsPanel from './components/panels/MaterialsPanel.vue'
import SettingsPanel from './components/panels/SettingsPanel.vue'
import MainContent from './components/editors/MainContent.vue'
import ComparePanel from './components/panels/ComparePanel.vue'
import DocumentVersionPanel from './components/results/DocumentVersionPanel.vue'
import DiffAnnotationPanel from './components/panels/DiffAnnotationPanel.vue'
import ArticleRewritePanel from './components/panels/ArticleRewritePanel.vue'
import ParadigmWriterPanel from './components/panels/ParadigmWriterPanel.vue'
import MimicWriterPanel from './components/panels/MimicWriterPanel.vue'
import ArticleFusionPanel from './components/panels/ArticleFusionPanel.vue'
import FusionResultPanel from './components/results/FusionResultPanel.vue'
import OutlineWriterPanel from './components/panels/OutlineWriterPanel.vue'
import OutlineResultPanel from './components/results/OutlineResultPanel.vue'
import ArticleEditorPanel from './components/panels/ArticleEditorPanel.vue'

const appStore = useAppStore()
const currentPage = computed(() => appStore.currentPage)

// 版本历史面板状态
const showVersionPanel = ref(false)
const selectedDocumentId = ref(null)
const selectedDocumentContent = ref('')

// 切换版本面板
const toggleVersionPanel = () => {
  showVersionPanel.value = !showVersionPanel.value
}

// 处理文稿选择
const handleDocumentSelected = (doc) => {
  selectedDocumentId.value = doc?.id || null
  selectedDocumentContent.value = doc?.content || ''
}

// 处理版本恢复
const handleVersionRestore = (content) => {
  selectedDocumentContent.value = content
  // 刷新 MainContent 显示
  if (appStore.currentDocument) {
    appStore.currentDocument.content = content
  }
}
</script>

