<template>
  <collapse
    id="collapse_InputCur"
    default-open
    :title="$t('pagePowerSup.InputCurrent')"
  >
    <template #image>
      <img src="@/assets/images/_sila/collapsed/current.svg" />
    </template>
    <page-section>
      <b-row class="align-items-end p-2">
        <b-col xs="12" md="6" lg="4" class="pt-2">
          <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" md="6" lg="4" class="pt-2">
          <b-form-group :label="$t('pagePowerSup.labels.shutdown')">
            <b-form-input
              v-model="shutdown"
              type="number"
              :min="warning"
              :max="10"
            >
            </b-form-input>
          </b-form-group>
        </b-col>
        <b-col xs="12" md="6" lg="3" class="pt-2">
          <b-button variant="primary" style="height: 35px">
            {{ $t('global.action.save') }}
          </b-button>
        </b-col>
      </b-row>
      <chart
        type="current"
        :time-scale="timeScale"
        :data="filteredItems"
        float
        :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: 5,
      shutdown: 6,
      isBusy: true,
      fields: [
        {
          key: 'name',
          label: this.$t('pagePowerSup.table.nameModule'),
          thStyle: { width: '25%' },
        },
        {
          key: 'current',
          label: this.$t('pagePowerSup.table.currentCurrent'),
        },
        {
          key: 'min',
          label: this.$t('pagePowerSup.table.minCurrent'),
        },
        {
          key: 'minDate',
          label: this.$t('pagePowerSup.table.minDate'),
        },
        {
          key: 'max',
          label: this.$t('pagePowerSup.table.maxCurrent'),
        },
        {
          key: 'maxDate',
          label: this.$t('pagePowerSup.table.maxDate'),
        },
      ],
    };
  },
  computed: {
    items() {
      return getItems(this.filteredItems, 3);
    },

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

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

    colors() {
      return this.$randomColor({
        count: this.items?.length,
        hue: 'random',
        luminosity: 'random',
      });
    },
  },
  watch: {
    timeScale() {
      this.loadData();
    },
  },
  created() {
    this.loadData();
  },
  methods: {
    loadData() {
      let payload = { metricsName: 'psu_current', lastHour: false };
      if (this.timeScale === 'hour') {
        payload.lastHour = true;
      }

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