<template>
  <div class="knowledge">
    <!-- 页面头部 -->
    <header class="page-header">
      <h1>{{ $t('knowledge.title') }}</h1>
      <div class="header-actions">
        <button class="btn btn-primary" @click="openCreateModal">{{ $t('knowledge.add') }}</button>
      </div>
    </header>

    <!-- 错误条:消费 store.error(加载/检索/配置失败对用户可见) -->
    <div v-if="store.error" class="error-banner">
      <span class="error-text">{{ store.error }}</span>
      <button class="error-dismiss" @click="clearError">✕</button>
    </div>

    <!-- 顶层 Tab: 知识库 / 审核收件箱 -->
    <div class="top-tabs">
      <button class="top-tab" :class="{ active: topTab === 'library' }" @click="switchTopTab('library')">
        {{ $t('knowledge.tab.library') }}
      </button>
      <button class="top-tab" :class="{ active: topTab === 'inbox' }" @click="switchTopTab('inbox')">
        {{ $t('knowledge.tab.inbox') }}
        <span v-if="store.candidates.length" class="inbox-badge">{{ store.candidates.length }}</span>
      </button>
    </div>

    <!-- 左右分栏: 列表 + 详情 -->
    <div class="kn-layout">
      <!-- ===== 左侧:列表 ===== -->
      <div class="kn-list-panel">
        <!-- 搜索栏(仅知识库) -->
        <div v-if="topTab === 'library'" class="search-bar">
          <input
            v-model="searchQuery"
            type="text"
            class="search-input"
            :placeholder="$t('knowledge.searchPlaceholder')"
            @input="onSearchInput"
          />
        </div>

        <!-- 分类(仅知识库) -->
        <div v-if="topTab === 'library'" class="category-tabs">
          <button
            v-for="cat in categories"
            :key="cat.key"
            class="cat-chip"
            :class="{ 'cat-active': activeKind === cat.key }"
            @click="activeKind = cat.key"
          >
            <span>{{ cat.icon }}</span>
            <span>{{ catLabel(cat.key) }}</span>
            <span class="cat-count">{{ getCategoryCount(cat.key) }}</span>
          </button>
        </div>

        <!-- 卡片列表 -->
        <div class="list-cards">
          <div
            v-for="item in listItems"
            :key="item.id"
            class="kn-card"
            :class="{ selected: selectedId === item.id }"
            @click="selectKnowledge(item.id)"
          >
            <div class="kn-card-head">
              <span class="kn-card-title">{{ item.title }}</span>
              <span class="kn-card-status" :class="'st-' + item.status">{{ statusLabel(item.status) }}</span>
            </div>
            <div class="kn-card-desc">{{ stripMd(item.content) }}</div>
            <div class="kn-card-meta">
              <span class="meta-kind">{{ kindLabel(item.kind) }}</span>
              <span v-if="item.confidence" :class="'conf-' + item.confidence">{{ confidenceLabel(item.confidence) }}</span>
              <span class="meta-reuse">🔄 {{ item.reuse_count }}</span>
            </div>
          </div>

          <div v-if="listItems.length === 0" class="list-empty">
            <div>{{ topTab === 'inbox' ? $t('knowledge.emptyInbox') : $t('knowledge.emptyLibrary') }}</div>
          </div>
        </div>
      </div>

      <!-- ===== 右侧:详情(KnowledgeDetail 子组件,SW-260618-20 拆分)===== -->
      <div class="kn-detail-panel">
        <KnowledgeDetail
          v-if="detail"
          :detail="detail"
          :top-tab="topTab"
          :submitting="submitting"
          @save="onDetailSave"
          @publish="publishCurrent"
          @reject="rejectCurrent"
          @archive="archiveCurrent"
        />

        <div v-else class="detail-empty">
          <div class="detail-empty-icon">👈</div>
          <div>{{ $t('knowledge.detailEmptyHint') }}</div>
        </div>
      </div>
    </div>

    <!-- ===== 新增知识对话框 ===== -->
    <div v-if="showCreateModal" class="modal-overlay" @click.self="closeCreateModal">
      <div class="modal-box">
        <div class="modal-title">{{ $t('knowledge.createTitle') }}</div>
        <div class="form-row">
          <label>{{ $t('knowledge.kindLabel') }}</label>
          <select v-model="form.kind" class="form-select">
            <option v-for="k in KNOWLEDGE_KINDS" :key="k.key" :value="k.key">{{ k.icon }} {{ kindText(k.key) }}</option>
          </select>
        </div>
        <div class="form-row">
          <label>{{ $t('knowledge.titleLabel') }}</label>
          <input v-model="form.title" type="text" class="form-input" :placeholder="$t('knowledge.titlePlaceholder')" />
        </div>
        <div class="form-row">
          <label>{{ $t('knowledge.contentLabel') }}</label>
          <textarea v-model="form.content" class="form-input form-textarea" :placeholder="$t('knowledge.contentPlaceholder')"></textarea>
        </div>
        <div class="form-row">
          <label>{{ $t('knowledge.tagsFieldLabel') }}</label>
          <input v-model="form.tagsInput" type="text" class="form-input" :placeholder="$t('knowledge.tagsFieldPlaceholder')" />
        </div>
        <div class="form-row">
          <label>{{ $t('knowledge.confidenceLabel') }}</label>
          <select v-model="form.confidence" class="form-select">
            <option value="">{{ $t('knowledge.confidenceNone') }}</option>
            <option value="high">{{ $t('knowledge.confidence.high') }}</option>
            <option value="medium">{{ $t('knowledge.confidence.medium') }}</option>
            <option value="low">{{ $t('knowledge.confidence.low') }}</option>
          </select>
        </div>
        <div class="modal-actions">
          <button class="btn btn-ghost" @click="closeCreateModal">{{ $t('common.cancel') }}</button>
          <button class="btn btn-primary" @click="submitCreate" :disabled="submitting || !form.title.trim()">{{ $t('knowledge.create') }}</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import {
  useKnowledgeStore,
  KNOWLEDGE_KINDS,
  kindText,
  kindLabel,
  knowledgeStatusLabel as statusLabel,
  knowledgeConfidenceLabel as confidenceLabel,
} from '@/stores/knowledge'
import { useRendered } from '@/composables/useMarkdown'
import { stripMd } from '@/utils/markdown'
import KnowledgeDetail from '@/components/knowledge/KnowledgeDetail.vue'
import type { KnowledgeDetailPayload } from '@/api/types'

const { t } = useI18n()
const store = useKnowledgeStore()

// 顶层 Tab: library(知识库) | inbox(审核收件箱)
const topTab = ref<'library' | 'inbox'>('library')

const categories = [
  { key: 'all', icon: '📦' },
  ...KNOWLEDGE_KINDS.map(k => ({ key: k.key, icon: k.icon })),
]
// 分类 chip 的展示文案:"全部" 走 categoryAll,其余走 knowledge.kind.<key>
function catLabel(key: string): string {
  return key === 'all' ? t('knowledge.categoryAll') : kindText(key)
}
const activeKind = ref('all')
const searchQuery = ref('')
let searchTimer: ReturnType<typeof setTimeout> | null = null

function onSearchInput() {
  if (searchTimer) clearTimeout(searchTimer)
  searchTimer = setTimeout(() => {
    if (searchQuery.value.trim()) {
      store.search(searchQuery.value)
    } else {
      store.loadList()
    }
  }, 300)
}

// 组件卸载清搜索 debounce timer:防卸载后 300ms 内 timer 触发,向单例 store 写入
// 已离开视图的搜索结果/列表(污染下次进入时的列表态)。对齐 Settings.vue toast timer 清理模式。
onUnmounted(() => {
  if (searchTimer) {
    clearTimeout(searchTimer)
    searchTimer = null
  }
})

function switchTopTab(tab: 'library' | 'inbox') {
  topTab.value = tab
  selectedId.value = null
  detail.value = null
  if (tab === 'inbox') {
    store.loadCandidates()
  } else {
    store.loadList()
  }
}

// 关闭错误条
function clearError() {
  store.clearError()
}

// 左侧列表(随 Tab 切换数据源 + 知识库的分类过滤)
const listItems = computed(() => {
  const src = topTab.value === 'inbox' ? store.candidates : store.items
  if (topTab.value === 'library' && activeKind.value !== 'all') {
    return src.filter(i => i.kind === activeKind.value)
  }
  return src
})

function getCategoryCount(key: string): number {
  if (key === 'all') return store.items.length
  return store.items.filter(i => i.kind === key).length
}

// ===== 详情选中 + 加载 =====
const selectedId = ref<string | null>(null)
const detail = ref<KnowledgeDetailPayload | null>(null)
// B-260615-25:Markdown 渲染器预热保留在父级(模块单例,与 AiChat/TaskDetail/KnowledgeDetail 共享),
// 详情内容渲染由 KnowledgeDetail 子组件通过 useRendered 消费,此处仅 ensureLoaded 预热不阻塞首屏
const { ensureLoaded } = useRendered(() => '')

async function selectKnowledge(id: string) {
  selectedId.value = id
  try {
    detail.value = await store.getDetail(id)
  } catch (e: any) {
    console.error('加载详情失败', e)
  }
}

// 列表数据变化时(如刷新),若选中项已被移除则清空详情
watch(listItems, items => {
  if (selectedId.value && !items.some(i => i.id === selectedId.value)) {
    selectedId.value = null
    detail.value = null
  }
})

// ===== 编辑(详情保存由 KnowledgeDetail 子组件 emit('save') 回传)=====
// 异步操作禁用态(IPC 期间防双击重复提交):onDetailSave/publishCurrent/rejectCurrent/archiveCurrent/submitCreate 共用
const submitting = ref(false)

// 子组件已完成表单校验(title 非空 + tags 解析),父级只负责 IPC + 刷新详情
async function onDetailSave(payload: { id: string; data: { title: string; content: string; tags: string; confidence: string } }) {
  if (!detail.value) return
  submitting.value = true
  try {
    await store.update(payload.id, payload.data)
    // 重新拉详情
    await selectKnowledge(payload.id)
  } finally {
    submitting.value = false
  }
}

// ===== 审核操作(收件箱) =====
async function publishCurrent() {
  if (!detail.value) return
  submitting.value = true
  try {
    await store.updateStatus(detail.value.knowledge.id, 'published')
    selectedId.value = null
    detail.value = null
  } finally {
    submitting.value = false
  }
}

async function rejectCurrent() {
  if (!detail.value) return
  submitting.value = true
  try {
    await store.archive(detail.value.knowledge.id)
    selectedId.value = null
    detail.value = null
  } finally {
    submitting.value = false
  }
}

async function archiveCurrent() {
  if (!detail.value) return
  submitting.value = true
  try {
    await store.archive(detail.value.knowledge.id)
    selectedId.value = null
    detail.value = null
  } finally {
    submitting.value = false
  }
}

// ===== 新增对话框 =====
const showCreateModal = ref(false)
const form = ref({ kind: 'pitfall', title: '', content: '', tagsInput: '', confidence: '' })

function openCreateModal() {
  form.value = { kind: 'pitfall', title: '', content: '', tagsInput: '', confidence: '' }
  showCreateModal.value = true
}
function closeCreateModal() {
  showCreateModal.value = false
}
async function submitCreate() {
  submitting.value = true
  try {
    const tags = form.value.tagsInput.split(',').map(t => t.trim()).filter(Boolean)
    await store.create({
      kind: form.value.kind,
      title: form.value.title.trim(),
      content: form.value.content.trim(),
      tags: JSON.stringify(tags),
      confidence: form.value.confidence || undefined,
    })
    showCreateModal.value = false
    switchTopTab('inbox')
  } finally {
    submitting.value = false
  }
}

// ===== 列表卡片标签辅助(详情相关 label/时间解析已迁移至 KnowledgeDetail 子组件)=====
// kindText/kindLabel/statusLabel/confidenceLabel 由 @/stores/knowledge 提供(列表/详情共用,单一来源)

onMounted(() => {
  ensureLoaded() // 后台预热 Markdown 渲染器(模块单例,与 AiChat/TaskDetail 共享),不阻塞
  store.loadList()
  store.loadCandidates()
})
</script>

<style scoped>
.knowledge { padding: 16px 20px 20px; height: 100%; display: flex; flex-direction: column; }

.page-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: var(--df-gap-page);
}
.page-header h1 { font-size: 24px; font-weight: 500; color: var(--df-text); }
.header-actions { display: flex; gap: 8px; }

/* ===== 错误条(消费 store.error) ===== */
.error-banner {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 12px; margin-bottom: var(--df-gap-page);
  background: rgba(255,107,107,0.12); border: 0.5px solid var(--df-danger);
  border-radius: var(--df-radius-sm); color: var(--df-danger); font-size: 12px;
}
.error-text { word-break: break-word; }
.error-dismiss {
  flex-shrink: 0; background: transparent; border: none; color: var(--df-danger);
  font-size: 14px; cursor: pointer; line-height: 1; padding: 0 2px;
}
.error-dismiss:hover { opacity: 0.7; }

/* ===== 顶层 Tab ===== */
.top-tabs { display: flex; gap: 4px; margin-bottom: var(--df-gap-page); border-bottom: 0.5px solid var(--df-border); }
.top-tab {
  padding: 8px 16px; border: none; background: transparent; color: var(--df-text-secondary);
  font-size: 13px; cursor: pointer; border-bottom: 2px solid transparent; transition: all 0.15s;
  display: flex; align-items: center; gap: 6px;
}
.top-tab:hover { color: var(--df-text); }
.top-tab.active { color: var(--df-accent); border-bottom-color: var(--df-accent); }
.inbox-badge {
  font-size: 11px; background: var(--df-danger); color: #fff;
  padding: 1px 6px; border-radius: var(--df-radius); min-width: 16px; text-align: center;
}

/* ===== 左右分栏 ===== */
.kn-layout {
  display: grid; grid-template-columns: minmax(240px, 360px) 1fr; gap: var(--df-gap-page);
  flex: 1; min-height: 0;
}
.kn-list-panel, .kn-detail-panel {
  background: var(--df-bg-panel, var(--df-bg-card));
  border: 0.5px solid var(--df-border);
  border-radius: var(--df-radius-lg);
  overflow: hidden; display: flex; flex-direction: column;
}
.kn-detail-panel { overflow-y: auto; padding: 20px; min-width: 0; }

/* 左侧搜索 + 分类 */
.search-bar { padding: 12px 12px 0; }
.search-input {
  width: 100%; padding: 8px 12px; background: var(--df-bg-card);
  border: 0.5px solid var(--df-border); border-radius: var(--df-radius);
  color: var(--df-text); font-size: 13px; outline: none; box-sizing: border-box;
}
.search-input:focus { border-color: var(--df-accent); }
.category-tabs { display: flex; flex-wrap: wrap; gap: 4px; padding: 10px 12px; }
.cat-chip {
  display: flex; align-items: center; gap: 4px; padding: 4px 8px;
  border: 0.5px solid var(--df-border); border-radius: var(--df-radius-sm);
  background: transparent; color: var(--df-text-secondary); font-size: 12px; cursor: pointer;
}
.cat-chip:hover { background: var(--df-bg-card); }
.cat-active { background: var(--df-accent); color: #fff; border-color: var(--df-accent); }
.cat-count { font-size: 10px; opacity: 0.8; }

/* 左侧卡片列表 */
.list-cards { flex: 1; overflow-y: auto; padding: 8px 12px 12px; display: flex; flex-direction: column; gap: 8px; }
.kn-card {
  background: var(--df-bg-card); border: 0.5px solid var(--df-border);
  border-radius: var(--df-radius); padding: 10px 12px; cursor: pointer; transition: all 0.15s;
}
.kn-card:hover { border-color: var(--df-accent); }
.kn-card.selected { border-color: var(--df-accent); box-shadow: inset 3px 0 0 var(--df-accent); }
.kn-card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 6px; }
.kn-card-title { font-size: 13px; font-weight: 500; color: var(--df-text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kn-card-desc {
  font-size: 12px; color: var(--df-text-secondary); line-height: 1.4; margin-bottom: 6px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.kn-card-meta { display: flex; gap: 8px; align-items: center; font-size: 11px; color: var(--df-text-dim); flex-wrap: wrap; }
.meta-kind { background: rgba(90,99,128,0.2); padding: 1px 6px; border-radius: var(--df-radius-sm); }
.meta-reuse { color: var(--df-info); }
.list-empty { text-align: center; padding: 40px 0; color: var(--df-text-dim); font-size: 13px; }

/* ===== 详情面板空态(详情四段样式已随 KnowledgeDetail 子组件迁移)===== */
.detail-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--df-text-dim); gap: 12px; }
.detail-empty-icon { font-size: 40px; }

/* 状态徽章 / 置信度(列表卡片复用,详情由子组件 scoped 自带) */
.kn-card-status { font-size: 11px; font-weight: 500; padding: 2px 8px; border-radius: var(--df-radius-xs); white-space: nowrap; }
.st-candidate { background: rgba(255,217,61,0.15); color: var(--df-warning); }
.st-pending_review { background: rgba(108,159,255,0.15); color: var(--df-info); }
.st-published { background: rgba(100,255,218,0.15); color: var(--df-success); }
.st-archived { background: rgba(90,99,128,0.2); color: var(--df-text-dim); }
.conf-high { background: rgba(100,255,218,0.15); color: var(--df-success); padding: 1px 6px; border-radius: var(--df-radius-xs); }
.conf-medium { background: rgba(255,217,61,0.15); color: var(--df-warning); padding: 1px 6px; border-radius: var(--df-radius-xs); }
.conf-low { background: rgba(255,107,107,0.15); color: var(--df-danger); padding: 1px 6px; border-radius: var(--df-radius-xs); }

/* ===== 按钮 ===== */
.btn {
  padding: 8px 16px; border: none; border-radius: var(--df-radius-sm);
  font-size: 13px; cursor: pointer; transition: all 0.15s;
}
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-primary { background: var(--df-accent); color: #fff; }
.btn-primary:hover { background: var(--df-accent-hover); }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-ghost { background: transparent; color: var(--df-text-secondary); border: 0.5px solid var(--df-border); }
.btn-ghost:hover { background: var(--df-bg-card); color: var(--df-text); }

/* ===== 新增对话框 ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
}
.modal-box {
  background: var(--df-bg-panel, var(--df-bg-card)); border: 0.5px solid var(--df-border);
  border-radius: var(--df-radius-lg); padding: 24px; width: 90%; max-width: 520px;
}
.modal-title { font-size: 16px; font-weight: 500; color: var(--df-text); margin-bottom: 16px; }
.form-row { margin-bottom: 12px; }
.form-row label { display: block; font-size: 12px; color: var(--df-text-secondary); margin-bottom: 4px; }
.form-input, .form-select {
  width: 100%; padding: 8px 10px; background: var(--df-bg-card);
  border: 0.5px solid var(--df-border); border-radius: var(--df-radius-sm);
  color: var(--df-text); font-size: 13px; outline: none; box-sizing: border-box;
}
.form-input:focus, .form-select:focus { border-color: var(--df-accent); }
.form-textarea { min-height: 80px; resize: vertical; font-family: inherit; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* ===== 响应式:窄屏单列布局(详情窄屏样式随 KnowledgeDetail 子组件迁移) ===== */
@media (max-width: 760px) {
  .kn-layout { grid-template-columns: 1fr; }
}
</style>
