// pages/member/incomedetails/incomedetails.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    sType: '',
    page: 1,
    total_page: 0,
    datas: [],
    loading: false,
    allLoaded: false,
    isLoadMore: true,
    typeData: [],
    popupSpecs: false,
  },

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

  },

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

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    this.setData({
      sType: '',
      page: 1,
      total_page: 0,
      datas: [],
      loading: false,
      allLoaded: false,
      isLoadMore: true,
      typeData: [],
      popupSpecs: false,
    });
    this._getData(this.data.sType);
  },

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

  },

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

  },

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

  },

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

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

  },
  //获取数据
  _getData(_type) {
    let that = this;
    let urlStr = app.getNetAddresss('finance.income.get-income-list');
    urlStr += '&income_type=' + _type;
    urlStr += '&page=' + this.data.page;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            total_page: res.data.last_page,
            datas: res.data.data,
          });
        }
      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  //获取更多
  _getMoreData(page, _type) {
    var that = this;
    if (this.data.page == this.data.total_page) {
      return;
    }
    if (this.data.page >= this.data.total_page) {
      that.setData({
        loading: true,
        allLoaded: true
      });
      return;

    } else {
      that.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss('finance.income.get-income-list');
      urlStr += '&income_type=' + _type;
      urlStr += '&page=' + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var myData = res.data;
            that.setData({
              loading: false,
              allLoaded: false,
              datas: that.data.datas.concat(myData.data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              loading: true,
              allLoaded: true,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function(res) {
          console.log(res.msg);
        }
      });
    }
  },
  //筛选
  screen() {
    if (this.data.typeData.length == 0) {
      this._getTypeData();
    }
    this.setData({
      popupSpecs: true
    });
  },
  //获取类型数据
  _getTypeData() {
    let that = this;
    let urlStr = app.getNetAddresss('finance.income.get-search-type');
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            typeData: res.data
          });
        }
      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  godetailsinfo(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageA/member/income_details_info/income_details_info?id=" + id
    });
  },
  //点击筛选处理
  screenType(e) {
    let _type = e.currentTarget.dataset.type;
    this.setData({
      sType: _type,
      popupSpecs: false
    });
    this.initData();
    this._getData(this.data.sType);
  },
  //初始化数据
  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      goload: true,
      loading: true,
      allLoaded: false,
      isLoadMore: true,
      datas: []
    });
  },
  screenTypeClose() {
    this.setData({
      popupSpecs: false
    });
  }
});
