var app = getApp()
const util = require('../../../utils/utils.js')
Page({
  data: {
    arr: '',
    years: '',
    month: '',
    total_amount: 0,
    link: null,
    index: 0,
    open_day_rent: wx.getStorageSync('setting').is_open_day_rent,
    img:app.globalData.imgUrl
  },
  onLoad: function (options) {
    var that = this;
    that.initial();
  },
  initial: util.throttle(function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    var date = new Date();
    that.setData({
      years: date.getFullYear(),
      month: date.getMonth() + 1
    })
    if (app.globalData.req) {
      app.request('/orders?year=' + date.getFullYear() + '&month=' + that.data.month, '', 'GET', app.globalData.req).then(res => {
        console.log(res)
        var total_amount = 0;
        if (res.statusCode == 200) {
          var arr = res.data.data;
          that.setData({
            arr
          })
          if (res.data.meta.pagination.links != null) {
            that.setData({
              link: res.data.meta.pagination.links.next
            })
          }
          for (var i = 0; i < arr.length; i++) {
            total_amount = total_amount + Number(arr[i].total_money)
          }
          console.log(total_amount)
          that.setData({
            total_amount
          })
          wx.hideLoading({
            complete: (res) => {},
          })
        }
      }).catch(err => {
        console.log(err)
      })
    }

  }, 1000),
  tabbar: util.throttle(function (e) {
    var that = this;
    that.setData({
      index: e.currentTarget.dataset.index
    })
    var date = new Date();
    that.setData({
      years: date.getFullYear(),
      month: that.data.month
    })
    var url = '/orders'
    if (e.currentTarget.dataset.index == 1) {
      url = '/rent/orders'
    } else {
      url = '/orders'
    }
    app.request(url + '?year=' + date.getFullYear() + '&month=' + that.data.month, '', 'GET').then(res => {
      console.log(res)
      var total_amount = 0;
      if (res.statusCode == 200) {
        var arr = res.data.data;
        that.setData({
          arr
        })
        if (res.data.meta.pagination.links != null) {
          that.setData({
            link: res.data.meta.pagination.links.next
          })
        }
        for (var i = 0; i < arr.length; i++) {
          total_amount = total_amount + Number(arr[i].total_money)
        }
        console.log(total_amount)
        that.setData({
          total_amount
        })
      }
    }).catch(err => {
      console.log(err)
    })
  }, 1000),
  onReady: function () {

  },
  getDateTime(e) {
    var that = this;
    console.log(e.detail.value)
    var month = e.detail.value.substring(5)
    var years = e.detail.value.substring(0, 4)
    this.setData({
      month,
      years
    })
    var url = ''
    if (that.data.index == 1) {
      url = '/rent/orders'
    } else {
      url = '/orders'
    }
    app.request(url + '?year=' + years + '&month=' + month, '', 'GET').then(res => {
      console.log(res)
      var total_amount = 0;
      if (res.statusCode == 200) {
        var arr = res.data.data;
        that.setData({
          arr
        })
        for (var i = 0; i < arr.length; i++) {
          total_amount = total_amount + Number(arr[i].total_money)
        }
        console.log(total_amount)
        that.setData({
          total_amount
        });
      }
    }).catch(err => {
      console.log(err)
    })
  },
  detail: util.throttle(function (e) {
    wx.navigateTo({
      url: '../../my_riding/my_riding?order=' + e.currentTarget.dataset.order + '&index=' + this.data.index,
    })
  }, 1000),
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    var that = this;
    var date = new Date();
    var url = ''
    if (that.data.index == 1) {
      url = '/rent/orders'
    } else {
      url = '/orders'
    }
    app.request(url + '?year=' + date.getFullYear() + '&month=' + that.data.month, '', 'GET').then(res => {
      console.log(res)
      var total_amount = 0;
      if (res.statusCode == 200) {
        var arr = res.data.data;
        that.setData({
          arr
        })
        if (res.data.meta.pagination.links != null) {
          that.setData({
            link: res.data.meta.pagination.links.next
          })
        }
        for (var i = 0; i < arr.length; i++) {
          total_amount = total_amount + Number(arr[i].total_money)
        }
        console.log(total_amount)
        that.setData({
          total_amount
        });
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    }).catch(err => {
      console.log(err)
    })
  },
  onReachBottom: function () {
    var that = this;
    if (that.data.link != null) {
      wx.showLoading({
        title: '加载中...',
      })
      console.log(link)
      wx.request({
        url: that.data.link,
        method: 'GET',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Authorization': wx.getStorageSync('token'),
        },
        success: function (res) {
          console.log(res)
          var total_amount = 0;
          var money = that.data.total_amount;
          if (res.statusCode == 200) {
            var arr = res.data.data;
            that.setData({
              arr: that.data.arr.concat(arr),
              link: res.data.meta.pagination.links.next
            })
            for (var i = 0; i < arr.length; i++) {
              total_amount = total_amount + Number(arr[i].total_money)
            }
            console.log(total_amount)
            that.setData({
              total_amount: that.data.total_amount + money
            });
            wx.hideLoading()
          } else {
            wx.hideLoading()
          }
        }
      })
    }
    if (that.data.link == null) {
      wx.showToast({
        title: '暂无更多数据~',
        icon: 'none'
      })
    }
  },
  onShareAppMessage: function () {
    
  }
})