<template>
  <div class="chart" ref="chart"></div>
</template>

<script>
import * as echarts from 'echarts'
import { fontSize } from '@/views/groupPage/util'
export default {
  name: 'supAndConsNumChart', //监理和施工扫码次数图表
  props: {
    chartData: {},
    type: { type: String, required: true }
  },
  data() {
    return { myChart: null }
  },
  watch: {
    chartData(val) {
      this.$nextTick(() => {
        this.initialChart()
      })
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initialChart()
    })
    //图表大小自适应
    window.addEventListener('resize', this.onResize)
  },
  methods: {
    destroyChart() {
      if (this.myChart != null) {
        this.myChart.dispose()
        this.myChart = null
      }
    },
    onResize() {
      if (this.myChart) {
        this.myChart.resize()
      }
    },
    initialChart() {
      this.destroyChart()
      let legend = []
      let xAxis = []
      let yAxisName = ''
      let series = []
      if (this.type == 'progress') {
        xAxis = this.chartData.xAxis
        yAxisName = '单位：次'
        legend = ['监理', '施工']
        series = [
          {
            name: '监理',
            type: 'bar',
            barWidth: 8,
            itemStyle: {
              color: '#FFD553'
            },
            data: this.chartData.spvData
          },
          {
            name: '施工',
            type: 'bar',
            barWidth: 8,
            itemStyle: {
              color: '#0EA7FF'
            },
            data: this.chartData.conData
          }
        ]
      }
      if (this.type == 'quality') {
        xAxis = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        yAxisName = '单位：个'
        legend = ['完成检验', '新增申请']
        series = [
          {
            name: '完成检验',
            type: 'bar',
            barWidth: 8,
            itemStyle: {
              color: '#00C272'
            },
            data: this.chartData.complete
          },
          {
            name: '新增申请',
            type: 'bar',
            barWidth: 8,
            itemStyle: {
              color: '#139FF0'
            },
            data: this.chartData.application
          }
        ]
      }
      let option = {
        grid: {
          left: '5%',
          right: '5%',
          top: '20%',
          bottom: '5%',
          containLabel: true
        },
        tooltip: {
          show: true,
          confine: true,
          borderWidth: 0,
          textStyle: {
            color: '#fff'
          },
          backgroundColor: 'rgba(50,50,50,0.7)',
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              type: 'solid',
              color: 'rgba(255, 255, 255, 0.3)'
            }
          }
        },
        legend: {
          show: true,
          x: 'right',
          y: '10',
          icon: 'rect',
          itemWidth: 10,
          itemHeight: 10,
          textStyle: {
            color: '#FFFFFF'
          },
          data: legend
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: true,
            axisLabel: {
              color: '#FFFFFF'
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            },
            data: xAxis
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: yAxisName,
            nameTextStyle: {
              color: '#FFFFFF'
            },
            axisLabel: {
              color: '#FFFFFF'
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: 'rgba(255, 255, 255, 0.2)'
              }
            }
          }
        ],
        series
      }
      this.creatChart(option, this.$refs.chart)
    },
    creatChart(option, ref) {
      this.myChart = echarts.init(ref) //this.$refs.chart
      this.myChart.resize()
      this.myChart.setOption(option, {
        notMerge: true
      })
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.onResize)
    this.destroyChart()
  }
}
</script>

<style lang='scss' scoped>
.chart {
  height: 100%;
  width: 100%;
}
</style>