// subPackage/page/pages/couponCenterInfo/couponCenterInfo.js
const wxService = require('../../../../utils/wxService')
wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId : null,
    activityInfo : {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    this.data.activityId = id;
    this.getACtivityInfo();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //获取活动详情
  getACtivityInfo(){
    wxService.post(`/coupon/couponCenterActivity/buyer/getDetail?id=${this.data.activityId}`).then(res => {
      if(res){
        let obj = res.data.data ? res.data.data : null;
        if (obj){
          let arr  = obj.definition ? JSON.parse(obj.definition) : [];
          obj.desc = arr.filter(item => item.type == 2);
          obj.hasExpDate = arr.filter(item => item.type == 1).length > 0 ;
          this.data.activityInfo = obj;
          this.setData({
            activityInfo: this.data.activityInfo
          });
        }
      }
    })
  },

  //领取优惠券
  onTapGetCoupon(){
    wx.showLoading({
      title: '领取中..',
    });
    let id = this.data.activityId;
    wxService.post(`/coupon/couponCenterActivity/buyer/coupon/draw?id=${id}`).then(res => {
      wx.hideLoading();
      if (res) {
        if (res.data.result == 0) {
          wx.showToast({
            title: '领取成功',
          });
        }
        else {
          wx.showToast({
            title: '领取失败，请稍后再试',
            icon: 'none'
          });
        }
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let url = this.data.activityInfo.listPicture ? this.data.activityInfo.listPicture : 'https://img3.bigaka.com/prd/3001/202003/20200331/3001649f2f08-3b4e-4b5b-97c1-30cdf646651c.png';
    return {
      title: `我分享给你一张“${this.data.activityInfo.couponName}”的优惠券，赶快领取吧~`,
      path: `/subPackage/page/pages/couponCenterInfo/couponCenterInfo?id=${this.data.activityId}`,
      imageUrl: url, // 可以更换分享的图片
    }
  }
})