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

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { getStoreOrderData } from "@/api/index";

const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart1'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      dates:[],
      orderCount:[],
      payPrice:[]

    }
  },
  mounted() {
    this.getStoreOrderData()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getStoreOrderData(){
      var that=this;
      getStoreOrderData().then((response) => {
          this.dates=response.dates;
          this.orderCount=response.orderCount;
          this.payPrice=response.payPrice;
          setTimeout(() => {
            that.initEchart();
          }, 500);
      });
    },
    initEchart(){
      var option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        legend: {
            data: ['订单数', '订单金额']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                data: this.dates
            }
        ],
        yAxis: [
            {
                type: 'value',
                axisLabel:{
                  formatter:'{value}'
                }
            }
        ],
        series: [
            {
                
                name: '订单数',
                type: 'line',
                emphasis: {
                    focus: 'series'
                },
                data: this.orderCount
            },
            {
                
                name: '订单金额',
                type: 'line',
                emphasis: {
                    focus: 'series'
                },
                data: this.payPrice
            } 
        ]
      };
      this.chart=echarts.init(document.getElementById("echart-customer1"));
      this.chart.setOption(option,true);
    },
  }
  
  
}
</script>
