<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'
import { debounce } from '@/utils'

// eslint-disable-next-line no-unused-vars
const animationDuration = 6000
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
    },
    contrastData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    contrastData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
        console.log('watch拿到的val', 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.contrastData)
      // console.log('methods拿到的pieDeptData', this.pieDeptData)
    },
    setOptions({ lastData, thisData, text, thisMinData, days } = {}) {
      this.chart.setOption({
        title: {
          text: text + '月维修同期对比',
          subtext: ''
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['去年', '今年']
        },
        toolbox: {
          show: true,
          feature: {
            dataZoom: {
              yAxisIndex: 'none'
            },
            dataView: { readOnly: false },
            magicType: { type: ['line', 'bar'] },
            restore: {},
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          name: '日期',
          boundaryGap: false,
          data: days
        },
        yAxis: {
          type: 'value',
          name: '元',
          axisLabel: {
            formatter: '{value}'
          }
        },
        grid: [
          { top: '25%', left: '15%' }
        ],
        series: [
          {
            name: '去年',
            type: 'line',
            // data: [11, 11, 15, 13, 12, 13, 10, 9, 11, 3],
            data: lastData,
            markPoint: {
              data: [
                // { type: 'max', name: '最大值' },
                // { type: 'min', name: '最小值' }
              ]
            },
            markLine: {
              data: [
                { type: 'average', name: '平均值' }
              ]
            }
          },
          {
            name: '今年',
            type: 'line',
            // data: [1, 5, 2, 5, 3, 2, 0, 9, 6, 7],
            data: thisData,
            markPoint: {
              data: [
                // { name: '周最低', value: 5, xAxis: 1, yAxis: 2 }
                // { type: 'max', name: '最大值' },
                // { type: 'min', name: '最小值' }
              ]
            },
            markLine: {
              data: [
                { type: 'average', name: '平均值' },
                [{
                  symbol: 'none',
                  x: '90%',
                  yAxis: 'max'
                }, {
                  symbol: 'circle',
                  label: {
                    normal: {
                      position: 'start',
                      formatter: '最大值'
                    }
                  },
                  type: 'max',
                  name: '最高点'
                }]
              ]
            }
          }
        ]
      })
    }
  }
}
</script>
