<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 { parseTime } from '@/utils/index.js'
// import { emit } from 'cluster'
// 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
    },
    barCharDeptData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      sourceDate: []
    }
  },
  watch: {
    barCharDeptData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
        console.log(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.barCharDeptData)
      // console.log('this.barCharDeptData', this.barCharDeptData)
    },
    setOptions({ xAxisData, seriesData, text } = {}) {
      this.chart.setOption({
        title: {
          text: text + '维修成本柱状图',
          subtext: ''
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '15%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            name: '水表名称',
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            // data: xAxisData,
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: { interval: 0, rotate: 40 }
          }
        ],
        yAxis: [
          {
            name: '吨',
            type: 'value'
          }
        ],
        series: [
          {
            type: 'bar',
            barWidth: '60%',
            data: [10, 52, 200, 334, 390, 330, 220]
            // data: seriesData
          }
        ]
      })
      const that = this
      // 点击柱状图拿到对应内容
      this.chart.on('click', function(params) {
        console.log(params)
        // console.log(parseTime(new Date(), '{y}') + '-' + params.name.substring(0, 2))
        // that.$emit('barClickDept', parseTime(new Date(), '{y}') + '-' + params.name.substring(0, 2))
        that.$emit('barClickDept', params.name)
      })
    }
  }
}
</script>
