// pages/wiki/detail/index.js
import { MOCK_WIKI } from '../../../utils/mockData';

Page({
    data: {
        plant: null,
        activeImageIndex: 0,
        swiperList: []
    },

    onLoad(options) {
        if (options.id) {
            this.loadPlantDetail(options.id);
        }
    },

    loadPlantDetail(id) {
        // Find plant in MOCK_WIKI
        const plant = MOCK_WIKI.find(p => p.id === id);
        if (plant) {
            // Set Page Title
            wx.setNavigationBarTitle({
                title: plant.name
            });

            // Prepare swiper list
            const swiperList = (plant.images || []).map(img => {
                return (img.indexOf('http') === 0 || img.indexOf('wxfile') === 0) ? img : `/assets/${img}`;
            });

            // Ensure some default values if missing
            const enrichedPlant = {
                ...plant,
                light: plant.light || { level: '中等', description: '适合明亮的散射光' },
                water: plant.water || { frequency: '每周1-2次', description: '保持土壤微润' },
                temperature: plant.temperature || '15-28℃',
                humidity: plant.humidity || '50%-70%',
                soil: plant.soil || '疏松透气的营养土',
                fertilizer: plant.fertilizer || '生长季每月施一次薄肥',
                toxicity: plant.toxicity || '无毒',
                commonPests: plant.commonPests || ['红蜘蛛'],
                careTips: plant.careTips || ['注意通风', '定期清洁叶面']
            };

            this.setData({
                plant: enrichedPlant,
                swiperList: swiperList
            });
        }
    },

    onSwiperChange(e) {
        this.setData({
            activeImageIndex: e.detail.current
        });
    },

    onShareAppMessage() {
        if (!this.data.plant) return {};
        return {
            title: `植物百科 - ${this.data.plant.name}`,
            path: `/pages/wiki/detail/index?id=${this.data.plant.id}`
        };
    }
});
