<template>
  <b-container fluid="xl">
    <page-title :description="$t('appPageTitle.dynamicInformation')" />
    <table-date-picker style="margin: -2rem 0 0 -2rem" />
    <collapse
      id="collapse_1"
      :title="$t('pageProcessors.temperature')"
      style="margin-top: 0"
    >
      <template #image>
        <img src="@/assets/images/_sila/collapsed/temperature.svg" />
      </template>
      <page-section>
        <b-row class="align-items-end p-2">
          <b-col xs="12" md="6" lg="3" class="pt-2">
            <b-form-group :label="$t('pageProcessors.labels.notNormal')">
              <b-form-input
                v-model="temperatureNonNormalInput"
                type="number"
                :min="0"
                :max="temperatureCriticalInput"
              ></b-form-input>
            </b-form-group>
          </b-col>
          <b-col xs="12" md="6" lg="3" class="pt-2">
            <b-form-group :label="$t('pageProcessors.labels.critical')">
              <b-form-input
                v-model="temperatureCriticalInput"
                type="number"
                :min="temperatureNonNormalInput"
                :max="temperatureWarningInput"
              >
              </b-form-input>
            </b-form-group>
          </b-col>
          <b-col xs="12" md="6" lg="3" class="pt-2">
            <b-form-group :label="$t('pageProcessors.labels.warning')">
              <b-form-input
                v-model="temperatureWarningInput"
                type="number"
                :min="temperatureCriticalInput"
                :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"
              @click="updateTemperature"
            >
              {{ 'Сохранить' }}
            </b-button>
          </b-col>
        </b-row>
        <chart
          type="processors"
          :data="filteredSensors"
          :colors="colors"
          :warning="temperatureWarning"
          :non-normal="temperatureNonNormal"
          :critical-start="temperatureCritical"
        ></chart>
        <b-table
          v-if="items && items.length > 0"
          responsive="md"
          show-empty
          table-variant="accessory"
          hover
          :items="items"
          :fields="fields"
          :empty-text="$t('global.table.emptyMessage')"
        >
          <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>
  </b-container>
</template>
<script>
import PageTitle from '@/components/_sila/Global/PageTitle';
import Chart from '@/components/_sila/Global/Chart';
import PageSection from '@/components/Global/PageSection';

import DataFormatterMixin from '@/components/_sila/Mixins/DataFormatterMixin';
import TableDatePicker from '@/components/_sila/Global/TableDatePicker';
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';

export default {
  components: { PageTitle, Collapse, PageSection, Chart, TableDatePicker },
  mixins: [DataFormatterMixin, LoadingBarMixin, TableFilterMixin],
  data() {
    return {
      activeFilters: [],
      timeScale: 'hour',
      temperatureWarning: 72,
      temperatureWarningInput: 72,
      temperatureNonNormal: 44,
      temperatureNonNormalInput: 44,
      temperatureCritical: 55,
      temperatureCriticalInput: 55,
      frequencyWarning: 660,
      frequencyWarningInput: 660,
      powerWarning: 66,
      powerWarningInput: 66,
      powerShutdown: 88,
      powerShutdownInput: 88,
      notificationInput: 42,
      fields: [
        {
          key: 'name',
          label: this.$t('pageProcessors.table.name'),
          thStyle: { width: '25%' },
        },
        {
          key: 'current',
          label: this.$t('pageProcessors.table.currentTemperature'),
        },
        {
          key: 'middle',
          label: this.$t('pageProcessors.table.middleTemperature'),
        },
        {
          key: 'min',
          label: this.$t('pageProcessors.table.minTemperature'),
        },
        {
          key: 'minDate',
          label: this.$t('pageProcessors.table.minDate'),
        },
        {
          key: 'max',
          label: this.$t('pageProcessors.table.maxTemperature'),
        },
        {
          key: 'maxDate',
          label: this.$t('pageProcessors.table.maxDate'),
        },
      ],
      items: [],
    };
  },

  computed: {
    allSensors() {
      let sensors = this.$store.getters['processors/processors'];
      return sensors;
    },

    filteredSensors() {
      return this.getFilteredTableData(this.allSensors, this.activeFilters);
    },
    colors() {
      return this.$randomColor({
        count: this.items?.length,
        hue: 'random',
        luminosity: 'random',
      });
    },
  },
  watch: {
    filteredSensors(data) {
      this.items = getItems(data);
    },
  },
  created() {
    this.loadData();
  },

  methods: {
    updateTemperature() {
      this.temperatureWarning = +this.temperatureWarningInput;
      this.temperatureNonNormal = +this.temperatureNonNormalInput;
      this.temperatureCritical = +this.temperatureCriticalInput;
    },

    loadData() {
      this.startLoader();
      this.$store.dispatch('processors/getProcessorsDynamic').finally(() => {
        this.endLoader();
      });
    },
  },
};
</script>
