<template>
  <div class="utilization">
    <div id="utilization-chart"></div>
  </div>
</template>

<script>
import {processData} from '@/utils/dealWithData'

export default {
  name: '',
  props: {
    propsInfo: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      curList: [
        {
          name: '日设备利用率',
          key: 'usage_rate',
          lineColor: '#F69B52',
          value: 0,
          d: 'kWh'
        }
      ],

      utilizationChart: null,
      utilizationChartData: {
        ydata: [],
        xdata: []
      }
    }
  },
  watch: {
    propsInfo: {
      handler(newVal, oldVal) {
        if (JSON.stringify(newVal) !== JSON.stringify(oldVal)) {
          this.$nextTick(() => {
            this.drawLineChart()
          })
        }
      },
      deep: true // 确保深度比较
    }
  },
  mounted() {},
  beforeUnmount() {
    window.removeEventListener('resize', this.handleResize)
    if (this.utilizationChart) {
      this.utilizationChart.dispose()
      this.utilizationChart = null
    }
  },

  methods: {
    handleResize() {
      if (this.utilizationChart) {
        this.utilizationChart.resize()
      }
    },

    getUtilizationData() {
      const arr = this.curList
      const keyList = this.curList.map((item) => item.key)
      const result = processData(this.propsInfo, keyList)

      this.utilizationChartData.xdata = result.dates
      arr.forEach((item, index) => {
        this.utilizationChartData.ydata[index] = {
          name: item.name,
          smooth: false,
          type: 'line',
          barWidth: 10,
          itemStyle: {
            borderRadius: 10,
            color: item.lineColor
          },
          emphasis: {
            focus: 'series'
          },

          global: false,
          showSymbol: false,
          data: result.values[index]
        }
      })
    },

    drawLineChart(activeKey) {
      this.getUtilizationData(activeKey)
      if (this.utilizationChart) {
        this.utilizationChart.dispose()
      }
      const chartDom = document.getElementById('utilization-chart')
      if (!chartDom) return
      let utilizationChart = this.$echarts.init(chartDom)
      this.utilizationChart = utilizationChart
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          top: 20,
          textStyle: {
            color: '#fff'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '5%',
          // containLabel: true
        },
        xAxis: {
          type: 'category',
          data: this.utilizationChartData.xdata,
          axisLine: {
            lineStyle: { type: 'dashed', color: '#435463' }
          },
          axisLabel: {
            color: '#fff'
          }
        },
        yAxis: {
          type: 'value',
          splitLine: {
            lineStyle: { type: 'dashed', color: '#435463' }
          },
          axisLabel: {
            color: '#fff'
          }
        },
        series: this.utilizationChartData.ydata
      }
      option && utilizationChart.setOption(option)
      this.setupResizeListener()
    },
    setupResizeListener() {
      window.removeEventListener('resize', this.handleResize)
      window.addEventListener('resize', this.handleResize)
    }
  }
}
</script>

<style lang="scss" scoped>
.utilization {
  height: calc(100% - 45px);

  #utilization-chart {
    height: 100%;
  }
}

</style>
