// pages/manager/thedata/pinlei-list.js
var wxCharts = require('../../../wx-charts/dist/wxcharts-min.js');
var lineChart = null;
var startPos = null;
const app = getApp()
var sever = require("../../../api.js").sever;
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
   category:'',
   days:0,
   list:[],
   count:[],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    backhome: function () {
      wx.reLaunch({
        url: '/page/homePage/homePage',
      })
    },
    onLoad: function (options) {
      var that = this
      that.setData({
        category: options.category,
        days: options.days
      })
    },
    onShow: function () {
      var that = this
      var days = that.data.days
      var category = that.data.category
      app.request({
        url:sever+'ordercount',
        data:{
          store_id:wx.getStorageSync('user').store_id,
          category: category,
          days: days
        },
        method:'get',
        success:function(res){
          console.log(res.data)
          if (res.data.data.length > 0) {
            var Arr = res.data.data
            var cat = []
            var category = []
            var cou = []
            var count = []

            for (var i = 0; i < Arr.length; i++) {
              var s = Arr[i].type
              cat.push(s)
              count.push(Arr[i].count)
            }

            for (var i = 0; i < cat.length; i++) {
              if (category.indexOf(cat[i]) == -1) {
                category.push(cat[i])
              }
            }

            for (var i = 0; i < category.length; i++) {
              cou[i] = 0;
              for (var j = 0; j < cat.length; j++) {
                if (cat[j] == category[i]) {
                  cou[i] += count[j]
                }
              }
            }
            that.setData({
              list: [...category],
              count: [...cou]
            })
          } else {
            that.setData({
              list: ['暂无'],
              count: [0]
            })
          }
          var select = that.data.select;
          var length = that.data.list.length * 100;
          that.setData({
            width: length + 'px'
          })
          var simulationData = that.createSimulationData();
          lineChart = new wxCharts({
            canvasId: 'columnCanvas',
            type: 'column',
            categories: that.data.list,
            series: [{
              name: '销售量',
              data: that.data.count
            }],
            yAxis: {
              format: function (val) {
                return val + '个';
              },
              min: 0,
            },
            width: 315,
            height: 300,
            dataLabel: true,
            dataPointShape: true,
            enableScroll: true,
            extra: {
              lineStyle: 'curve'
            }
          });
        }
      })
    },
    touchHandler: function (e) {
      lineChart.scrollStart(e);
    },
    moveHandler: function (e) {
      lineChart.scroll(e);
    },
    touchEndHandler: function (e) {
      lineChart.scrollEnd(e);
      lineChart.showToolTip(e, {
        format: function (item, category) {
          return category + ' ' + item.name + ':' + item.data
        }
      });
    },
    createSimulationData: function () {
      var categories = [];
      var data = [];
      for (var i = 0; i < 10; i++) {
        categories.push('201620162-' + (i + 1));
        data.push(Math.random() * (20 - 10) + 10);
      }
      return {
        categories: categories,
        data: data
      }
    },
  }
})
