<template>
  <div class="event-trend-chart" :style="{height:height,width:width}" />
</template>

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

export default {
  mixins: [resize],
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler() {
        this.setOptions()
      }
    }
  },
  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.setOptions()
    },
    setOptions() {
      const { dates = [], level1 = [], level2 = [], level3 = [], level4 = [] } = this.chartData
      
      this.chart.setOption({
        animation: true,
        animationDuration: 800,
        animationEasing: 'cubicOut',
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        legend: {
          data: ['I级', 'II级', 'III级', 'IV级'],
          bottom: 10
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '15%',
          top: '10%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: dates,
          axisTick: {
            show: false
          }
        },
        yAxis: {
          type: 'value',
          axisTick: {
            show: false
          }
        },
        series: [
          {
            name: 'I级',
            type: 'line',
            smooth: true,
            data: level1,
            itemStyle: { color: '#b45309' },
            lineStyle: { color: '#b45309', width: 2 }
          },
          {
            name: 'II级',
            type: 'line',
            smooth: true,
            data: level2,
            itemStyle: { color: '#d97706' },
            lineStyle: { color: '#d97706', width: 2 }
          },
          {
            name: 'III级',
            type: 'line',
            smooth: true,
            data: level3,
            itemStyle: { color: '#3b82f6' },
            lineStyle: { color: '#3b82f6', width: 2 }
          },
          {
            name: 'IV级',
            type: 'line',
            smooth: true,
            data: level4,
            itemStyle: { color: '#6366f1' },
            lineStyle: { color: '#6366f1', width: 2 }
          }
        ]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.event-trend-chart {
  width: 100%;
}
</style>