// packageC/cashier/income_breakdown/income_breakdown.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    isLoadMore: true,
    total_page: 0,
    page: 1,
    todayList: [],
    yesterdayList: [],
    monthList: [],
    lastMonthList: [],
    getType: 'today',
    total_price: '',
    total: ''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.getData();
  },
  swichTabTItem(e) {
    let index = e.detail.index;
    if (index == 1) {
      this.data.getType = 'yesterday';
    } else if (index == 3) {
      this.data.getType = 'month';
    } else if (index == 2) {
      this.data.getType = 'lastMonth';
    } else {
      this.data.getType = 'today';
    }
    this.data.isLoadMore = true;
    this.data.total_page = 0;
    this.data.page = 1;
    this.getData();
  },
  getData() {
    let that = this;
    let url = '';
    if (this.data.getType == 'today') {
      url = 'plugin.store-cashier.frontend.cashier.income.today';
    } else if (this.data.getType == 'yesterday') {
      url = 'plugin.store-cashier.frontend.cashier.income.yesterday';
    } else if (this.data.getType == 'month') {
      url = 'plugin.store-cashier.frontend.cashier.income.this-month';
    } else if (this.data.getType == 'lastMonth') {
      url = 'plugin.store-cashier.frontend.cashier.income.last-month';
    }
    let urlStr = app.getNetAddresss(url);
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.page
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (that.data.getType == 'today') {
            that.setData({
              todayList: res.data.data,
              total_price: res.data.total_price,
              total: res.data.total
            });
          } else if (that.data.getType == 'yesterday') {
            that.setData({
              yesterdayList: res.data.data,
              total_price: res.data.total_price,
              total: res.data.total
            });
          } else if (that.data.getType == 'month') {
            that.setData({
              monthList: res.data.data,
              total_price: res.data.total_price,
              total: res.data.total
            });
          } else if (that.data.getType == 'lastMonth') {
            that.setData({
              lastMonthList: res.data.data,
              total_price: res.data.total_price,
              total: res.data.total
            });
          }
          that.setData({
            total_page: res.data.last_page
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  _getMoreData() {
    var that = this;
    let url = '';
    if (this.data.getType == 'today') {
      url = 'plugin.store-cashier.frontend.cashier.income.today';
    } else if (this.data.getType == 'yesterday') {
      url = 'plugin.store-cashier.frontend.cashier.income.yesterday';
    } else if (this.data.getType == 'month') {
      url = 'plugin.store-cashier.frontend.cashier.income.this-month';
    } else if (this.data.getType == 'lastMonth') {
      url = 'plugin.store-cashier.frontend.cashier.income.last-month';
    }
    if (this.data.page == this.data.total_page) {
      return;
    }
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss(url);
      urlStr += '&page=' + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            if (that.data.getType == 'today') {
              that.setData({
                todayList: that.data.todayList.concat(res.data.data)
              });
            } else if (that.data.getType == 'yesterday') {
              that.setData({
                yesterdayList: that.data.yesterdayList.concat(res.data.data)
              });
            } else if (that.data.getType == 'month') {
              that.setData({
                monthList: that.data.monthList.concat(res.data.data)
              });
            } else if (that.data.getType == 'lastMonth') {
              that.setData({
                lastMonthList: that.data.lastMonthList.concat(res.data.data)
              });
            }
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }

        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

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

  },

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

  },

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

  },

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

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

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

  }
});
