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

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

export default {
  mixins: [resize],
  props: {
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '500px',
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
      default: () => {},
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      },
    },
  },
  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(this.chartData)
    },
    // 绘制图表
    setOptions({ studyTaskCountByMonth, examTaskCountByMonth, trainingTaskCountByMonth } = null) {
      this.chart.setOption({
        xAxis: {
          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
          boundaryGap: false,
          axisTick: {
            show: false,
          },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 40,
          top: 30,
          containLabel: true,
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
        },
        legend: {
          data: ['学习任务', '考试任务', '培训任务'],
        },
        series: [
          {
            name: '学习任务',
            itemStyle: {
              normal: {
                color: '#1890ff',
                lineStyle: {
                  color: '#1890ff',
                  width: 2,
                },
              },
            },
            smooth: false,
            type: 'line',
            data: this.setChartsNum(studyTaskCountByMonth),
            animationDuration: 2800,
            animationEasing: 'cubicInOut',
          },
          {
            name: '考试任务',
            smooth: false,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#2fc25b',
                lineStyle: {
                  color: '#2fc25b',
                  width: 2,
                },
              },
            },
            data: this.setChartsNum(examTaskCountByMonth),
            animationDuration: 2800,
            animationEasing: 'quadraticOut',
          },
          {
            name: '培训任务',
            smooth: false,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#facc14',
                lineStyle: {
                  color: '#facc14',
                  width: 2,
                },
              },
            },
            data: this.setChartsNum(trainingTaskCountByMonth),
            animationDuration: 2800,
            animationEasing: 'quadraticOut',
          },
        ],
      })
    },
    // 处理后台返回的数据
    setChartsNum(data) {
      const numList = data ? data.map(e => e.recordCount) : []
      return numList
    },
  },
}
</script>
