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

  /**
   * 页面的初始数据
   */
  data: {

    data: {
      type: 1,
      dim: 1,
      day: util.formatShortTime(new Date())
    },
    sorttype: 'down',
    index: 2
  },
  sort(e) {
    var index = parseInt(e.currentTarget.dataset.index)
    this.setData({
      index: index,
      sorttype: this.data.sorttype === 'up' ? 'down' : 'up'
    })
    this.setData({
      list: this.sortdata(this.data.list)
    })
  },
  sortdata(list) {
    var index = this.data.index
    list = list.sort(function(a, b) {
      if (that.data.sorttype === 'up') {
        return a[index] - b[index]
      } else {
        return b[index] - a[index]
      }
    })

    for (var i = 0; i < list.length; i++) {
      var classname = ''
      if (i === 0) {
        classname = 'first'
      } else if (i === 1) {
        classname = 'second'
      } else if (i === 2) {
        classname = 'third'
      }
      list[i][0] = classname
    }
    return list
  },
  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()
  },
  typechage(e) {
    var data = this.data.data
    data.type = e.detail.type
    this.setData({
      data: data
    })
    this.getData()
  },

  getData() {
    var data1 = this.data.data
    util.query('userinfo/wxapi/getReport', data1, function(res) {
      var data = []
      if (!res.data || res.data.length === 0) {
        that.setData({
          list: []
        })
        return
      }
      if (that.data.data.type === 1) {
        for (var i = 0; i < res.data.length; i++) {
          data.push(['', res.data[i].supplierName, res.data[i].stockNum, res.data[i].selledNum, res.data[i].profit])
        }


      }
      if (that.data.data.type === 2) {
        for (var i = 0; i < res.data.length; i++) {

          data.push(['', res.data[i].goodsName, res.data[i].selledNum, res.data[i].amount, res.data[i].profit])
        }

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})