<template>
  <el-card shadow="always" class="common-card-container" style="margin-top:20px">
    <div slot="header">
      <span class="card-title">电量指标</span>
    </div>
    <div class="card-main" 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="getData" 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 class="total-data">
        <div>总充电量:<span class="point">{{totalChargedCap | formatNumber}}kWh</span></div>
        <div>总放电量:<span class="point">{{totalDisChargedCap | formatNumber}}kWh</span></div>
        <div>综合效率:<span class="point">{{efficiency | formatNumber}}%</span></div>
      </div>
      <div id="dlzbChart" 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 {getElectricData} from '@/api/ems/dzjk'
import {formatDate} from '@/filters/ems'
export default {
  mixins: [resize,getQuerySiteId],
  data() {
    return {
      pickerOptions:{
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      defaultDateRange:[],//默认展示的时间
      dateRange:[],
      loading:false,
      chart: null,
      totalChargedCap:'',
      totalDisChargedCap:'',
      efficiency:''
    }
  },
  methods: {
    // 重置
    onReset(){
      this.dateRange=[]
      this.getData()
    },
    setOption(data,unit){
      const source = [['日期','充电量','放电量','效率']]
      data.forEach(item=>{
        source.push([item.ammeterDate, item.chargedCap,item.disChargedCap,item.dailyEfficiency])
      })
      this.chart.setOption({
        legend: {
          left: 'center',
          bottom: '15',
        },
        grid: {
          top:40,
          containLabel: true
        },
        tooltip: {},
        xAxis: [{
          type: 'category',
          name:`单位：${unit}`,
          nameLocation:'center',
          nameGap:30
        }],
        yAxis: [{
          type: 'value',
          name:'充电量/放电量kWh',
          axisLine: {
            lineStyle:{
              color: '#333333',
            },
            onZero:false
          }
        },{
          type: 'value',
          name:'效率%',
          axisLine: {
            lineStyle:{
              color: '#333333',
            },
            onZero:false
          }
        }],
        dataset:{
          source
        },
        //所有充放电颜色保持统一
        series: [
          {
            yAxisIndex:0,
            type: 'bar',//柱状图
            color:'#4472c4'
          },
          {
            yAxisIndex:0,
            type: 'bar',//柱状图
            color:'#70ad47'
          },
          {
            yAxisIndex:1,
            type: 'line',//柱状图
            color:'#FFBD00'
          },
        ]
      })
    },
    getData(){
      this.loading=true;
      //接口调用完成之后 设置图表、结束loading
      const [start='',end='']=this.dateRange || [];
      const startDate=formatDate(start),endDate = formatDate(end)
      getElectricData({siteId:this.siteId,startDate,endDate}).then(response => {
        const {totalChargedCap='',totalDisChargedCap='',efficiency='',sevenDayDisChargeStats=[],unit=''}=response?.data || {}
        this.setOption(sevenDayDisChargeStats || [],unit)
        this.totalChargedCap=totalChargedCap
        this.totalDisChargedCap=totalDisChargedCap
        this.efficiency=efficiency
      }).finally(() => {
        this.loading=false;
      })
    },
    init(){
      this.chart = echarts.init(document.querySelector('#dlzbChart'));
      this.onReset()
    }
  },
  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>


<style scoped lang="scss">
.total-data{
  line-height: 18px;
  color: #333333;
  font-size: 16px;
  padding:20px 0;
  >div{
    display: inline-block;
    margin-right: 20px;
    .point{
      color: #05AEA3;
      font-weight: 500;
      font-size: 18px;
    }
  }
}
</style>
