
<template>
  <el-card shadow="always" class="common-card-container time-range-card" style="margin-top:20px">
    <div slot="header" class="time-range-header">
      <span class="card-title">功率曲线</span>
      <date-range-select ref="dateRangeSelect" @updateDate="updateDate"/>
    </div>
    <div class="card-main" v-loading="loading">
      <div id="glqxEchart" style="height: 310px;"></div>
    </div>
  </el-card>
</template>


<script>
import * as echarts from 'echarts'
import resize from "@/mixins/ems/resize";
import getQuerySiteId from "@/mixins/ems/getQuerySiteId";
import {getPcsNameList, getPowerData} from "@/api/ems/dzjk";
import {formatDate} from "@/filters/ems";
import DateRangeSelect from "@/components/Ems/DateRangeSelect/index.vue";
export default {
  name:'DzjkTjbbGlqx',
  components: {DateRangeSelect},
  mixins: [resize,getQuerySiteId],
  data() {
    return {
      pickerOptions:{
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      dateRange:[],
      loading:false,
    }
  },
  methods: {
    // 更新时间范围 重置图表
    updateDate(data){
      this.dateRange=data || []
      this.getData()
    },
    getData(){
      const {siteId}=this;
      const [start='',end='']=(this.dateRange || [])
      //接口调用完成之后 设置图表、结束loading
      this.loading=true;
      getPowerData({siteId,startDate:formatDate(start),endDate:formatDate(end)}).then(response => {
        this.setOption(response?.data || [])
      }).finally(()=>{this.loading=false;})
    },
    setOption(data) {
      const source = [['日期','电网功率','负载功率','储能功率','光伏功率']]
      data.forEach(item=>{
        source.push([item.statisDate,item.gridPower,item.loadPower,item.storagePower,item.pvPower])
      })
      this.chart.setOption({
        grid: {
          containLabel: true
        },
        legend: {
          left: 'center',
          bottom: '15',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        textStyle:{
          color:"#333333",
        },
        xAxis: {
          type: 'category',
         },
        yAxis: {
          type: 'value',
        },
        dataset:{source},
        series: [
          {
            type: 'scatter',
          },
          {
            type: 'scatter',
          },
          {
            type: 'scatter',
          },
          {
            type: 'scatter',
          }
        ]
      },true)
    },
    initChart() {
      this.chart = echarts.init(document.querySelector('#glqxEchart'));
    },
    init(){
      this.$nextTick(()=>{
        this.initChart()
        this.$refs.dateRangeSelect.init()
      })
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
}
</script>

