
<template>
  <div v-loading="loading">
    <div class="select-container">
      <el-form :inline="true">
        <el-form-item label="时间选择">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            :picker-options="pickerOptions"
            :default-value="defaultDateRange"
            end-placeholder="结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch" native-type="button">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="onReset" native-type="button">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="margin:30px 0;">
      <div id="glqxEchart" style="height:360px;"></div>
    </div>
  </div>
</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";
export default {
  name:'DzjkTjbbGlqx',
  mixins: [resize,getQuerySiteId],
  data() {
    return {
      pickerOptions:{
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      defaultDateRange:[],//默认展示的时间
      dateRange:[],
      loading:false,
    }
  },
  methods: {
    // 搜索
    onSearch(){
      this.getData()
    },
    // 重置
    onReset(){
      this.dateRange=[]
      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.loading = true
      this.initChart()
      this.getData()
    }
  },
  mounted(){
    const now = new Date();
    const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);
    this.defaultDateRange = [lastMonth, now];
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
}
</script>

