// subPackage/page/pages/recordList/recordList.js
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    pageSize: 10,
    totalPage: 0,
    recordList: [],
    hasMore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '正在加载数据..',
    });

    this.getWxchangeRecords();
  },


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

  },

  //获取兑换记录
  getWxchangeRecords() {
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    let pageNo = this.data.pageNo,
      pageSize = this.data.pageSize;
    wxService.post(`/member/pointsRedemptionProductRecord/page?pageNo=${pageNo}&pageSize=${pageSize}`, {
      memberId: memberId
    }).then(res => {
      let list = res.data.data.content ? res.data.data.content : [];
      this.data.totalPage = res.data.data.totalPages;
      list.forEach(item => {
        let proviceCity = item.exchangeInfo.distinct.split(',');
        item.title = item.title ? item.title : '--';
        item.fullAddress = [proviceCity, item.exchangeInfo.address].join('');
      });
      if (list.length > 0) {
        this.data.hasMore = true;
      }

      this.data.recordList = this.data.recordList.concat(list);
      this.setData({
        totalPage: this.data.totalPage,
        recordList: this.data.recordList,
        hasMore: this.data.hasMore
      });
    });
  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.pageNo < this.data.totalPage) {
      this.data.pageNo++;
      this.getWxchangeRecords();
    }
    else {
      if (this.data.recordList.length != 0) {
        this.setData({
          hasMore: false,
        })
      }
    }
  },

})