<template>
  <div
    id="topCenterChart"
    class="echart"
    :style="{
      width: '100%',
      height: '100%',
      padding: '10px'
    }"
  />
</template>

<script>
import {
  dutyTypeProblemStatisticsView3,
  dutyTypeProblemStatisticsView4
} from '@/api/allApi/index.js'
import * as echarts from 'echarts'
export default {
  name: 'PostCheckWebTopCenterChart',
  props: {
    type: {
      type: Number,
      default: 1
    },
    queryParams: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      dataList: []
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.initList()
    })
  },

  methods: {
    async initList() {
      this.dataList = []
      if (this.type == 3) {
        const res = await dutyTypeProblemStatisticsView3(this.queryParams)
        if (res.code !== 200) return this.$message.error(res.msg)
        this.dataList = [
          res.data.dutyTypeOneRate * 100,
          res.data.dutyTypeTwoRate * 100,
          res.data.dutyTypeThreeRate * 100,
          res.data.dutyTypeFourRate * 100,
          res.data.dutyTypeFiveRate * 100,
          res.data.dutyTypeSixRate * 100
        ]
      } else {
        const res = await dutyTypeProblemStatisticsView4(this.queryParams)
        if (res.code !== 200) return this.$message.error(res.msg)
        this.dataList = [
          res.data.dutyTypeOneRate * 100,
          res.data.dutyTypeTwoRate * 100,
          res.data.dutyTypeThreeRate * 100,
          res.data.dutyTypeFourRate * 100,
          res.data.dutyTypeFiveRate * 100,
          res.data.dutyTypeSixRate * 100
        ]
      }
      setTimeout(() => {
        this.initTopLeftChartEcharts()
      }, 100)
    },
    initTopLeftChartEcharts() {
      const option = {
        title: {
          text: '岗检问题职责类别整改占比',
          left: 'center'
        },
        //格式化提示
        tooltip: {
          trigger: 'item',
          formatter: '{c}%'
        },
        legend: {
          top: '8%',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: [
            '业务职责',
            '党建及党风廉政',
            '安全环保职责',
            '合规保密职',
            '任职资格与能力素质要求'
          ],
          axisLabel: {
            interval: 0,
            rotate: 20 //倾斜的程度
          }
        },
        yAxis: {
          axisLabel: {
            formatter: '{value}%'
          },
          minInterval: 1
        },
        series: [
          {
            data: this.dataList,
            type: 'bar',
            barWidth: '25%',
            //格式话label显示
            label: {
              show: true,
              position: 'top', // 顶部显示
              formatter: '{c}%'
            }
          }
        ]
      }
      const topCenterChart = echarts.init(
        document.getElementById('topCenterChart')
      ) // 图标初始化
      topCenterChart.setOption(option) // 渲染页面
      // 随着屏幕大小调节图表
      window.addEventListener('resize', () => {
        topCenterChart.resize()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>