const { calculateDailyFortune } = require('../../utils/core/index.js');

Page({
  data: {
    hasBazi: false,
    fortune: null,
    scoreLevelText: ''
  },

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

  onPullDownRefresh() {
    this.loadFortune();
    wx.stopPullDownRefresh();
  },

  loadFortune() {
    const bazi = wx.getStorageSync('lunar-user-bazi');
    if (!bazi) {
      this.setData({ hasBazi: false, fortune: null });
      return;
    }

    const today = new Date();
    const fortune = calculateDailyFortune(bazi.eightChar, today);

    const levelMap = {
      great: '大吉',
      good: '吉',
      fair: '平',
      poor: '凶',
      bad: '大凶'
    };

    this.setData({
      hasBazi: true,
      fortune,
      scoreLevelText: levelMap[fortune.scoreLevel] || '平'
    });
  },

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