let util = require('../../../utils/util.js');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: util.imgurl,
    data: [],
    isfirst: true,
    typeindex: 0,
    types: [],

  },
  dateChange(e) {
    console.log(e)
    this.setData({
      param: {
        dim: e.detail.type,
        day: e.detail.year + '-' + e.detail.month + '-' + e.detail.day,
        month: e.detail.year + '-' + e.detail.month,
        year: e.detail.year,
        beginDate: e.detail.stime,
        endDate: e.detail.etime
      }
    })
    this.getList()
  },
  toadd(e) {
    wx.navigateTo({
      url: '/pages/stock/addpayrecord/addpayrecord',
    })
  },
  toedite(e) {
    wx.navigateTo({
      url: '/pages/stock/addpayrecord/addpayrecord?id=' + e.currentTarget.dataset.id,
    })
  },
  getList() {
    var data = this.data.param
    data = data ? data : {
      dim: 1,
      day: util.formatShortTime(new Date()),
      pageNo: 1,
      pageSize: 9999
    }
    if (this.data.typeindex && this.data.typeindex != '0') {
      data.expendTypeId = this.data.types[this.data.typeindex].id
    } else {
      delete data.expendTypeId
    }
    util.query('expend/wxapi/listpage', data, function(res) {
      if (res.code === 10000) {
        var total = 0
        res.data.content.forEach(function(e) {
          e.time = e.dateStr
          total += e.money
        })
        that.setData({
          data: res.data.content,
          total: total.toFixed(2)
        })
      } else {
        that.setData({
          data: [],
          total: 0
        })
      }
    })
  },
  typechange(e) {
    this.setData({
      typeindex: e.detail.value
    })
    this.getList()
  },
  getTypes() {
    util.query('expendtype/wxapi/list', {
      pageNo: 1,
      pageSize: 99999
    }, function(res) {
      if (res.code === 10000) {
        res.data.unshift({
          id: 0,
          name: '全部'
        })
        that.setData({
          types: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    this.getList()
    this.getTypes()
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!this.data.isfirst) {
      this.getList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      isfirst: false
    })
  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

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

  }
})