<template>
  <collapse
    id="collapse_OutputVolt"
    default-open
    :title="$t('pagePowerSup.OutputVolt')"
  >
    <template #image>
      <img src="@/assets/images/_sila/collapsed/voltage.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="100"
            >
            </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="voltage-output"
        :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: 72,
      shutdown: 95,
      isBusy: true,
      fields: [
        {
          key: 'name',
          label: this.$t('pagePowerSup.table.nameModule'),
          thStyle: { width: '25%' },
        },
        {
          key: 'current',
          label: this.$t('pagePowerSup.table.currentVolt'),
        },
        {
          key: 'min',
          label: this.$t('pagePowerSup.table.minVolt'),
        },
        {
          key: 'minDate',
          label: this.$t('pagePowerSup.table.minDate'),
        },
        {
          key: 'max',
          label: this.$t('pagePowerSup.table.maxVolt'),
        },
        {
          key: 'maxDate',
          label: this.$t('pagePowerSup.table.maxDate'),
        },
      ],
    };
  },

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

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

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

    colors() {
      return this.$randomColor({
        count: this.items?.length,
        hue: 'random',
        luminosity: 'random',
      });
    },
  },
  watch: {
    items() {
      this.items.length ? (this.isBusy = false) : (this.isBusy = true);
    },
  },
};
</script>
