<template>
  <DataSearchBar @getDate="getDate" />
  <IntegralElectric ref="child1" class="mt-4" />
  <TotalCorrosion ref="child2" class="mt-4" />
  <MoistTime ref="child3" class="mt-4" />
</template>

<script setup>
import DataSearchBar from 'components/data-search-bar.vue';
import IntegralElectric from 'components/integral-electric.vue';
import TotalCorrosion from 'components/total-corrosion.vue';
import MoistTime from 'components/moist-time.vue';
import { computed, ref } from 'vue';
import { useStore } from 'vuex';

const child1 = ref(null);
const child2 = ref(null);
const child3 = ref(null);

let timer = null;
const store = useStore();
const token = computed(() => store.getters['user/token']);
const electricData = computed(() => store.state.statistics.electricData);
const corrosionData = computed(() => store.state.statistics.corrosionData);
const moistTimeData = computed(() => store.state.statistics.moistTimeData);

// 获取月累计数据
const getDate = async options => {
  if (token) {
    const params = {
      deviceId: options && options.deviceId ? options.deviceId : '', // 站点 设备id
      date: options && options.date && options.date.length ? options.date : [], // 年月时间段
      sort: 1, // 1 -> 按时间正序 -1->按时间倒序
    };
    await store.dispatch('statistics/getMonthsDate', params);
    timer && clearTimeout(timer);
    timer = null;
    // 渲染图表
    child1.value.setDate(electricData.value);
    child2.value.setDate(corrosionData.value);
    child3.value.setDate(moistTimeData.value);
  } else {
    timer = setTimeout(() => {
      getDate();
    });
  }
};

getDate();
</script>
