<template>
  <b-container fluid="xl">
    <page-title />
    <page-section :section-title="$t('pagePci.title')">
      <b-table
        responsive="md"
        show-empty
        hover
        :items="items"
        :fields="fields"
        :empty-text="$t('global.table.emptyMessage')"
        :busy="isBusy"
      >
      </b-table>
    </page-section>
  </b-container>
</template>

<script>
import PageTitle from '@/components/_sila/Global/PageTitle';
import PageSection from '@/components/_sila/Global/PageSection';

import DataFormatterMixin from '@/components/_sila/Mixins/DataFormatterMixin';
import LoadingBarMixin from '@/components/_sila/Mixins/LoadingBarMixin';
import TableRowExpandMixin, {
  expandRowLabel,
} from '@/components/_ibs/Mixins/TableRowExpandMixin';

export default {
  components: { PageTitle, PageSection },
  mixins: [DataFormatterMixin, LoadingBarMixin, TableRowExpandMixin],
  data() {
    return {
      isBusy: true,
      fields: [
        {
          key: 'expandRow',
          label: '',
          tdClass: 'table-row-expand',
          sortable: false,
        },
        {
          key: 'DeviceName',
          label: this.$t('pagePci.table.name'),
          formatter: this.dataFormatter,
        },
        {
          key: 'health',
          label: this.$t('pagePci.table.health'),
          formatter: this.dataFormatter,
          tdClass: 'text-nowrap',
        },
        {
          key: 'deviceClass',
          label: this.$t('pagePci.table.type'),
          formatter: this.dataFormatter,
        },
        {
          key: 'manufacturer',
          label: this.$t('pagePci.table.manufacturer'),
          formatter: this.dataFormatter,
        },
      ],
      expandRowLabel: expandRowLabel,
    };
  },

  computed: {
    items() {
      return this.$store.getters['pci/pciDevices'];
    },
  },

  created() {
    this.startLoader();
    return this.$store
      .dispatch('pci/getDevices')
      .then(() => {
        return this.$store.dispatch('pci/getDevicesMembers');
      })
      .then(() => {
        this.$store.dispatch('pci/getFunctionDevices');
      })
      .finally(() => {
        this.endLoader();
        this.isBusy = false;
      });
  },
};
</script>
