Page({
  data: {
    weekStartDay: 0,
    ziHourSect: 'lateZiNextDay',
    solarTime: true,
    showBuddhist: false,
    highlightLunar: true
  },

  onShow() {
    this.loadSettings();
  },

  loadSettings() {
    const settings = wx.getStorageSync('lunar-settings') || {};
    this.setData({
      weekStartDay: settings.weekStartDay || 0,
      ziHourSect: settings.ziHourSect || 'lateZiNextDay',
      solarTime: settings.solarTime !== false,
      showBuddhist: settings.showBuddhist === true,
      highlightLunar: settings.highlightLunar !== false
    });
  },

  saveSettings() {
    const { weekStartDay, ziHourSect, solarTime, showBuddhist, highlightLunar } = this.data;
    wx.setStorageSync('lunar-settings', { weekStartDay, ziHourSect, solarTime, showBuddhist, highlightLunar });
  },

  setWeekStart(e) {
    this.setData({ weekStartDay: Number(e.currentTarget.dataset.v) });
    this.saveSettings();
  },

  setZiSect(e) {
    this.setData({ ziHourSect: e.currentTarget.dataset.v });
    this.saveSettings();
  },

  onSolarTimeChange(e) {
    this.setData({ solarTime: e.detail.value });
    this.saveSettings();
  },

  onBuddhistChange(e) {
    this.setData({ showBuddhist: e.detail.value });
    this.saveSettings();
  },

  onHighlightLunarChange(e) {
    this.setData({ highlightLunar: e.detail.value });
    this.saveSettings();
  }
});
