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

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

const animationDuration = 6000

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

    }
  },
  mounted() {
    this.getStorePaymentData()
    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: {
    getStorePaymentData(){
      var that=this;
      getStorePaymentData().then((response) => {
          this.dates=response.dates;
          this.orderCount=response.orderCount;
          this.payMoney=response.payMoney;
          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.payMoney
            } 
        ]
      };
      this.chart=echarts.init(document.getElementById("echart-customer2"));
      this.chart.setOption(option,true);
    },
  }
  
  
}
</script>
