// 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),
    list:[],
    delBtnWidth: 90,
    startX: ""
  },
  
  bindDateChange: function (e) {
    this.setData({
      dates: e.detail.value,
      list: [],
    })   
    var year=this.data.dates.split('-')[0];
    var month = this.data.dates.split('-')[1];
    // var day = this.data.dates.split('-')[2];
    var i1 = parseInt(year)
    var i2 = parseInt(month)
    // var i3 = parseInt(day)
    var that=this;
    pageindex = 0;
    var params = [i1, i2, pageindex,pagesize];
    this.getBalanceList(that, params);
  },

  //滑动到底部加载
  addOtherTenOrder: function (event) {
    var that = this;
    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(that, params);
  },

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

  getBalanceList : function (that,params) {
    var reslut = [];
    network.requestLoading(method, params, function (res) {
      console.log(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");
       }
      console.log(array);
      let arrLast = that.data.list.concat(array);
      that.setData({
        list: arrLast
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  touchS: function (e) {
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function (e) {
    if (e.touches.length == 1) {
      //手指移动时水平方向位置
      var moveX = e.touches[0].clientX;
      //手指起始点位置与移动期间的差值
      var disX = this.data.startX - moveX;
      var delBtnWidth = this.data.delBtnWidth;
      var txtStyle = "";
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，说明向右滑动，文本层位置不变
        txtStyle = "left:0px";
      } else if (disX > 0) {//移动距离大于0，文本层left值等于手指移动距离
        txtStyle = "left:-" + disX + "px";
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度
          txtStyle = "left:-" + delBtnWidth + "px";
        }
      }
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var list = this.data.list;
      list[index].txtStyle = txtStyle;
      //更新列表的状态
      this.setData({
        list: list
      });
    }
  },
  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var list = this.data.list;
      list[index].txtStyle = txtStyle;
      //更新列表的状态
      this.setData({
        list: list
      });
    }
  },
  
  //点击删除按钮事件
  delItem: function (e) {
    //获取列表中要删除项的下标
    var index = e.currentTarget.dataset.index;
    var list = this.data.list;
    //移除列表中下标为index的项
    list.splice(index, 1);
    //更新列表的状态
    this.setData({
      list: list
    });
  },
})