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

<script>
import * as echarts from 'echarts';
import resize from '@/views/dashboard/mixins/resize'
import {listPlan} from "@/api/ps/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: {
        planMonth: 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() {
      listPlan(this.queryParams).then(response => {
        response.rows.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%'],
            itemStyle:{
              borderRadius: 15,
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            data: this.planList,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
