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

  /**
   * 页面的初始数据
   */
  data: {
    records : [],
    pageNum : 1,
    pageSize : 10,
    isNoMore : false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    this.getBuyRecordsList();
  },

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

  },

  //复制
  onTapCopy(e){
    let value = e.currentTarget.dataset.value;
    wx.setClipboardData({
      //准备复制的数据
      data: value,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        });
      }
    });
  },

  //获取购买记录
  getBuyRecordsList(refresh){
    const { memberId } = wx.getStorageSync('_baseUserInfo');
    if (!memberId) {
      return;
    }
    
    let pageNum = this.data.pageNum,
      pageSize = this.data.pageSize;

    let url = `/merchant/superValueCard/miniProgram/buyRecord/getPageList?memberId=${memberId}&pageNum=${pageNum}&pageSize=${pageSize}`;
    wxService.post(url).then(res => {
      if(res){
        let list = res.data.data ? res.data.data : [];
        if (list.length == 0 && this.data.pageNum > 1){
          this.data.isNoMore = true;
        } 
        else {
          this.data.isNoMore = false;
        }

        list.forEach(item => {
          item.startTime = item.startTime.substring(0, 10);
          item.endTime = item.endTime.substring(0,10);
          if (item.buyAwayType == 1){
            item.buyPriceText = parseFloat(item.buyPrice/10/10).toFixed(2);
          }
        });

        this.data.records = this.data.records.concat(list);

        this.setData({
          isNoMore: this.data.isNoMore,
          records: this.data.records
        });
        
        
      }

      if(refresh){
        wx.stopPullDownRefresh();
        wx.showToast({
          title: '刷新成功',
        });
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageNum = 1;
    this.data.records = [];
    this.data.isNoMore = false;
    this.getBuyRecordsList(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.isNoMore){
      this.data.pageNum ++ ;
      this.getBuyRecordsList();
    }
  },

})