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

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: '',
    data: {},
    isfirst: true
  },
  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.getData()
  },
  getDefaultBook() {
    util.query('accountbook/wxapi/getdefault', {}, function(res) {
      if (res.code === 10000) {

        that.setData({
          nowbook: res.data
        })
      }
    })
  },
  getData() {
    var data = this.data.param
    data = data ? data : {
      dim: 1,
      day: util.formatShortTime(new Date())
    }
    util.query('userinfo/wxapi/getdashboard', data, function(res) {
      res.totalAmount = res.totalAmount.toFixed(2)
      res.totalCost = res.totalCost.toFixed(2)
      res.totalFreight = res.totalFreight.toFixed(2)
      res.totalProfit = res.totalProfit.toFixed(2)
      res.expendAmount = res.expendAmount.toFixed(2)
      that.setData({
        data: res
      })
    })
  },
  getBookList() {
    util.query('accountbook/wxapi/list', {
      pageNo: 1,
      pageSize: 9999
    }, function(res) {
      if (res.code === 10000) {
        that.setData({
          books: res.data
        })
      } else {
        that.setData({
          books: []
        })
      }
    })
  },
  bookChange(e) {
    util.query('accountbook/wxapi/setdefault', {
      accountBookId: this.data.books[e.detail.value].id
    }, function(res) {
      if (res.code === 10000) {
        that.getDefaultBook()
        that.getData()
      }
    }, 'POST')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    util.checklogin(
      function() {
        that.getDefaultBook()
        that.getBookList()
        that.getData()
      }
    )
  },
  tostockrecord() {
    wx.navigateTo({
      url: '/pages/stock/stockrecord/stockrecord',
    })

  },
  tosuppliermanage() {
    wx.navigateTo({
      url: '/pages/stock/suppliermanage/suppliermanage',
    })
  },
  tocustommanage() {
    wx.navigateTo({
      url: '/pages/order/custommanage/custommanage',
    })
  },
  toaddstock() {
    wx.navigateTo({
      url: '/pages/stock/addstock/addstock',
    })
  },
  tostockchoise() {
    wx.navigateTo({
      url: '/pages/stock/choise/choise',
    })
  },
  toaddpr() {
    wx.navigateTo({
      url: '/pages/stock/payrecord/payrecord',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

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

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

  },

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

  },

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

  },

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

  }
})