// pages/promotion-order/promotion-order.js
var http = require("../../utils/http.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sts: 0,
    state: '',
    records: [],
    loadingMsg: "正在加载..",
    emptyMsg: "",
    totalPage: 1,
    pageNum: 1,
  },

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

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.loadPageData(1)
  },

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

  },

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

  /**
   * 点击切换事件 0:全部
   */
  onStsTap: function(e) {
    let state = e.currentTarget.dataset.state
    this.setData({
      state: state,
      current: 1,
      pages: 0
    })
    wx.showLoading({
      mask:true
    })
    this.loadPageData(1);
  },

  /**
   * 通过状态加载分销订单列表
   */
  loadPageData: function (pageNum) {
    let ths = this;
    var state = ths.data.state;
    if (state === "0") {
      state = ''
    }
    var params = {
      url: "/p/distribution/income/myPromotionOrder",
      method: "GET",
      data: {
        current: pageNum,
        size: 20,
        state: state
      },
      callBack: (res) =>{
        console.log(res)
        wx.hideLoading();
        var records = [];
        if (res.current == 1) {
          records = res.records;
        } else {
          records = this.data.records;
          Array.prototype.push.apply(records, res.records);
        }
        var loadingMsg = this.data.loadingMsg;
        if (pageNum == res.pages) {
          loadingMsg = "没有更多了~";
        }
        ths.setData({
          records: records,
          totalPage: res.pages,
          pageNum: pageNum,
          loadingMsg: loadingMsg,
          emptyMsg: "没有找到相关记录"
        });
      }
    }
    http.request(params);
  },

})