// pages/statementOfAccount/statementOfAccount.js
var network = require("../../utils/net.js");
var timechage = require("../../utils/dateTimeUtil.js");
var pageindex = 0;
var pagesize = 10;
var year;
var month;
var method = '/MainSystem/B3MiniProgramRpc/XuRpcs/Customer/AccountBalanceRpc/GetBalanceList';

Page({
  data: {
    dates: new Date().getFullYear() + '-' + (new Date().getMonth() + 1)
  },

  bindDateChange: function(e) {
    this.setData({
      dates: e.detail.value
    })
    var year = this.data.dates.split('-')[0];
    var month = this.data.dates.split('-')[1];
    var i1 = parseInt(year)
    var i2 = parseInt(month)
    pageindex = 0;
    var params = [i1, i2, pageindex, pagesize];
    this.getBalanceList(params, true);
  },

  //滑动到底部加载
  addOtherTenOrder: function(event) {
    pageindex = pageindex + 1;
    var year = this.data.dates.split('-')[0];
    var month = this.data.dates.split('-')[1];
    var params = [year, month, pageindex, pagesize];
    this.getBalanceList(params, false);
  },

  onLoad: function(options) {
    pageindex = 0;
    var year = this.data.dates.split('-')[0];
    var month = this.data.dates.split('-')[1];
    var params = [year, month, pageindex, pagesize];
    this.getBalanceList(params, true);
  },

  getBalanceList: function(params, clear) {
    let that = this;
    network.transfer_request(method, params, function(res) {
      var array = JSON.parse(res.result);
      if (array.length <= 0) {
        wx.showToast({
          title: '无数据更新',
        })
        if (pageindex > 0) {
          pageindex = pageindex - 1;
        }
        return;
      }
      for (var i = 0; i < array.length; i++) {
        array[i].Date = timechage.formatTimeTwo(array[i].Date.substring(6, 19), "Y/M/D");
      }
      let result = array;
      if (!clear)
        result = that.data.list.concat(array);

      that.setData({
        list: result,
      })
    })
  },
})