<template>
  <div class="pv">
    <!-- <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="pv-chart"></div>
  </div>
</template>

<script>
import {processData} from '@/utils/dealWithData'
export default {
  name: '',
  props: {
    total: {
      type: Object,
      default: () => {}
    },
    deviceInfo: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      curList: [
        {
          name: '发电量',
          key: 'solar_elect_gen',
          lineColor: '#22E4FF',
          value: 0,
          d: 'kWh'
        },
        // {
        //   name: '入网电量',
        //   key: 'solar_elect_grid',
        //   lineColor: '#0E68E4',
        //   value: 0,
        //   d: 'kWh'
        // }
      ],
      pvChart: null,
      pvChartData: {
        ydata: [],
        xdata: []
      },
      count: {
        charge: {
          cur: [1, 2]
        }
      }
    }
  },
  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.pvChart) {
      this.pvChart.dispose()
      this.pvChart = null
    }
  },

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

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

    drawLineChart(activeKey) {
      this.getChargeData(activeKey)
      if (this.pvChart) {
        this.pvChart.dispose()
      }
      const chartDom = document.getElementById('pv-chart')
      if (!chartDom) return
      let pvChart = this.$echarts.init(chartDom)
      this.pvChart = pvChart
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          top: 10,
          textStyle: {
            color: '#fff'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '5%',
          top: '32%',
          // containLabel: true
        },
        xAxis: {
          type: 'category',
          data: this.pvChartData.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.pvChartData.ydata
      }
      option && pvChart.setOption(option)
      this.setupResizeListener()
    },
    setupResizeListener() {
      window.removeEventListener('resize', this.handleResize)
      window.addEventListener('resize', this.handleResize)
    }
  }
}
</script>

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

</style>
