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

<script>
import * as echarts from 'echarts'

import {
  lastThreeYearsRatelViewSC,
  lastThreeYearsRatelViewZN
} from '@/api/allApi/index.js'
export default {
  name: 'PostCheckWebLeftZhu',
  props: {
    type: {
      type: Number,
      default: 3
    },
    queryParams: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      titleList: [],
      totalList: []
    }
  },

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

  methods: {
    async initList() {
      if (this.type == 3) {
        const res = await lastThreeYearsRatelViewSC(this.queryParams)
        if (res.code !== 200) return this.$message.error(res.msg)
        this.titleList = res.data.map((item) => item.yearView)
        this.totalList = res.data.map((item) => item.problemRate * 100)
      } else {
        const res = await lastThreeYearsRatelViewZN(this.queryParams)
        if (res.code !== 200) return this.$message.error(res.msg)
        this.titleList = res.data.map((item) => item.yearView)
        this.totalList = res.data.map((item) => item.problemRate * 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: this.titleList
        },
        yAxis: {
          axisLabel: {
            formatter: '{value}%'
          },
          minInterval: 1
        },
        series: [
          {
            data: this.totalList,
            type: 'bar',
            barWidth: '25%',
            //格式话label显示
            label: {
              show: true,
              position: 'top', // 顶部显示
              formatter: '{c}%'
            }
          }
        ]
      }
      const rightZhuChart = echarts.init(
        document.getElementById('rightZhuChart')
      ) // 图标初始化
      rightZhuChart.setOption(option) // 渲染页面
      // 随着屏幕大小调节图表
      window.addEventListener('resize', () => {
        rightZhuChart.resize()
      })
    }
  }
}
</script>