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

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: util.imgurl,
    baseImgUrl: util.baseImgUrl
  },
  tosettle() {
    var data = [
      //   {
      //   goodid: 234776,
      //   goodname: '红米手机',
      //   price: 10,
      //   goodonlyid: '928502452021897932341492096432414',
      //   goodsBatchid: 308787,
      //   goodsBatchonlyid: '902801730046553607347570690271441'

      // }
    ]
    this.data.data.forEach(function(e) {
      if (e.goodsBatchList) {
        e.goodsBatchList.forEach(function(res) {
          if (res.sel) {
            data.push({
              goodid: res.goodsBatch.goods.id,
              goodname: res.goodsBatch.goods.name,
              price: res.goodsBatch.purchasePrice,
              sale: res.goodsBatch.prices1,
              goodonlyid: res.goodsBatch.goods.onlyID,
              goodsBatchid: res.goodsBatch.id,
              goodsBatchonlyid: res.goodsBatch.onlyID
            })
          }
        })
      }

    })
    if (data.length === 0) {
      util.showToast("请选择商品")
      return
    }
    wx.navigateTo({
      url: '/pages/stock/settlement/settlement?goods=' + JSON.stringify(data),
    })
  },
  down(e) {
    var da = this.data.data
    da[e.currentTarget.dataset.index].sel = true
    this.setData({
      data: da
    })
  },
  up(e) {
    var da = this.data.data
    da[e.currentTarget.dataset.index].sel = false
    this.setData({
      data: da
    })
  },
  setsel(e) {
    var da = this.data.data
    da[e.currentTarget.dataset.index].goodsBatchList[e.currentTarget.dataset.index1].sel = !da[e.currentTarget.dataset.index].goodsBatchList[e.currentTarget.dataset.index1].sel
    this.setData({
      data: da
    })
  },
  getData() {
    util.query('goodsbatch/wxapi/listbygroup', {}, function(res) {
      if (res.code === 10000) {
        var da = []
        res.data.forEach(function(e) {
          if (e.goods && e.goodsBatchList && e.goodsBatchList.length > 0) {
            var all = 0,
              allcb = 0,
              allcount = 0
            e.goodsBatchList.forEach(function(r) {
              r.goodsBatch.time = r.goodsBatch.createTime ? (r.goodsBatch.createTime.split(' ')[0]) : ''
              r.goodsBatch.sel = false
              r.goodsBatch.allcb = (r.goodsBatch.purchasePrice ? r.goodsBatch.purchasePrice : 0) * (r.stockAmount ? r.stockAmount : 0)
              all += (r.goodsBatch.purchasePrice ? r.goodsBatch.purchasePrice : 0)
              allcb += r.goodsBatch.allcb
              allcount += r.stockAmount
            })
            da.push({
              goods: e.goods,
              goodsBatchList: e.goodsBatchList,
              perprice: (all / e.goodsBatchList.length).toFixed(2),
              allcb: allcb,
              type: e.goodsBatchList.length > 1 ? 2 : 1,
              sel: false,
              ch: false,
              time: e.goodsBatchList[0].goodsBatch.createTime ? (e.goodsBatchList[0].goodsBatch.createTime.split(' ')[0]) : '',
              allcount: allcount
            })
          }
        })
        da = da.sort(function(a, b) {
          return b.goodsBatchList[0].goodsBatch.id - a.goodsBatchList[0].goodsBatch.id
        })
        that.setData({
          data: da
        })
      } else {

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})