<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 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'
import DateRangeSelect from '@/components/Ems/DateRangeSelect/index.vue'
export default {
  components: {DateRangeSelect},
  mixins: [resize,getQuerySiteId],
  data() {
    return {
      pickerOptions:{
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      dateRange:[],
      loading:false,
      chart: null,
      totalChargedCap:'',
      totalDisChargedCap:'',
      efficiency:''
    }
  },
  methods: {
    // 更新时间范围 重置图表
    updateDate(data){
      this.dateRange=data || []
      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: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        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
      }).catch(() => {
        this.setOption([], '')
        this.totalChargedCap=''
        this.totalDisChargedCap=''
        this.efficiency=''
        // 错误提示由全局请求拦截器处理，这里兜底避免出现 Uncaught (in promise)
      }).finally(() => {
        this.loading=false;
      })
    },
    init(){
      this.$nextTick(()=>{
        this.chart = echarts.init(document.querySelector('#dlzbChart'));
        this.$refs.dateRangeSelect.init()
      })

    }
  },
  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>
