<template>
  <div :class="className" :style="{height:height,width:width}" />



</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'



import { shopchart,orderchart,zhucechart } from '@/api/index'


const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    typelogo:{
      type: String,
      default: 'chart'
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      msg: 'Welcome to Your Vue.js App'
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptionVal(colortmp,dateVal){

      var day = []
      var num = []

      dateVal.forEach(function(item) {
        day.push(item.time)
        num.push(item.num)
      })
      this.chart.setOption({
        color:[colortmp],
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data:  day,
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{

          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: num,
          animationDuration
        }]
      })
    },
    initChart() {

      this.chart = echarts.init(this.$el, 'macarons')

      if (this.typelogo==="chart"){
        return;
      }

      let  colorList=['#FBC300','#9BCA63','#C1232B','#ADD8E6'];
      let colortmp=colorList[0];
      if (this.typelogo==="countuser"){
        colortmp=colorList[0];

        zhucechart().then(response => {
          console.log(response.data)

          this.setOptionVal(colortmp,response.data.chart);
        });


      }
      if (this.typelogo==="countshop"){
        colortmp=colorList[1];

        shopchart().then(response => {
          console.log(response.data)

          this.setOptionVal(colortmp,response.data.chart);
        });


      }
      if(this.typelogo==="countorder"){
        colortmp=colorList[2];
        orderchart().then(response => {
          console.log(response.data)

          this.setOptionVal(colortmp,response.data.chart);
        });

      }



    }
  }
}
</script>
