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

<script>
import * as echarts from 'echarts';
import resize from './mixins/resize'
import {listAllPlan} from "@/api/patrol/patrol/plan";

require('echarts/theme/macarons') // echarts theme

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '570px'
    }
  },
  data() {
    return {
      chart: null,
      planList: [],
      // 查询参数
      queryParams: {
        planQuarter: new Date().getMonth() + 1, planYear: new Date().getFullYear()
      },
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getList()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getList(){
      listAllPlan(this.queryParams).then(response => {
        response.data.forEach(item=>{
          this.planList.push({name:item.planName,value:item.uncheckedNum})
        })
        this.initChart();
      })},
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          type: 'scroll',
          orient: 'horizontalAndVertical',
          top: '70%',
          data: this.planList,
        },
        series: [
          {
            name: '计划中未检测的设备数量',
            type: 'pie',
            radius: ['30%','50%'],
            center: ['50%', '30%'],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            data: this.planList,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
