<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from '../mixins/resize'
import {getWorkcetificate} from "@/api/common/homedataCbpb";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      nextEdit: [],
      nextReview: [],
      monthList: [],
    }
  },
  mounted() {
    this.$nextTick(() => {
      getWorkcetificate().then(res => {
          let CERALL = res.data.cerAll;
          this.nextEdit= [CERALL.CONTAINER ,CERALL.PIPE ,CERALL.WORKER ,CERALL.FOREMAN ,CERALL.AC ,CERALL.FIREFIGHTER ,CERALL.SAFETY ,CERALL.TDS]
        let CERWARN = res.data.cerWarn;
          this.nextReview= [CERWARN.CONTAINER ,CERWARN.PIPE ,CERWARN.WORKER ,CERWARN.FOREMAN ,CERWARN.AC ,CERWARN.FIREFIGHTER ,CERWARN.SAFETY ,CERWARN.TDS]
        this.initChart()
      })
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(
        {
          xAxis: {
            data: [
              '压力\n容器',
              '压力\n管道',
              '技术\n工人\n上岗证',
              '班组长\n安全\n培训',
              '制冷与\n空调',
              '消防员\n证',
              '安全\n管理',
              'TDS'
            ],
            axisTick: {
              show: true
            }
          },
          grid: {
            left: 10,
            right: 30,
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow' // 修改为柱状图的显示方式
            },
          },
          yAxis: {
            axisTick: {
              show: false
            },
          },
          legend: {
            data: ['总数', '预警数']
          },
          series: [
            {
              name: '总数',
              type: 'bar', // 将类型修改为柱状图
              barWidth: '20%', // 设置柱状图的宽度
              itemStyle: {
                normal: {
                  color: '#60c6fc' // 设置第二个序列为红色
                }
              },
              data: this.nextEdit // 确保数据适用于柱状图
            },
            {
              name: '预警数',
              type: 'bar', // 将类型修改为柱状图
              barWidth: '20%', // 设置柱状图的宽度
              itemStyle: {
                normal: {
                  color: '#fd806e' // 设置第二个序列为红色
                }
              },
              data: this.nextReview // 确保数据适用于柱状图
            }
          ]
        }


      )
    }
  }
}
</script>
