// pages/exchange-record/exchange-record.js
import {
  getMpMemberCompaignsReward,
  getMpMemberCompaignsActivity
} from '../../../../services/index.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {

    title_list: [{ //标题数组
        name: '好礼'
      },
      {
        name: '活动'
      }
    ],
    active_index: 0, // 生效索引
    pageNo: 1, // 页数
    func: ['gift', 'activity'], // 方法数组
    list: [], // 兑换列表
  },
  // 更改方法
  changeIndex(e) {
    const active_index = e.currentTarget.dataset.index;
    if (active_index === this.data.active_index) {
      return
    }
    this.setData({
      active_index,
      type: this.data.func[active_index],
      pageNo: 1,
      list: []
    }, _ => {
      this[this.data.func[active_index]]();
    })
  },
  gift(pageNo = this.data.pageNo) {
    let params = {
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id'),
      pageNo
    }
    getMpMemberCompaignsReward(params).then(res => {
      if (res.errcode === 0) {
        if (pageNo === 1 && res.data.mpMemberCompaignsAPIList && res.data.mpMemberCompaignsAPIList.length === 0) {
          this.setData({
            show: false
          })
        } else {
          pageNo++;
          this.setData({
            list: [...this.data.list, ...res.data.mpMemberCompaignsAPIList],
            isLast: res.data.isLast,
            pageNo,
            show: true
          })
        }
      }
    })
  },
  activity(pageNo = this.data.pageNo) {
    let params = {
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id'),
      pageNo
    }
    getMpMemberCompaignsActivity(params).then(res => {
      if (res.errcode === 0) {
        if (pageNo === 1 && res.data.mpMemberCompaignsAPIList && res.data.mpMemberCompaignsAPIList.length === 0) {
          this.setData({
            show: false
          })
        } else {
          pageNo++;
          this.setData({
            list: [...this.data.list, ...res.data.mpMemberCompaignsAPIList],
            isLast: res.data.isLast,
            pageNo,
            show: true
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.type) {
      this[options.type]();
      this.setData({
        active_index: this.data.func.indexOf(options.type),
        type: options.type
      })
    }
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      pageNo: 1,
      list: []
    }, _ => {
      this[this.data.type]();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    const isLast = this.data.isLast;
    if (isLast) {

    } else {
      this[this.data.type]();
    }
  },
})