<template>
	<div class="knowledge-management-page">
		<div class="header">
			<div class="flex items-center justify-between">
				<h1>{{ t('pages.knowledge.title') }}</h1>
			</div>
			<p class="subtitle">{{ t('pages.knowledge.subtitle') }}</p>
		</div>
		<div class="main-container">
			<KnowledgeBaseSidebar :current-base-id="currentBase?.id!" @select-base="handleSelectBase" />
			<div v-if="currentBase" class="content-container">
				<el-tabs v-model="currentModule" type="border-card">
					<el-tab-pane :label="t('pages.knowledge.knowledgeTab')" name="document" v-if="currentBase.type === 'document'">
						<DocumentManage v-if="currentModule === 'document'" :current-base="currentBase" />
					</el-tab-pane>
					<el-tab-pane :label="t('pages.knowledge.wikiTab')" name="wiki" v-if="currentBase.type === 'document'">
						<WikiManage v-if="currentModule === 'wiki'" :current-base="currentBase" />
					</el-tab-pane>
					<el-tab-pane :label="t('pages.knowledge.graphTab')" name="graph" v-if="currentBase.type === 'document'">
						<WikiGraph v-if="currentModule === 'graph'" :current-base="currentBase" />
					</el-tab-pane>
					<el-tab-pane :label="t('pages.knowledge.qaTab')" name="qa" v-if="currentBase.type === 'faq'">
						<QaManage v-if="currentModule === 'qa'" :current-base-id="currentBase?.id!" />
					</el-tab-pane>
				</el-tabs>
			</div>
			<el-empty v-else :description="t('pages.knowledge.selectBase')" class="empty-container" />
		</div>
	</div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useI18n } from '@/composables/useI18n'
import DocumentManage from './DocumentManage.vue'
import KnowledgeBaseSidebar from './KnowledgeBaseSidebar.vue'
import QaManage from './QaManage.vue'
import type { KnowledgeBaseItem } from './types'
import WikiGraph from './WikiGraph.vue'
import WikiManage from './WikiManage.vue'

const { t } = useI18n()

const currentBase = ref<KnowledgeBaseItem>()
const currentModule = ref<'document' | 'qa' | 'wiki' | 'graph'>('document')

function handleSelectBase(base?: KnowledgeBaseItem) {
	currentBase.value = base
	currentModule.value = base?.type === 'document' ? 'document' : 'qa'
}
</script>

<style scoped lang="less">
.knowledge-management-page {
	min-height: 100%;
	padding: 24px;
	// background: #f5f7fa;
	box-sizing: border-box;
}

.header {
	margin-bottom: 16px;

	h1 {
		margin: 0;
		font-size: 28px;
		color: var(--text-primary);
	}
}

.subtitle {
	margin: 6px 0 0;
	font-size: 14px;
	color: var(--text-secondary);
}

.main-container {
	display: flex;
	gap: 16px;
	align-items: stretch;
	min-height: calc(100vh - 108px);
}

.content-container {
	flex: 1;
	min-width: 0;

	:deep(.el-tabs) {
		height: 100%;
		border-radius: 12px;
		overflow: hidden;
	}

	:deep(.el-tabs__content) {
		min-height: calc(100vh - 250px);
		padding-top: 16px;
		background: var(--bg-base);
	}
}

.empty-container {
	flex: 1;
	display: flex;
	align-items: center;
	justify-content: center;
	min-height: calc(100vh - 108px);
	border-radius: 12px;
	background: var(--bg-base);
}

:deep(.el-tab-pane) {
	height: 100%;
	position: relative;
}

@media (max-width: 960px) {
	.main-container {
		flex-direction: column;
	}
}
</style>
