// pages/garden/index.js
import { MOCK_PLANTS } from '../../utils/mockData';

Page({
    data: {
        plants: [],
        dateString: '',
        greeting: ''
    },

    onLoad(options) {
        this.initTime();
        this.loadPlants();
    },

    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: 0
            })
        }
        // Refresh list in case new plant was added
        this.loadPlants();
    },

    loadPlants() {
        this.setData({ plants: MOCK_PLANTS });
    },

    initTime() {
        const updateTime = () => {
            const now = new Date();
            const hour = now.getHours();

            let greet = '晚上好';
            if (hour >= 5 && hour < 12) greet = '上午好';
            else if (hour >= 12 && hour < 14) greet = '中午好';
            else if (hour >= 14 && hour < 19) greet = '下午好';

            const month = now.getMonth() + 1;
            const day = now.getDate();
            const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
            const weekDay = weekDays[now.getDay()];

            this.setData({
                greeting: greet,
                dateString: `${month}月${day}日 ${weekDay}`
            });
        };

        updateTime();
    },

    navigateToDetail(e) {
        const { id } = e.currentTarget.dataset;
        wx.navigateTo({
            url: `/pages/plant-detail/index?id=${id}`,
        });
    },

    navigateToAdd() {
        wx.navigateTo({
            url: '/pages/garden/add/index'
        });
    },

    onScrollLower() {
        console.log('Scroll to lower - loading more plants...');
        // In a real app, this would trigger pagination
    }
})
