<template>
  <div class="chart-container">
    <el-radio-group v-model="radio" class="radio-group" @change="initChart">
      <el-radio v-for="radio in radioList" :key="radio.value" :label="radio.value">{{ radio.text }}</el-radio>
    </el-radio-group>
    <div ref="chart" class="chart-body" />
  </div>
</template>

<script>
import _ from 'lodash'
import echarts from 'echarts'
export default {
  name: 'DeptAccessChart',
  props: {
    list: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      radio: 'sumary',
      radioList: [
        {
          text: '应处置数',
          value: 'totalShouldProcess'
        },
        {
          text: '处置数',
          value: 'process'
        },
        {
          text: '返工数',
          value: 'returnJob'
        },
        {
          text: '综合指标',
          value: 'sumary'
        }
      ],
      chart: null,
      x: [],
      y: []
    }
  },
  activated() {
    this.initChart()
  },
  methods: {
    initChart() {
      console.log('init chart')
      // 取x,y
      this.x = []
      this.y = []
      this.list.forEach(item => {
        this.x.push(item.deptName)
        switch (this.radio) {
          case 'totalShouldProcess': {
            this.y.push(item.totalShouldProcess)
            break
          }
          case 'process': {
            this.y.push(item.process)
            break
          }
          case 'returnJob': {
            this.y.push(item.returnJob)
            break
          }
          case 'sumary': {
            this.y.push(item.sumary)
            break
          }
          default: break
        }
      })
      // tooltip
      const tooltip = _.find(this.radioList, ['value', this.radio]).text

      this.chart = echarts.init(this.$refs.chart)
      this.chart.setOption({
        tooltip: {
          show: true,
          trigger: 'item',
          formatter: (params) => tooltip + '<br/><span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:' +
                                  params.color + '"></span>' + params.name + ':' + params.data
        },
        color: '#2a7bc4',
        xAxis: {
          data: this.x,
          axisLabel: {
            fontSize: 14
          }
        },
        yAxis: {
          axisLabel: {
            fontSize: 14
          },
          minInterval: 1
        },
        series: [{
          name: '职能单位评价',
          type: 'bar',
          data: this.y,
          barWidth: '40%',
          barMaxWidth: '100',
          tooltip: {
            position: 'right'
          }
        }]
      })
    }
  }
}
</script>

<style scoped>
.chart-container{
  text-align: center;
}
.radio-group{
  margin-top: 20px;
}
.chart-body{
  height: 50vh;
  width: 80%;
  margin-right: auto;
  margin-left: auto;
}
</style>
