const { getDayInfo, getAlmanacInfo } = require('../../utils/core/index.js');

Page({
  data: {
    date: '',
    dayInfo: {},
    almanac: {}
  },

  onLoad(options) {
    const date = options.date || new Date().toISOString().split('T')[0];
    this.setData({ date });
    this.loadData(date);
  },

  loadData(dateStr) {
    const [year, month, day] = dateStr.split('-').map(Number);
    const dayInfo = getDayInfo(year, month, day);
    const almanac = getAlmanacInfo(year, month, day);
    this.setData({ dayInfo, almanac });
  },

  addBookmark() {
    const bookmarks = wx.getStorageSync('lunar-bookmarks') || [];
    const { dayInfo } = this.data;
    const exists = bookmarks.some(b => b.date === dayInfo.solarDate);
    if (!exists) {
      bookmarks.push({
        date: dayInfo.solarDate,
        lunarDate: `${dayInfo.lunarMonthName}${dayInfo.lunarDayName}`,
        note: ''
      });
      wx.setStorageSync('lunar-bookmarks', bookmarks);
      wx.showToast({ title: '已收藏', icon: 'success' });
    } else {
      wx.showToast({ title: '已收藏过', icon: 'none' });
    }
  },

  goBazi() {
    wx.navigateTo({ url: '/pages/bazi/bazi' });
  }
});
