<template>
  <!--  今日工单类型分析  -->
  <el-card :body-style="{ padding: '10px' }" :style="{height:height}" shadow="hover" class="circular_chart">
    <div class="sc_title">
      <h4 class="title">{{ option.series[0].name }}</h4>
      <el-radio-group v-model="search_date" size="mini" class="sc_btns" @change="changeDate">
        <el-radio-button label="1">日</el-radio-button>
        <el-radio-button label="3">月</el-radio-button>
        <el-radio-button label="4">年</el-radio-button>
      </el-radio-group>
    </div>
    <div v-loading="loading" ref="circularChart" class="chart"/>
  </el-card>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { getOrderType } from '@/api/dashboard/orderDatas'

export default {
  name: 'CircularChart',
  props: {
    height: {
      type: String,
      default: '320px'
    }
  },
  data() {
    return {
      loading: false,
      chart: null,
      search_date: '1',
      option: {
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '0',
          data: ['咨询', '投诉']
        },
        calculable: true,
        series: [{
          name: '今日工单类型分析',
          type: 'pie',
          radius: ['50%', '65%'],
          center: ['50%', '45%'],
          data: [{
            value: 0,
            name: '咨询'
          },
          {
            value: 0,
            name: '投诉'
          }
          ],
          animationEasing: 'cubicInOut',
          animationDuration: 2600
        }]
      }
    }
  },
  created() {
    this.getDatas(Number(this.search_date))
  },
  mounted() {
    this.initChart()
    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: {
    // 获取数据
    getDatas(datetype) {
      this.loading = true
      return new Promise(resolve => {
        getOrderType(datetype).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            const res = response.data
            if (res && res.length > 0) {
              const sData = []
              res.forEach((v, i) => {
                sData.push({
                  value: v.count,
                  name: v.name
                })
              })
              let changeName
              switch (datetype) {
                case 1:
                  changeName = '今日工单类型分析'
                  break
                case 3:
                  changeName = '本月工单类型分析'
                  break
                case 4:
                  changeName = '本年工单类型分析'
                  break
              }
              this.option.series[0].name = changeName
              this.option.series[0].data = sData
              this.initChart()
            }
          }
        })
        resolve()
      })
    },
    initChart() {
      this.chart = echarts.init(this.$refs.circularChart, 'macarons')
      this.chart.clear()
      this.chart.setOption(this.option)
    },
    changeDate(v) {
      this.getDatas(Number(v))
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	.circular_chart {
		width: 100%;
		.sc_title{
			position: relative;
			.title {
				color: #333333;
				font-size: 14px;
				margin: 7px 0;
				padding: 0;
				font-weight: 500;
			}
			.sc_btns{
				position: absolute;
				right: 0;
				top: -7px;
			}
		}
		.chart{
			margin-top: 10px;
			width: 100%;
			height: 240px;
		}
	}
</style>
