<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'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    numberData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      dateArr: [],
      yAsix:10000,
    }
  },
  watch: {
    numberData: {
      deep: true,
      handler(val) {
        this.dateArr= val.date
        this.numberMax(this.numberData.ordercount,this.numberData.customerprice)
        this.setOptions(val)
      }
    }
  },
  created() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  mounted() {

    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    numberMax(num1,num2){
      if(num1,num2){
        let result = num1.every(function(item,index,array){
          return item == 0
        })
        let result2 = num2.every(function(item,index,array){
          return item == 0
        })
        if(result||result2){
          this.yAsix=100
        }else{
          var c = num1.concat(num2)
          this.yAsix =  Math.ceil(Math.max(...c))
        }
      }
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.numberData)
    },
    setOptions({ordercount,customerprice,ordertotal,customerArr,dateArr,retentionArr} = {}) {
      this.chart.setOption({
        xAxis: {
          data: this.dateArr,
          boundaryGap: true,
          axisTick: {
            show: true
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: [
          {
          	type: 'value',
          	name: '交易量',
          	min: 0,
          	max: this.yAsix,
          	interval: Math.ceil(this.yAsix/5),
          	axisLabel: {
          		formatter: '{value}'
          	}
          },
        ],
        legend: {
          data: ['总订单数','总订单金额','客单价']
        },
        series: [{
          name: '总订单数', itemStyle: {
            normal: {
              color: '#4784FF',
              lineStyle: {
                color: '#4784FF',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'bar',
          data: ordercount,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        },
        {
          name: '总订单金额',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#EE7945',
              lineStyle: {
                color: '#EE7945',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: ordertotal,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: '客单价',
          smooth: true,
          type: 'bar',
          // yAxisIndex: 1,
          itemStyle: {
            normal: {
              color: '#49D3CE',
              lineStyle: {
                color: '#49D3CE',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: customerprice,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>
