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

  /**
   * 页面的初始数据
   */
  data: {
    data: {
      type: 1,
      dim: 1,
      day: util.formatShortTime(new Date())
    },
    sorttype: 'down',
    index: 1,
    number1: 0,
    price1: 0,
    number2: 0,
    price2: 0,
    number3: 0,
    price3: 0,
    baseImgUrl: util.baseImgUrl,
    goodsName1: '',
    goodsName2: '',
    ussindex: 0
  },
  ussChane(e) {
    this.setData({
      ussindex: e.detail.value
    })
    this.getData(1)
    this.getData(2)
    this.getData(3)
  },
  getDefaultBook() {
    util.query('accountbook/wxapi/getdefault', {}, function(res) {
      if (res.code === 10000) {
        res.data.userInfos.unshift({
          key: 0,
          wechatName: '全部'
        })
        that.setData({
          uss: res.data.userInfos
        })
      }
    })
  },
  search1(e) {
    this.setData({
      goodsName1: e.detail.value
    })
    this.getData(1)
  },
  search2(e) {
    this.setData({
      goodsName2: e.detail.value
    })
    this.getData(2)
  },
  dateChange(e) {
    console.log(e)
    var data = this.data.data
    data.dim = e.detail.type + ''
    if (data.dim === '1') {
      data.day = e.detail.year + '-' + e.detail.month + '-' + e.detail.day
      data.month = ''
      data.year = ''
    } else if (data.dim === '2') {
      data.month = e.detail.year + '-' + e.detail.month
      data.day = ''
      data.year = ''
    } else if (data.dim === '3') {
      data.year = e.detail.year
      data.month = ''
      data.day = ''
    } else if (data.dim === '4') {
      data.beginDate = e.detail.stime
      data.endDate = e.detail.etime
    }

    this.setData({
      data: data
    })
    this.getData(1)
    this.getData(2)
    this.getData(3)
  },
  setindex(e) {

    this.setData({
      index: e.currentTarget.dataset.index
    })

  },
  clear1() {
    this.setData({
      goodsName1: ''
    })
    this.getData(1)
  },
  clear2() {
    this.setData({
      goodsName2: ''
    })
    this.getData(2)
  },
  getData(kind) {
    var data1 = this.data.data
    data1.type = kind
    if (kind === 1) {
      data1.goodsName = this.data.goodsName1
    } else if (kind === 2) {
      data1.goodsName = this.data.goodsName2
    }
    if (this.data.ussindex != 0) {
      data1.uid = this.data.uss[this.data.ussindex].uid
    }
    util.query('userinfo/wxapi/businessReport', data1, function(res) {
      if (res.code === 10000) {

        if (kind === 1) {
          res.data.forEach(function(e) {
            e.submitDate = e.submitDate ? e.submitDate.split(' ')[0] : ''
          })
          that.setData({
            number1: res.purchaseNumber,
            price1: res.purchasePrice.toFixed(2),
            list1: res.data
          })
        } else if (kind === 2) {
          res.data.forEach(function(e) {
            e.submitDate = e.submitDate ? e.submitDate.split(' ')[0] : ''
          })
          that.setData({
            number2: res.sellNumber,
            price2: res.sellPrice.toFixed(2),
            list2: res.data
          })
        } else {

          var data = []
          res.data.forEach(function(e) {
            var name = ''
            that.data.payWayarray.forEach(function(a) {
              if (a.key.toString() === e.payWay) {
                name = a.name
              }
            })

            data.push({
              name: name,
              amount: e.amount
            })
          })

          that.setData({
            price3: res.realReceive.toFixed(2),
            list3: data
          })
        }

      } else {
        if (kind === 1) {
          that.setData({
            number1: 0,
            price1: 0,
            list1: []
          })
        } else if (kind === 2) {
          that.setData({
            number2: 0,
            price2: 0,
            list2: []
          })
        } else {
          that.setData({
            price3: 0,
            list3: []
          })
        }

      }

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    util.getPayWay(function(e) {
      that.setData({
        payWayarray: e
      })
    })
    this.getDefaultBook()
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

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

  },

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

  },

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

  },

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

  }
})