const { request } = require('../../utils/request.js');

Page({
  data: {
    wish: null,
    loading: true,
  },

  onLoad(options) {
    const { id } = options;
    if (id) {
      this.loadWishDetail(id);
    }
  },

  async loadWishDetail(id) {
    try {
      const res = await request({
        url: `/api/wish/detail/${id}`,
        method: 'GET',
      });
      
      if (res.code === 0) {
        this.setData({
          wish: res.data,
          loading: false,
        });
      }
    } catch (err) {
      console.error('加载许愿详情失败:', err);
      // 使用模拟数据
      this.setData({
        wish: {
          id: id,
          content: '愿家人平安健康，事业顺利，心想事成！',
          type: 'paid',
          createdAt: '2024-01-15 12:00:00',
          user: {
            nickname: '祈福用户',
            avatar: '',
          },
        },
        loading: false,
      });
    }
  },

  // 分享
  onShareAppMessage() {
    const { wish } = this.data;
    return {
      title: wish ? `我的愿望：${wish.content.substring(0, 20)}...` : '快来许愿吧！',
      path: `/pages/wish-detail/wish-detail?id=${wish ? wish.id : ''}`,
    };
  },
});
