<template>
    <div class="knowledge-card">
        <div class="knowledge-header">
            <div class="knowledge-title">
                <div class="title-item" :class="{ active: activeKnowledgeId === 8 }" @click="handleKnowledgeClick(8)">
                    焦点话题
                </div>
                <div class="title-item" :class="{ active: activeKnowledgeId === 6 }" @click="handleKnowledgeClick(6)">
                    种植精华
                </div>
                <!-- <div class="title-item" :class="{ active: activeKnowledgeId === 7 }" @click="handleKnowledgeClick(7)">
                    实战操作
                </div> -->
            </div>
            <!-- <div class="more-text" @click="handleMoreClick">
                更多<el-icon class="more-icon"><ArrowRight /></el-icon>
            </div> -->
        </div>
        <div class="knowledge-content focus-content" v-if="activeKnowledgeId === 8">
            <div
                class="focus-item"
                @click="handleBannerClick(item.id)"
                v-for="(item, index) in knowledgeList"
                :key="index"
            >
                <img class="thumb" :src="item.media?.[0]" alt="" />
                <div class="focus-content">
                    <div class="title van-ellipsis">{{ item.title }}</div>
                    <div class="focus-expert">
                        <div class="expert-name">
                            <el-avatar :size="22" :src="item.expertAvatar+resize_300" />
                            {{ item.expertName || '专家' }}
                            <span class="expert-text">发表了回复，点击查看</span>
                        </div>
                        <div class="date">{{ formatDate(item.createTime) }}</div>
                    </div>
                    <!-- <div class="focus-answer">
                        <div class="answer-icon">
                            答
                        </div>
                        {{ item.summary }}
                    </div> -->
                </div>
            </div>
            <div class="no-data" v-if="knowledgeList.length === 0">暂无数据</div>
        </div>
        <div class="knowledge-content" v-else>
            <div
                class="knowledge-item"
                @click="handleBannerClick(item.id)"
                v-for="(item, index) in knowledgeList"
                :key="index"
            >
                <img class="thumb" :src="item.media?.[0]" alt="" />
                <div class="card-body-left">
                    <div class="title van-multi-ellipsis--l2">{{ item.title }}</div>
                    <div class="date">{{ formatDate(item.createTime) }}</div>
                </div>
            </div>
            <div class="no-data" v-if="knowledgeList.length === 0">暂无数据</div>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref, watch } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { resize_300 } from "@/api/config";

const router = useRouter();
const store = useStore();
const activeKnowledgeId = ref(8);

// 按 topicId 缓存知识列表，避免每次切换都请求接口
const knowledgeCache = ref({});

const handleKnowledgeClick = (index) => {
    activeKnowledgeId.value = index;

    // 如果已经有缓存，直接用缓存数据
    if (knowledgeCache.value[index]) {
        knowledgeList.value = knowledgeCache.value[index];
        return;
    }

    // 没有缓存时再发起请求
    getKnowledgeList();
};

const handleBannerClick = (id) => {
    router.push(`/warning_detail?id=${id}&showImage=true`);
};

const handleMoreClick = () => {
    router.push(`/knowledge_list?topicId=${activeKnowledgeId.value}`);
};

const knowledgeList = ref([]);
onMounted(() => {
    // 默认 topicId 首次进入时请求一次
    getKnowledgeList();
});
watch(
    () => store.state.home.gardenId,
    (newGardenId) => {
        if (newGardenId) {
            getKnowledgeList();
        }
    }
);

const formatDate = (dateString) => {
    if (!dateString) return "";
    // 将 2026-01-06T00:00:00 格式转换为 2026-01-06
    return dateString.split("T")[0];
};

const getKnowledgeList = async () => {
    try {
        let data = [];
        // 实战知识库：使用原接口
        const params = {
            page: 1,
            limit: 10,
            topicId: activeKnowledgeId.value,
        };
        const { data: result } = await VE_API.home.warningPageList(params);
        data = result || [];

        knowledgeList.value = data;
        // 按当前 topicId 缓存，后续切换直接读取
        knowledgeCache.value[activeKnowledgeId.value] = data;
    } catch (error) {
        console.error("获取知识列表失败:", error);
        knowledgeList.value = [];
    }
};
</script>

<style lang="scss" scoped>
.knowledge-card {
    padding: 0 10px;
    .knowledge-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 6px 0 2px 0;
        .knowledge-title {
            display: flex;
            align-items: center;
            gap: 10px;
            color: #bfbfbf;
            .title-item {
                cursor: pointer;
                font-size: 18px;
                font-family: "PangMenZhengDao";
                &.active {
                    color: #2199f8;
                }
            }
        }
        .more-text {
            font-size: 13px;
            color: #4e5969;
            cursor: pointer;
            display: flex;
            align-items: center;
        }
    }
    .knowledge-content {
        padding: 8px 0;
        .knowledge-item {
            display: flex;
            gap: 12px;
            // padding: 12px 10px;
            // background: #ffffff;
            border-radius: 12px;
            align-items: center;
            height: 74px;
            box-sizing: border-box;
            .thumb {
                width: 112px;
                height: 74px;
                border-radius: 8px;
                object-fit: cover;
                flex: none;
            }
            .card-body-left {
                flex: 1;
                height: 95%;
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                font-size: 14px;
                line-height: 22px;
                .date {
                    font-size: 13px;
                    color: #86909c;
                    margin-top: 4px;
                }
            }
        }
        .knowledge-item + .knowledge-item {
            margin-top: 12px;
        }
        .item-content {
            font-size: 14px;
            color: #666666;
        }
        .focus-item {
            margin-bottom: 8px;
            border: 1px solid rgba(142, 142, 142, 0.2);
            border-radius: 12px;
            padding: 5px 10px;
            .thumb {
                width: 100%;
                height: 123px;
                border-radius: 5px;
                object-fit: cover;
            }
            .focus-content {
                .title {
                    font-weight: 500;
                    font-size: 16px;
                    line-height: 22px;
                    color: #1d2129;
                    padding: 8px 0;
                }
                .focus-expert {
                    padding: 0 5px;
                    margin-bottom: 5px;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    font-size: 12px;
                    color: #666666;
                    height: 32px;
                    border-radius: 5px;
                    background: rgba(33, 153, 248, 0.05);
                    .expert-name {
                        display: flex;
                        align-items: center;
                        gap: 6px;
                        .expert-text {
                            padding-left: 2px;
                            color: #2199F8;
                        }
                    }
                }
                .focus-answer {
                    padding: 5px;
                    background: rgba(33, 153, 248, 0.05);
                    border-radius: 5px;
                    font-size: 13px;
                    line-height: 22px;
                    color: #86909C;
                    text-align: justify;
                    display: flex;
                    gap: 8px;
                    .answer-icon {
                        margin-top: 2px;
                        flex: none;
                        width: 18px;
                        height: 18px;
                        background: #2199f8;
                        border-radius: 2px;
                        font-size: 12px;
                        color: #fff;
                        text-align: center;
                        line-height: 18px;
                    }
                }
            }
        }
    }
    .no-data {
        font-size: 14px;
        color: #999999;
        text-align: center;
        margin-top: 20px;
    }
}
</style>
