
<template>
  <el-card shadow="always" class="common-card-container common-card-container-body-no-padding" style="margin-top:25px;">
    <div slot="header">
      <span class="card-title">策略模板曲线展示</span>
    </div>
    <div style="height: 360px" id="tempChart"/>
  </el-card>
</template>

<style scoped lang="scss"></style>
<script>
import * as echarts from 'echarts'
import resize from '@/mixins/ems/resize'
import {curveList} from '@/api/ems/dzjk'
export default {
  inject:['$home'],
  mixins: [resize],
  data() {
    return {
      chart: null
    }
  },
  beforeDestroy() {
    this.destoryChart()
  },
  methods: {
    destoryChart(){
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    changeSiteId(){
      this.destoryChart()
    },
    init(){
      if(!this.chart){
        this.chart = echarts.init(document.getElementById('tempChart'))
      }
      const strategyId = this.$home.updateStrategyId
      const siteId=this.$home.siteId
      curveList({strategyId,siteId}).then(response => {
        this.setOption(response?.data || [])
      })
    },
    setOption(data) {
      if(!this.chart) return
      let obj = {}
      for(var i=0;i<=23;i++){
        obj[i] = {
          title:i<=9?`0${i}:00` : `${i}:00`
        }
      }
      const nowMonth = new Date().getMonth()+1;
      const localMonth = data.find(item=>item.month === nowMonth)?.powerList || []
      localMonth.forEach(item => {
        const startHours = parseInt(item.startTime.split(':')[0], 10)
        const endHours =parseInt(item.endTime.split(':')[0], 10)
        for(let i=startHours;i<=endHours;i++){
          obj[i].value = item.powerData
        }
      })
      let source = [['时间','冲放功率']]
      Object.values(obj).forEach(item => {
        const {title,value} = item
        source.push([title,value])
      })
      this.chart.setOption({
        color:['#FFBD00','#3C81FF'],
        grid: {
          containLabel: true
        },
        legend: {
          left: 'center',
          bottom: '15',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        textStyle:{
          color:"#333333",
        },
        xAxis: {
          type: 'category',
          axisLine: {
            lineStyle:{
              color: '#333333',
            }
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            lineStyle:{
              color: '#333333',
            }
          }
        },
        dataset: {
          source
        },
        series: [
        {
          name:'冲放功率',
          type: 'line',
        }]
      })
    }
  }
}

</script>
