<template>
  <collapse
    id="collapse_psuTempp"
    :title="$t('pagePowerSup.temperature')"
    @opened="onOpened"
  >
    <template #image>
      <img src="@/assets/images/_sila/collapsed/temperature.svg" />
    </template>
    <page-section>
      <!-- <b-row class="align-items-end limit-container">
        <b-col xs="12" sm="6" xl="3" class="pt-4">
          <b-form-group :label="$t('pagePowerSup.labels.notNormal')">
            <b-form-input
              v-model="notNormal"
              type="number"
              :min="0"
              :max="100"
            ></b-form-input>
          </b-form-group>
        </b-col>
        <b-col xs="12" sm="6" xl="3" class="pt-4">
          <b-form-group :label="$t('pagePowerSup.labels.critical')">
            <b-form-input v-model="critical" type="number" :min="0" :max="100">
            </b-form-input>
          </b-form-group>
        </b-col>
        <b-col xs="12" sm="6" xl="3" class="pt-4">
          <b-form-group :label="$t('pagePowerSup.labels.warning')">
            <b-form-input v-model="warning" type="number" :min="0" :max="100">
            </b-form-input>
          </b-form-group>
        </b-col>
        <b-col xs="12" sm="6" xl="3" class="pt-4">
          <b-button variant="primary" style="height: 35px">
            {{ $t('global.action.save') }}
          </b-button>
        </b-col>
      </b-row> -->
      <chart
        type="processors"
        :time-scale="timeScale"
        :data="allSensors"
        :colors="colors"
        :warning="+warning"
        :not-normal="+notNormal"
        :critical="+critical"
      ></chart>
      <b-table
        responsive="md"
        show-empty
        table-variant="accessory"
        hover
        :items="items"
        :fields="fields"
        :empty-text="$t('global.table.emptyMessage')"
        :busy="isBusy"
      >
        <template #cell(name)="{ value, index }">
          <div
            class="item-color"
            :style="`background-color: ${colors[index]}`"
          ></div>
          {{ value }}
        </template>
        <template #cell(minDate)="{ value }">
          <span style="color: rgb(12, 28, 41)">
            {{ value.time }}
          </span>
          <span>
            {{ value.date }}
          </span>
        </template>
        <template #cell(maxDate)="{ value }">
          <span style="color: rgb(12, 28, 41)">
            {{ value.time }}
          </span>
          <span>
            {{ value.date }}
          </span>
        </template>
      </b-table>
    </page-section>
  </collapse>
</template>
<script>
import Chart from '@/components/_sila/Global/Chart';
import PageSection from '@/components/Global/PageSection';

import DataFormatterMixin from '@/components/_sila/Mixins/DataFormatterMixin';
import TableFilterMixin from '@/components/_sila/Mixins/TableFilterMixin';
import LoadingBarMixin from '@/components/_sila/Mixins/LoadingBarMixin';
import Collapse from '@/components/_sila/Global/Collapse';

import { getItems } from '@/utilities/_sila/metricProperties';

export default {
  components: { PageSection, Chart, Collapse },
  mixins: [DataFormatterMixin, TableFilterMixin, LoadingBarMixin],
  props: {
    timeScale: {
      type: String,
      default: 'hour',
    },
  },
  data() {
    return {
      warning: 72,
      notNormal: 44,
      critical: 55,
      isBusy: true,
      fields: [
        {
          key: 'name',
          label: this.$t('pagePowerSup.table.nameModule'),
          thStyle: { width: '25%' },
        },
        {
          key: 'current',
          label: this.$t('pagePowerSup.table.currentTemperature'),
        },
        {
          key: 'middle',
          label: this.$t('pagePowerSup.table.middleTemperature'),
        },
        {
          key: 'min',
          label: this.$t('pagePowerSup.table.minTemperature'),
        },
        {
          key: 'minDate',
          label: this.$t('pagePowerSup.table.minDate'),
        },
        {
          key: 'max',
          label: this.$t('pagePowerSup.table.maxTemperature'),
        },
        {
          key: 'maxDate',
          label: this.$t('pagePowerSup.table.maxDate'),
        },
      ],
    };
  },

  computed: {
    items() {
      return getItems(this.allSensors);
    },

    allSensors() {
      return this.timeScale === 'hour'
        ? this.$store.getters['powerSupply/psuTempLastHour']
        : this.$store.getters['powerSupply/psuTemp'];
    },

    colors() {
      return this.$randomColor({
        count: this.items?.length,
        hue: 'random',
        luminosity: 'random',
      });
    },
  },
  watch: {
    timeScale() {
      this.loadData();
    },
  },
  created() {
    this.loadData();
  },
  methods: {
    onOpened(state) {
      if (state) {
        this.loadData();
      }
    },
    loadData() {
      let payload = { metricsName: 'psu_temp', lastHour: false };
      if (this.timeScale === 'hour') {
        payload.lastHour = true;
      }
      this.startLoader();
      this.$store.dispatch('powerSupply/getPsu', payload).finally(() => {
        this.$root.$emit('psu-temp');
        this.isBusy = false;
        this.endLoader();
      });
    },
  },
};
</script>
