<template>
  <view class="sub-page">
    <TopBar title="行业知识" show-back />

    <view class="px-4 pt-3">
      <!-- 搜索栏：默认白底灰边 -->
      <view class="search-bar mb-3">
        <text class="uni-icons uniui-search search-icon"></text>
        <input
          v-model="searchText"
          class="search-input"
          placeholder="搜索知识标题或关键词"
        />
      </view>

      <!-- 分类筛选 -->
      <view class="filter-bar mb-3">
        <view
          v-for="cat in categories"
          :key="cat.value"
          class="filter-chip"
          :class="{ 'filter-chip-active': currentCategory === cat.value }"
          @click="currentCategory = cat.value"
        >
          {{ cat.label }}
        </view>
      </view>

      <view v-if="loading" class="py-10 text-center">
        <text class="empty-text">加载中...</text>
      </view>

      <view v-else-if="filteredKnowledges.length === 0">
        <EmptyState message="暂无知识内容" />
      </view>

      <view v-else class="gap-y-3">
        <view
          v-for="item in filteredKnowledges"
          :key="item.id"
          class="knowledge-card"
          hover-class="card-hover"
          :hover-start-time="0"
          :hover-stay-time="120"
          @click="goToDetail(item.id)"
        >
          <view v-if="item.cover" class="card-cover">
            <image :src="item.cover" mode="aspectFill" class="w-full h-full" />
          </view>
          <view v-else class="card-cover card-cover-placeholder">
            <text class="uni-icons uniui-folder-add-filled cover-placeholder-icon"></text>
          </view>

          <view class="p-3">
            <text class="card-title truncate">{{ item.title }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import TopBar from '../../components/common/TopBar.vue'
import EmptyState from '../../components/common/EmptyState.vue'
import {
  getKnowledgeList,
  getKnowledgeCategories,
  type KnowledgeItem,
} from '../../api/knowledge'

const loading = ref(false)
const searchText = ref('')
const currentCategory = ref('all')

const categories = ref([{ value: 'all', label: '全部' }])
const knowledges = ref<KnowledgeItem[]>([])

const filteredKnowledges = computed(() => {
  if (!searchText.value) return knowledges.value
  const keyword = searchText.value.toLowerCase()
  return knowledges.value.filter(k =>
    k.title.toLowerCase().includes(keyword) ||
    (k.content || '').toLowerCase().includes(keyword)
  )
})

function goToDetail(id: number) {
  uni.navigateTo({ url: `/subPackages/pages-sales/knowledgeDetail?id=${id}` })
}

async function loadList() {
  loading.value = true
  try {
    const category = currentCategory.value === 'all' ? undefined : currentCategory.value
    const res = await getKnowledgeList(1, 50, category)
    knowledges.value = res.rows || []
  } catch (error) {
    console.error('获取知识列表失败:', error)
  } finally {
    loading.value = false
  }
}

watch(currentCategory, loadList)

onMounted(async () => {
  loadList()
  try {
    const cats = await getKnowledgeCategories()
    categories.value = [{ value: 'all', label: '全部' }, ...cats.map(c => ({ value: c.key, label: c.name }))]
  } catch (error) {
    console.error('获取知识分类失败:', error)
  }
})
</script>

<style scoped>
.sub-page {
  max-width: 430px;
  margin: 0 auto;
  min-height: 100vh;
  background-color: #f6fbf9;
  padding-bottom: 24px;
}

.search-bar {
  display: flex;
  align-items: center;
  padding: 0 12px;
  height: 40px;
  border-radius: 999px;
  background-color: #ffffff;
  border: 1px solid #e5e7eb;
}

.search-icon {
  font-size: 16px;
  color: #9ca3af;
  margin-right: 8px;
}

.search-input {
  flex: 1;
  font-size: 14px;
  color: #1f2937;
}

/* 筛选条：默认白底灰边，激活态绿色 */
.filter-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

.filter-chip {
  padding: 6px 16px;
  border-radius: 999px;
  font-size: 13px;
  color: #6b7280;
  background-color: #ffffff;
  border: 1px solid #e5e7eb;
  flex-shrink: 0;
}

.filter-chip-active {
  color: #ffffff;
  font-weight: 600;
  background: linear-gradient(135deg, #368f6f 0%, #4ba98a 100%);
  border-color: transparent;
}

.gap-y-3 {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.knowledge-card {
  background-color: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid rgba(164, 216, 152, 0.35);
  border-radius: 20px;
  box-shadow: 0 14px 40px -10px rgba(54, 143, 111, 0.22);
  overflow: hidden;
}

.card-hover {
  background-color: rgba(164, 216, 152, 0.1);
}

.card-cover {
  height: 150px;
  overflow: hidden;
}

.card-cover-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, rgba(164, 216, 152, 0.35) 0%, rgba(90, 184, 208, 0.25) 100%);
}

.cover-placeholder-icon {
  font-size: 40px;
  color: #368f6f;
}

.card-title {
  display: block;
  font-size: 15px;
  font-weight: 600;
  color: #1f2937;
}

.empty-text {
  font-size: 13px;
  color: #9ca3af;
}
</style>
