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

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

export default {
  name: '',
  props: {
    propsInfo: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      curList: [
        {
          name: '充电电量',
          key: 'storage_elect_in',
          lineColor: '#9BD801',
          value: 0,
          d: 'kWh'
        },
        {
          name: '放电电量',
          key: 'storage_elect_out',
          lineColor: '#3DFEFA',
          value: 0,
          d: 'kWh'
        }
      ],

      disChargeChart: null,
      disChargeChartData: {
        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.disChargeChart) {
      this.disChargeChart.dispose()
      this.disChargeChart = null
    }
  },

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

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

      this.disChargeChartData.xdata = result.dates
      arr.forEach((item, index) => {
        this.disChargeChartData.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.getDisChargeData(activeKey)
      if (this.disChargeChart) {
        this.disChargeChart.dispose()
      }
      const chartDom = document.getElementById('disCharge-chart')
      if (!chartDom) return
      let disChargeChart = this.$echarts.init(chartDom)
      this.disChargeChart = disChargeChart
      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.disChargeChartData.xdata,
          axisLine: {
            lineStyle: { type: 'dashed', color: '#435463' }
          },
          axisLabel: {
            color: '#fff'
          }
        },
        yAxis: {
          type: 'value',
          splitLine: {
            lineStyle: { type: 'dashed', color: '#435463' }
          },
          axisLabel: {
            color: '#fff'
          }
        },
        series: this.disChargeChartData.ydata
      }
      option && disChargeChart.setOption(option)
      this.setupResizeListener()
    },
    setupResizeListener() {
      window.removeEventListener('resize', this.handleResize)
      window.addEventListener('resize', this.handleResize)
    }
  }
}
</script>

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

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

</style>
