<template>
  <collapse
    id="collapse_InputPower"
    :title="$t('pagePowerSup.InputPower')"
    @opened="onOpened"
  >
    <template #image>
      <img src="@/assets/images/_sila/collapsed/power.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.warning')">
            <b-form-input
              v-model="warning"
              type="number"
              :min="0"
              :max="shutdown"
            ></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.shutdown')">
            <b-form-input
              v-model="shutdown"
              type="number"
              :min="warning"
              :max="125"
            >
            </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="psu-power"
        :time-scale="timeScale"
        :data="filteredItems"
        :colors="colors"
        :warning="+warning"
        :shutdown="+shutdown"
      ></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 LoadingBarMixin from '@/components/_sila/Mixins/LoadingBarMixin';
import TableFilterMixin from '@/components/_sila/Mixins/TableFilterMixin';
import Collapse from '@/components/_sila/Global/Collapse';

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

export default {
  components: { PageSection, Chart, Collapse },
  mixins: [DataFormatterMixin, LoadingBarMixin, TableFilterMixin],
  props: {
    timeScale: {
      type: String,
      default: 'hour',
    },
  },
  data() {
    return {
      warning: Infinity,
      shutdown: Infinity,
      isBusy: true,
      opened: false,
      fields: [
        {
          key: 'name',
          label: this.$t('pagePowerSup.table.nameModule'),
          thStyle: { width: '25%' },
        },
        {
          key: 'current',
          label: this.$t('pagePowerSup.table.currentPower'),
        },
        {
          key: 'min',
          label: this.$t('pagePowerSup.table.minPower'),
        },
        {
          key: 'minDate',
          label: this.$t('pagePowerSup.table.minDate'),
        },
        {
          key: 'max',
          label: this.$t('pagePowerSup.table.maxPower'),
        },
        {
          key: 'maxDate',
          label: this.$t('pagePowerSup.table.maxDate'),
        },
      ],
    };
  },

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

    filteredItems() {
      return psuFilter(this.allSensors, 'Input');
    },

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

    colors() {
      return this.$randomColor({
        count: this.items?.length,
        hue: 'random',
        luminosity: 'random',
      });
    },
  },
  watch: {
    timeScale() {
      if (!this.opened) {
        return;
      }

      this.loadData();
    },
  },
  methods: {
    onOpened(state) {
      if (state) {
        this.loadData();
      }
      this.opened = state;
    },

    loadData() {
      let payload = { metricsName: 'psu_power', lastHour: false };
      if (this.timeScale === 'hour') {
        payload.lastHour = true;
      }

      this.$root.$emit('psu-power-input', true);
      this.startLoader();
      this.$store.dispatch('powerSupply/getPsu', payload).finally(() => {
        this.$root.$emit('psu-power-input', false);
        this.isBusy = false;
        this.endLoader();
      });
    },
  },
};
</script>
