// subPackage/page/pages/pointGoodsList/pointGoodsList.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
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  },
  // 活动下商品列表 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
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { activityId} = this.options
    if (activityId){
      this.setData({
        activityId,
      },()=> {
        this.getSysHeight()
        this.findActivityProductList(activityId, 1, 50)
      })
    }
  },
  getSysHeight() {
    wxService.getSystemInfo().then(res=>{
      this.setData({
        windowHeight: res.windowHeight
      })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const {  pageNo, pageSize, totalPages, activityId } =  this.data
    if ( pageNo < totalPages) {
      this.setData({
        pageNo: pageNo + 1,
      },()=> {
        this.findActivityProductList(activityId,this.data.pageNo,pageSize)
      })
      } else {
      this.setData({
        isNoMore: true,
        isMore: false
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})