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

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { getOnOffCount } from '@/api/sDevice'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    }
  },
  data() {
    return {
      relayon: 0,
      relayoff: 0,
      devon: 0,
      devoff: 0,
      chart: null,
      scale: 1,
      total_datas: 0
    }
  },
  mounted() {
    this.getOnOffCount()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {

    getOnOffCount() {
      getOnOffCount().then(res => {
        if (res.relayon != null) {
          this.relayon = res.relayon
          this.total_datas += res.relayon
        }
        if (res.relayoff != null) {
          this.relayoff = res.relayoff
          this.total_datas += res.relayoff
        }
        if (res.devon != null) {
          this.devon = res.devon
          this.total_datas += res.devon
        }
        if (res.devoff != null) {
          this.devoff = res.devoff
          this.total_datas += res.devoff
        }
        // console.log(this.relayon, this.relayoff, this.devon, this.devoff)
        this.initChart()
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title: {
          text: '设备总数',
          subtext: this.total_datas, // 设备总数
          textStyle: {
            color: '#00fcfd',
            fontSize: 20,
            align: 'center'
          },
          subtextStyle: {
            fontSize: 15,
            color: ['#ff9d19']
          },
          x: 'center',
          y: '55%'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          x: 'center',
          top: '10%',
          itemWidth: 12, // 设置图例图形的宽
          itemHeight: 9, // 设置图例图形的高
          textStyle: {
            color: '#fff' // 图例文字颜色
          },
          // itemGap设置各个item之间的间隔，单位px，默认为10，横向布局时为水平间隔，纵向布局时为纵向间隔
          itemGap: 10

        },
        calculable: true,
        series: [
          {
            name: '设备状态预览',
            type: 'pie',
            itemStyle: {
              normal: {
                borderWidth: 5, // 饼图之间的间距
                borderColor: 'rgb(26,42,55)'
              }
            },
            radius: ['38%', '50%'],
            center: ['50%', '60%'],
            hoverAnimation: false,
            data: [
              { value: this.relayon, name: '控制器在线', itemStyle: { color: '#E58B44' }},
              { value: this.relayoff, name: '控制器离线', itemStyle: { color: '#D95CFF' }},
              { value: this.devon, name: '设备在线', itemStyle: { color: '#6f81da' }},
              { value: this.devoff, name: '设备离线', itemStyle: { color: '#00ffb4' }}
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 1000
          }
        ]
      })
    }
  }
}
</script>
