<template>
  <b-container fluid="xl">
    <page-title :description="$t('appPageTitle.dynamicInformation')" />
    <table-date-picker
      :class="{ disabledDiv: loading }"
      :time-scale="timeScale"
      @changePeriod="onChangePeriod"
    />
    <fan-speed-cpu :time-scale="timeScale"></fan-speed-cpu>
    <fan-speed-system :time-scale="timeScale"></fan-speed-system>
  </b-container>
</template>

<script>
import PageTitle from '@/components/_sila/Global/PageTitle';
import TableDatePicker from '@/components/_sila/Global/TableDatePicker';

import DataFormatterMixin from '@/components/_sila/Mixins/DataFormatterMixin';
import LoadingBarMixin, {
  loading,
} from '@/components/_sila/Mixins/LoadingBarMixin';
import TableFilterMixin from '@/components/_sila/Mixins/TableFilterMixin';

import FanSpeedCpu from './FanSpeedCpu';
import FanSpeedSystem from './FanSpeedSystem';

export default {
  components: { FanSpeedCpu, FanSpeedSystem, PageTitle, TableDatePicker },
  mixins: [DataFormatterMixin, LoadingBarMixin, TableFilterMixin],
  data() {
    return {
      loading,
      timeScale: 'hour',
    };
  },
  created() {
    this.startProgress();
  },
  methods: {
    resetZoom() {
      const resetButton = document.querySelector('.highcharts-reset-zoom');
      if (!resetButton) {
        return;
      }

      resetButton.dispatchEvent(new Event('click'));
    },
    onChangePeriod(period) {
      this.timeScale = period;
      this.startProgress();
      this.resetZoom();
    },
    startProgress() {
      this.startLoader();
      this.$root.$on('fan-cpu', (loading) => this.onLoading(loading));
      this.$root.$on('fan-system', (loading) => this.onLoading(loading));
    },
    onLoading(loading) {
      loading ? this.startLoader() : this.endLoader();
    },
  },
};
</script>
