<template>
  <div class="knowledge-container">
    <div class="knowledge-header">
      <h2>安全知识库</h2>
      <div class="search-bar">
        <input v-model="searchKeyword" placeholder="搜索安全知识..." @input="searchKnowledge" />
      </div>
    </div>
    <div class="knowledge-content">
      <div class="category-tabs">
        <button
          v-for="cat in categories"
          :key="cat"
          :class="['tab', { active: currentCategory === cat }]"
          @click="currentCategory = cat"
        >{{ cat }}</button>
      </div>
      <div class="article-list">
        <div
          v-for="article in filteredArticles"
          :key="article.id"
          class="article-card"
          @click="viewArticle(article)"
        >
          <h3>{{ article.title }}</h3>
          <p class="summary">{{ article.summary }}</p>
          <div class="meta">
            <span class="category">{{ article.category }}</span>
            <span class="date">{{ article.date }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="article-modal" v-if="selectedArticle" @click.self="selectedArticle = null">
      <div class="modal-content">
        <button class="close-btn" @click="selectedArticle = null">✕</button>
        <h2>{{ selectedArticle.title }}</h2>
        <div class="article-body" v-html="selectedArticle.content"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { getKnowledgeArticles, getKnowledgeCategories } from '@/mock/knowledgeMock'

const articles = ref(getKnowledgeArticles())
const categories = ref(['全部', ...getKnowledgeCategories()])
const currentCategory = ref('全部')
const searchKeyword = ref('')
const selectedArticle = ref(null)

const filteredArticles = computed(() => {
  let result = articles.value
  if (currentCategory.value !== '全部') {
    result = result.filter(a => a.category === currentCategory.value)
  }
  if (searchKeyword.value) {
    const keyword = searchKeyword.value.toLowerCase()
    result = result.filter(a =>
      a.title.toLowerCase().includes(keyword) ||
      a.summary.toLowerCase().includes(keyword)
    )
  }
  return result
})

const searchKnowledge = () => {
  // filtered by computed
}

const viewArticle = (article) => {
  selectedArticle.value = article
}
</script>

<style scoped lang="scss">
.knowledge-container {
  height: 100%;
  background: #f8f9fa;
  overflow-y: auto;
  padding: 24px;
}

.knowledge-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;

  h2 {
    font-size: 20px;
    color: #333;
  }

  .search-bar input {
    padding: 10px 16px;
    width: 300px;
    border: 1px solid #d9d9d9;
    border-radius: 20px;
    outline: none;

    &:focus {
      border-color: #1890ff;
    }
  }
}

.category-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
  flex-wrap: wrap;

  .tab {
    padding: 8px 16px;
    border: 1px solid #d9d9d9;
    background: #fff;
    border-radius: 20px;
    cursor: pointer;
    font-size: 13px;
    transition: all 0.2s;

    &:hover {
      border-color: #1890ff;
      color: #1890ff;
    }

    &.active {
      background: #1890ff;
      color: #fff;
      border-color: #1890ff;
    }
  }
}

.article-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.article-card {
  background: #fff;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.05);
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  }

  h3 {
    font-size: 16px;
    color: #333;
    margin-bottom: 8px;
  }

  .summary {
    color: #666;
    font-size: 13px;
    line-height: 1.5;
    margin-bottom: 12px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .meta {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: #999;

    .category {
      padding: 2px 8px;
      background: #f0f5ff;
      color: #1890ff;
      border-radius: 4px;
    }
  }
}

.article-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;

  .modal-content {
    background: #fff;
    width: 80%;
    max-height: 80vh;
    border-radius: 12px;
    padding: 32px;
    overflow-y: auto;
    position: relative;

    .close-btn {
      position: absolute;
      top: 16px;
      right: 16px;
      background: none;
      border: none;
      font-size: 20px;
      cursor: pointer;
      color: #666;
    }

    h2 {
      margin-bottom: 16px;
      color: #333;
    }

    .article-body {
      line-height: 1.8;
      color: #333;

      :deep(p) {
        margin-bottom: 12px;
      }
    }
  }
}
</style>
