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

    <view class="px-4 pt-3">
      <view v-if="loading" class="py-10 text-center">
        <text style="font-size: 13px; color: #9ca3af;">加载中...</text>
      </view>

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

      <view v-else class="gap-y-3 px-4">
        <view
          v-for="item in knowledges"
          :key="item.id"
          class="knowledge-card"
          hover-class="card-hover"
          :hover-start-time="0"
          :hover-stay-time="120"
          @click="goToDetail(item)"
        >
          <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, onMounted } from 'vue'
import TopBar from '@/components/common/TopBar.vue'
import EmptyState from '@/components/common/EmptyState.vue'
import { getKnowledgeList, type KnowledgeItem } from '@/api/knowledge'

const loading = ref(false)
const knowledges = ref<KnowledgeItem[]>([])

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

onMounted(async () => {
  loading.value = true
  try {
    const res = await getKnowledgeList(1, 50)
    knowledges.value = res.rows || []
  } catch (error) {
    console.error('获取知识列表失败:', error)
  } finally {
    loading.value = false
  }
})
</script>

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

.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;
  background-color: rgba(164, 216, 152, 0.1);
}

.card-cover-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
}

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

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