import { ALL_WORKSHEETS, getWorksheetsByAge } from '../../core/data/worksheets';

interface AgeGroup {
    label: string;
    desc: string;
    emoji: string;
    minAge: number;
    maxAge: number;
    count: number;
    gradient: string;
}

Page({
    data: {
        ageGroups: [] as AgeGroup[],
    },

    onLoad() {
        const groups = [
            { label: '3-4 岁', desc: '启蒙认知', emoji: '👶', minAge: 3, maxAge: 4, gradient: 'linear-gradient(135deg, #fff9c4, #fff176)' },
            { label: '4-5 岁', desc: '基础练习', emoji: '🧒', minAge: 4, maxAge: 5, gradient: 'linear-gradient(135deg, #c8e6c9, #81c784)' },
            { label: '5-6 岁', desc: '能力提升', emoji: '👦', minAge: 5, maxAge: 6, gradient: 'linear-gradient(135deg, #bbdefb, #64b5f6)' },
            { label: '6-7 岁', desc: '幼小衔接', emoji: '👧', minAge: 6, maxAge: 7, gradient: 'linear-gradient(135deg, #e1bee7, #ba68c8)' },
            { label: '7-8 岁', desc: '巩固挑战', emoji: '🎒', minAge: 7, maxAge: 8, gradient: 'linear-gradient(135deg, #ffccbc, #ff8a65)' },
        ];

        const ageGroups = groups.map((g) => ({
            ...g,
            count: getWorksheetsByAge(ALL_WORKSHEETS, g.minAge).length,
        }));

        this.setData({ ageGroups });
    },

    onAgeCardTap(e: WechatMiniprogram.TouchEvent) {
        const minAge = Number(e.currentTarget.dataset.minAge);
        const maxAge = Number(e.currentTarget.dataset.maxAge);
        if (Number.isNaN(minAge) || Number.isNaN(maxAge)) return;
        const title = `${minAge}-${maxAge}岁推荐`;
        wx.navigateTo({
            url: `/pages/category/category?minAge=${minAge}&maxAge=${maxAge}&title=${encodeURIComponent(title)}`,
        });
    },
});
