<template>
  <div
    id="topBigZhu"
    class="echart"
    :style="{
      width: '100%',
      height: '100%',
      margin: '0px 0px 50px 0px'
    }"
  />
</template>

<script>
import * as echarts from 'echarts'
import {
  functionDeptProblemContrast3,
  functionDeptProblemContrast4
} from '@/api/allApi/index.js'

export default {
  name: 'PostCheckWebTopBigChart',
  props: {
    type: {
      type: Number,
      default: 3
    },
    queryParams: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      title: '',
      total: '',

      titleList: [],
      problemCategoryFiveList: [],
      problemCategoryFourList: [],
      problemCategoryOneList: [],
      problemCategorySixList: [],
      problemCategoryThreeList: [],
      problemCategoryTwoList: [],
      problemCompleteList: [],
      problemEffectiveList: [],
      problemSuitableList: [],
      problemTotalList: []
    }
  },

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

  methods: {
    async initList() {
      this.titleList = []
      this.problemCategoryFiveList = []
      this.problemCategoryFourList = []
      this.problemCategoryOneList = []
      this.problemCategorySixList = []
      this.problemCategoryThreeList = []
      this.problemCategoryTwoList = []
      this.problemCompleteList = []
      this.problemEffectiveList = []
      this.problemSuitableList = []
      this.problemTotalList = []
      if (this.type == 3) {
        const res = await functionDeptProblemContrast3(this.queryParams)
        if (res.code !== 200) return this.$message.error(res.msg)
        if (res.data.length == 0) {
          this.titleList = []
          this.problemCategoryFiveList = []
          this.problemCategoryFourList = []
          this.problemCategoryOneList = []
          this.problemCategorySixList = []
          this.problemCategoryThreeList = []
          this.problemCategoryTwoList = []
          this.problemCompleteList = []
          this.problemEffectiveList = []
          this.problemSuitableList = []
          this.problemTotalList = []
        } else {
          res.data.forEach((item) => {
            this.titleList.push(item.deptName)
            this.problemCategoryFiveList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCategoryFourList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCategoryOneList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCategorySixList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCategoryThreeList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCategoryTwoList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCompleteList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemEffectiveList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemSuitableList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemTotalList.push(item.deptProblemRectificationRate * 100)
          })
        }
      } else {
        const res = await functionDeptProblemContrast4(this.queryParams)
        if (res.code !== 200) return this.$message.error(res.msg)
        if (res.data.length == 0) {
          this.titleList = []
          this.problemCategoryFiveList = []
          this.problemCategoryFourList = []
          this.problemCategoryOneList = []
          this.problemCategorySixList = []
          this.problemCategoryThreeList = []
          this.problemCategoryTwoList = []
          this.problemCompleteList = []
          this.problemEffectiveList = []
          this.problemSuitableList = []
          this.problemTotalList = []
        } else {
          res.data.forEach((item) => {
            this.titleList.push(item.deptName)
            this.problemCategoryFiveList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCategoryFourList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCategoryOneList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCategorySixList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCategoryThreeList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCategoryTwoList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemCompleteList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemEffectiveList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemSuitableList.push(
              item.deptProblemRectificationRate * 100
            )
            this.problemTotalList.push(item.deptProblemRectificationRate * 100)
          })
        }
      }
      setTimeout(() => {
        this.title = this.titleList.shift()
        this.total = this.problemTotalList.shift() + "%"
        this.initTopBigChartEcharts()
      }, 100)
    },

    initTopBigChartEcharts() {
      const posList = [
        'left',
        'right',
        'top',
        'bottom',
        'inside',
        'insideTop',
        'insideLeft',
        'insideRight',
        'insideBottom',
        'insideTopLeft',
        'insideTopRight',
        'insideBottomLeft',
        'insideBottomRight'
      ]
      app.configParameters = {
        rotate: {
          min: -90,
          max: 90
        },
        align: {
          options: {
            left: 'left',
            center: 'center',
            right: 'right'
          }
        },
        verticalAlign: {
          options: {
            top: 'top',
            middle: 'middle',
            bottom: 'bottom'
          }
        },
        position: {
          options: posList.reduce(function (map, pos) {
            map[pos] = pos
            return map
          }, {})
        },
        distance: {
          min: 0,
          max: 100
        }
      }
      app.config = {
        rotate: 90,
        align: 'left',
        verticalAlign: 'middle',
        position: 'insideBottom',
        distance: 15,
        onChange: function () {
          const labelOption = {
            rotate: app.config.rotate,
            align: app.config.align,
            verticalAlign: app.config.verticalAlign,
            position: app.config.position,
            distance: app.config.distance
          }
          myChart.setOption({
            series: [
              {
                label: labelOption
              },
              {
                label: labelOption
              },
              {
                label: labelOption
              },
              {
                label: labelOption
              }
            ]
          })
        }
      }
      const labelOption = {
        show: true,
        position: app.config.position,
        distance: app.config.distance,
        align: app.config.align,
        verticalAlign: app.config.verticalAlign,
        rotate: app.config.rotate,
        formatter: '{c}  {name|{a}}',
        fontSize: 16,
        rich: {
          name: {}
        }
      }
      const option = {
        //格式化提示
        tooltip: {
          trigger: 'item',
          formatter: '{c}%'
        },
        title: {
          text: ' ',
          subtext: this.title + `：` + this.total,
          left: 'center'
        },
        legend: {
          data: [
            // '问题总数',
            '总整改率',
            '完整性',
            '适宜性',
            '有效性职责权限',
            '工作内容',
            '工作标准',
            '考核奖励',
            '任职资格',
            '其他'
          ]
        },
        xAxis: [
          {
            type: 'category',
            axisTick: { show: false },
            data: this.titleList,
            axisLabel: { interval: 0, rotate: 30 }
          }
        ],
        yAxis: {
          axisLabel: {
            formatter: '{value}%'
          },
          minInterval: 1
        },
        series: [
          {
            // name: '问题总数',
            name: '总整改率',
            type: 'bar',
            barGap: 0,
            barWidth: '30%',
            emphasis: {
              focus: 'series'
            },
            label: {
              show: true,
              position: 'top', // 顶部显示
              formatter: '{c}%' // 显示数据值
            },
            data: this.problemTotalList
          }
        ]
      }
      const topBigZhu = echarts.init(document.getElementById('topBigZhu')) // 图标初始化
      topBigZhu.setOption(option) // 渲染页面
      // 随着屏幕大小调节图表
      window.addEventListener('resize', () => {
        topBigZhu.resize()
      })
    }
  }
}
</script>

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