// pages/income-details/income-details.js
var http = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "unsettledAmount": 0, // 待结算金额
    "settledAmount": 0, // 可提现金额
    "invalidAmount": 0, // 已失效金额
    "addupAmount": 0, // 累计收益
    "monthAmount": 0, // 本月收益
    "todayAmount": 0, // 今日收益
    records: [],
    loadingMsg: "正在加载..",
    emptyMsg: "",
    totalPage: 1,
    pageNum: 1,
  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 获取钱包数据
    this.getDisWalletData()
    // 获取收益数据
    this.getDisIncomeData()
    // 获取收益列表
    this.loadPageData(1)
  },

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

  },

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

  },

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

  },

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

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

  },

  /**
   * 获取我的钱包信息
   */
  getDisWalletData: function() {
    var ths = this
    wx.showLoading();
    var params = {
      url: "/p/distribution/wallet/info",
      method: "GET",
      // data: {
      //   shopId: 1
      // },
      callBack: function(res) {
        wx.hideLoading();
        ths.setData({
          "unsettledAmount": res.unsettledAmount,
          "settledAmount": res.settledAmount,
          "invalidAmount": res.invalidAmount,
          "addupAmount": res.addupAmount
        });
      }
    };
    http.request(params);
  },

  /**
   * 获取用户本月收益和今日收益
   */
  getDisIncomeData: function() {
    var ths = this
    wx.showLoading();
    var params = {
      url: "/p/distribution/income/statistics",
      method: "GET",
      // data: {
      //   shopId: 1
      // },
      callBack: function(res) {
        wx.hideLoading();
        ths.setData({
          "todayAmount": res.todayAmount,
          "monthAmount": res.monthAmount
        });
      }
    };
    http.request(params);
  },

  /**
   * 收入列表
   */
  loadPageData: function (pageNum) {
    var ths = this;
    var params = {
      url: "/p/distribution/income/page",
      method: "GET",
      data: {
        // shopId: 1,
        current: pageNum,
        size: 20
      },
      callBack: (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 = "没有更多了~";
        }
        console.log(res)
        ths.setData({
          records: records,
          totalPage: res.pages,
          pageNum: pageNum,
          loadingMsg: loadingMsg,
          emptyMsg: "今天还没有收益哦~"
        });
      }
    };
    http.request(params);
  }
})