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

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

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: document.body.offsetWidth * 0.7 + 'px'
    },
    type: {
      type: String,
      default: 'line'
    },
    chartData: {
      type: Object,
      default() {
        return {
          top: [],
          tj: [],
          time: []
        }
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      handler() {
        this.initChart()
      },
      deep: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '最新报名趋势图',
          top: 8,
          textStyle: {
            color: '#000000',
            fontSize: 16,
            fontWeight: 'bold'
          }
        },
        grid: {
          left: 4,
          right: 12,
          bottom: 8,
          containLabel: true
        },
        color: ['#4C9ECD', '#D970D0'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          top: 8,
          right: 8,
          data: ['销售经理', '销售主管']
        },
        toolbox: {
          show: false
        },
        calculable: true,
        xAxis: [
          {
            name: '时间',
            nameTextStyle: {
              fontSize: 12
            },
            type: 'category',
            axisTick: {
              show: false
            },
            data: this.chartData.time
          }],
        yAxis: [
          {
            type: 'value',
            name: '人'
          }
        ],
        series: [
          {
            name: '销售经理',
            type: this.type,
            barGap: 0,
            data: this.chartData.top
          },
          {
            name: '销售主管',
            type: this.type,
            data: this.chartData.tj
          }
        ],
        animation: true
      })
    }
  }
}
</script>
