const wx2my = require('../../../../wx2my');
const Behavior = require('../../../../Behavior');
// subPackage/home/pages/h5/h5page.js
const wxService = require('../../../../utils/wxService');

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [],
    isNoMore: false,
    pageNo: 1,
    pageSize: 50,
    totalPages: 0,
    totalElements: 0,
    activityId: '',
    isMore: true,
    windowHeight: 0
  },
  // 活动下商品列表 id  pageNo pageSize
  findActivityProductList(activityId, pageNo, pageSize) {
    wx.showLoading({
      title: '加载中'
    })
    wxService.get(`/sale/touchpoints/pointConsumptionActivity/findActivityProductList?id=${activityId}&pageNo=${pageNo}&pageSize=${pageSize}`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          let isMoreFlag = data.totalPages == 1 ? false : true
          let isNoMoreFlag = data.totalPages == 1 ? this : false
          this.setData({
            goodsList: this.data.pageNo == 1 ? [...data.content] : [...this.data.goodsList, ...data.content],
            totalPages: data.totalPages,
            totalElements: data.totalElements,
            isMore: isMoreFlag,
            isNoMore: isNoMoreFlag
          })
        }
      }
    })
  },
  getSysHeight() {
    wxService.getSystemInfo().then(res => {
      this.setData({
        windowHeight: res.windowHeight
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  onmessage(e) {
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const { activityId } = this.options
    if (activityId) {
      this.setData({
        activityId,
      }, () => {
        this.getSysHeight()
        this.findActivityProductList(activityId, 1, 50)
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(options) {
    const {
      path
    } = this.data;
    const enCodeUrl = encodeURIComponent(path);
    let sharePath = `/subPackage/page/pages/h5/h5page?url=${enCodeUrl}`;
    return {
      path: sharePath,
      'web-view': options.webViewUrl,
    };
  },

  // onShareAppMessage: function () {
  //   // 需要 encodeURIComponent path
  //   const {
  //     path
  //   } = this.data;
  //   const enCodeUrl = encodeURIComponent(path);
  //   let sharePath = `/subPackage/page/pages/h5/h5page?url=${enCodeUrl}`;
  //   return {
  //     // title: '分享给好友',
  //     path: sharePath,
  //     success() {
  //       wx2my.showToast({
  //         title: "转发成功",
  //         icon: 'success',
  //         duration: 2000
  //       });
  //     },

  //     fail() {
  //       wx2my.showToast({
  //         title: "转发失败",
  //         icon: 'none',
  //         duration: 2000
  //       });
  //     }

  //   };
  // }
});