<template>
  <div class="energy">
    <!-- <div class="text_Cur">
      <div v-for="item in curList" :key="item.key">
        <div>{{ item.name }}</div>
        <div>
          <span class="mark">{{ item.value ? item.value : 0 }}</span>
          <span class="d">{{ item.d }}</span>
        </div>
      </div>
    </div> -->
    <div id="energy-chart"></div>
  </div>
</template>

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

export default {
  name: '',
  props: {
    total: {
      type: Object,
      default: () => {}
    },
    deviceInfo: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      uid:'1',
      curList: [
        {
          name: '充电电量',
          key: 'storage_elect_in',
          lineColor: '#22E4FF',
          value: 0,
          d: 'kWh'
        },
        {
          name: '放电电量',
          key: 'storage_elect_out',
          lineColor: '#0E68E4',
          value: 0,
          d: 'kWh'
        }
      ],
      energyChart: null,
      energyChartData: {
        ydata: [],
        xdata: []
      }
    }
  },
  watch: {
    total: {
      handler(newVal,oldVal) {
        if (newVal!==oldVal) {
          let that = this
          that.curList.forEach((item) => {
            item.value = that.total[item.key]
          })
        }
      },
    
    },
    deviceInfo: {
      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.energyChart){
      this.energyChart.dispose()
      this.energyChart = null
    }

  },

  methods: {
    handleResize() {
      if(this.energyChart){
        this.energyChart.resize()
      }
    },
 
    getChargeData() {
      const arr = this.curList
      const keyList = this.curList.map((item) => item.key)
      const result = processData(this.deviceInfo, keyList)

      this.energyChartData.xdata = result.dates
      arr.forEach((item, index) => {
        this.energyChartData.ydata[index] = {
          name: item.name,
          // type: 'line',
          // smooth: true,
          // barWidth: 5,
          // itemStyle: {
          //   borderRadius: [5, 5, 0, 0],
          //   color: item.lineColor
          // },
          // emphasis: {
          //   focus: 'series'
          // },
          // global: false,
          // showSymbol: false,
          // data: result.values[index]
          type: 'bar',
          data: [5,3,6,3,8,4,6],
          color: item.lineColor
        }
      })
    },

    drawLineChart(activeKey) {
      this.getChargeData(activeKey)
      if(this.energyChart){
        this.energyChart.dispose()
      }
      const chartDom = document.getElementById('energy-chart')
      if (!chartDom) return;
      let energyChart = this.$echarts.init(chartDom)
      this.energyChart = energyChart
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          top: 20,
          textStyle: {
            color: '#fff'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          top: '32%',
          // containLabel: true
        },
        xAxis: {
          type: 'category',
          data: this.energyChartData.xdata,
          axisLine: {
            lineStyle: { type: 'dashed', color: '#435463' }
          },
          axisLabel: {
            color: '#fff'
          }
        },
        yAxis: {
          name: '电量(kWh)',
          nameTextStyle: { color: '#fff' },
          type: 'value',
          splitNumber: 2,
          splitLine: {
            lineStyle: { type: 'dashed', color: '#435463' }
          },
          axisLabel: {
            // interval: 4,
            color: '#fff',
            fontSize:12

          }
        },
        series: this.energyChartData.ydata
      }
      option && energyChart.setOption(option)
      this.setupResizeListener()
    },
    setupResizeListener(){
      window.removeEventListener('resize', this.handleResize);
      window.addEventListener('resize', this.handleResize)
    }
  }
}
</script>

<style lang="scss" scoped>
.energy {
  height: calc(100% - 35px);
  background-color: #50505030;
  #energy-chart {
    height: calc(100% - 5px);
  }
}

</style>
