<template>
  <div class="knowledge-base-container">
    <div class="card-container">
      <el-card class="create-card" shadow="hover" @click="handleCreate">
        <div class="create-content">
          <el-icon class="create-icon"><Plus /></el-icon>
          <span class="create-text">创建知识库</span>
        </div>
        <div class="create-footer">
          导入您自己的文本数据或通过 Webhook 实时写入数据以增强 LLM 的上下文。
        </div>
      </el-card>
       <el-card class="document-card" v-if="loading">
        <div class="document-header">
          <el-icon>
            <Folder />
          </el-icon>
          <el-skeleton animated :rows="1" />
        </div>
        <div class="document-info">
          <el-skeleton animated :rows="1" />
            
        </div>
        <p class="document-description">
          <el-skeleton animated :rows="3" />
        </p>
       </el-card> 
      <el-card
        v-for="item in tableData"
        :key="item.id"
        class="document-card"
        shadow="hover"
        @click="toDataset(item.id)"
      >
        <div class="document-header">
          <el-icon>
            <Folder />
          </el-icon>
          <span>{{ item.name }}</span>
        </div>
        <div class="document-info">
          <el-tag size="small">{{ item.docsNum || 0 }} 文档</el-tag>
          <el-tag size="small" type="info"
            >{{ (Number(item.totalSize) / 1000000).toFixed(2) }} MB</el-tag
          >
          <el-tag size="small" type="warning" v-if="item.embedStore">
            {{ item.embedStore.name }}</el-tag
          >
        </div>
        <p class="document-description">
          {{ item.des || '暂无描述' }}
        </p>
      </el-card>
    </div>

    <div class="pagination-container">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :small="false"
        :disabled="false"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <create ref="createRef" @reload="loadData" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Plus, Folder } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { KnowledgeApi } from '@/api/new-ai/knowledge'
import Create from './components/create.vue'
const router = useRouter()
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const tableData = ref([])
const createRef = ref()
const loading = ref(false)
const loadData = async () => {
  try {
    loading.value = true
    const res = await KnowledgeApi.getKnowledgePage({
      pageNo: currentPage.value,
      pageSize: pageSize.value
    }).finally(() => loading.value = false)
    tableData.value = res.list || []
    total.value = res.total || 0
  } catch (error) {
    console.error('Failed to load knowledge list:', error)
  }
}

const handleSizeChange = (val: number) => {
  pageSize.value = val
  loadData()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  loadData()
}

const toDataset = (id: string) => {
  router.push(`/ai/console/knowledge/${id}`)
}

const handleCreate = () => {
  createRef.value.open()
}
onMounted(() => {
  loadData()
})
</script>

<style scoped>
.knowledge-base-container {
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei',
    '微软雅黑', Arial, sans-serif;
  padding: 20px;
}

.card-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.create-card {
  cursor: pointer;
  transition: all 0.3s;
}

.create-card:hover {
  transform: translateY(-5px);
}

.create-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px 0;
}

.create-icon {
  font-size: 40px;
  color: var(--el-color-primary);
  margin-bottom: 10px;
}

.create-text {
  font-size: 18px;
  font-weight: bold;
  color: var(--el-color-primary);
}

.create-footer {
  text-align: center;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  padding: 10px;
  border-top: 1px solid var(--el-border-color-lighter);
}

.document-card {
  cursor: pointer;
  transition: all 0.3s;
}

.document-card:hover {
  transform: translateY(-5px);
}

.document-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.document-header .el-icon {
  margin-right: 8px;
  font-size: 20px;
  color: var(--el-color-primary);
}

.document-header span {
  font-size: 16px;
  font-weight: bold;
}

.document-info {
  display: flex;
  gap: 10px;
  margin-bottom: 15px;
}

.document-description {
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin: 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
