<template>
  <div class="notebook-view" v-if="notebook">
    <!-- Title bar -->
    <div class="notebook-title-bar">
      <BookOutlined class="title-bar-icon" />
      <span
        v-if="!editingTitle"
        class="notebook-title"
        @click="startEditTitle"
      >
        {{ notebook.title }}
        <EditOutlined class="title-edit-icon" />
      </span>
      <a-input
        v-else
        v-model:value="titleDraft"
        class="title-input"
        size="small"
        @blur="saveTitle"
        @keydown.enter="saveTitle"
        ref="titleInputRef"
      />
    </div>

    <!-- Three-panel layout -->
    <div class="notebook-layout">
      <!-- Left: Sources -->
      <div class="panel panel-left">
        <SourcePanel :notebook-id="notebookId" />
      </div>

      <!-- Center: Chat -->
      <div class="panel panel-center">
        <ChatPanel :notebook-id="notebookId" />
      </div>

      <!-- Right: Outputs -->
      <div class="panel panel-right">
        <OutputPanel :notebook-id="notebookId" />
      </div>
    </div>
  </div>

  <div v-else-if="loadError" class="notebook-error">
    <a-result status="404" title="专题写稿不存在" sub-title="该专题写稿可能已被删除">
      <template #extra>
        <a-button type="primary" @click="$router.push('/notebook')">返回列表</a-button>
      </template>
    </a-result>
  </div>

  <div v-else class="notebook-loading">
    <a-spin size="large" />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch, nextTick } from 'vue'
import { useRoute } from 'vue-router'
import { EditOutlined, BookOutlined } from '@ant-design/icons-vue'
import { useNotebookStore } from '@/stores/notebook'
import SourcePanel from '@/components/notebook/SourcePanel.vue'
import ChatPanel from '@/components/notebook/ChatPanel.vue'
import OutputPanel from '@/components/notebook/OutputPanel.vue'

const route = useRoute()
const store = useNotebookStore()

const notebookId = computed(() => route.params.id as string)
const notebook = computed(() => store.currentNotebook)
const loadError = ref(false)

const editingTitle = ref(false)
const titleDraft = ref('')
const titleInputRef = ref<any>(null)

function startEditTitle() {
  titleDraft.value = notebook.value?.title || ''
  editingTitle.value = true
  nextTick(() => {
    titleInputRef.value?.focus()
  })
}

async function saveTitle() {
  editingTitle.value = false
  const newTitle = titleDraft.value.trim()
  if (newTitle && notebook.value && newTitle !== notebook.value.title) {
    await store.updateNotebook(notebookId.value, { title: newTitle })
  }
}

async function loadNotebook(id: string) {
  loadError.value = false
  try {
    await store.loadNotebook(id)
  } catch {
    loadError.value = true
  }
}

onMounted(() => {
  if (notebookId.value) {
    loadNotebook(notebookId.value)
  }
})

watch(notebookId, (newId) => {
  if (newId) loadNotebook(newId)
})
</script>

<style scoped lang="less">
.notebook-view {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.notebook-title-bar {
  padding: 8px 16px;
  border-bottom: 1px solid #f0f0f0;
  background: #fff;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
}

.title-bar-icon {
  font-size: 18px;
  color: #1890ff;
}

.notebook-title {
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;

  .title-edit-icon {
    font-size: 13px;
    color: #bfbfbf;
    opacity: 0;
    transition: opacity 0.2s;
  }

  &:hover .title-edit-icon {
    opacity: 1;
  }
}

.title-input {
  max-width: 400px;
  font-size: 16px;
  font-weight: 600;
}

.notebook-layout {
  flex: 1;
  display: flex;
  height: 100%;
  overflow: hidden;
}

.panel {
  display: flex;
  flex-direction: column;
  border-right: 1px solid #f0f0f0;
  overflow: hidden;

  &:last-child {
    border-right: none;
  }
}

.panel-left {
  width: 280px;
  min-width: 240px;
  background: #fff;
}

.panel-center {
  flex: 1;
  min-width: 400px;
}

.panel-right {
  width: 360px;
  min-width: 300px;
  background: #fff;
}

.notebook-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.notebook-error {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
</style>
