<i18n>
{
  "en": {
    "id": "ID",
    "name": "Name",
    "description": "Description",
    "entries": "Entries",
    "configs": "Configs",
    "deleteConfigs": "Delete one config | Delete {0} configs",
    "createConfig": "Create config",
    "deleteEntries": "Delete one entry | Delete {0} entries",
    "createEntry": "Create entry",
    "successSetAsDefault": "Successfully set config as default.",
    "defaultConfig": "Default Config"
  },
  "de": {
    "id": "ID",
    "name": "Name",
    "description": "Beschreibung",
    "entries": "Einträge",
    "configs": "Konfigurationen",
    "deleteConfigs": "Eine Konfiguration löschen | {0} Konfigurationen löschen",
    "createConfig": "Konfiguration erstellen",
    "deleteEntries": "Einen Eintrag löschen | {0} Einträge löschen",
    "createEntry": "Eintrag erstellen",
    "successSetAsDefault": "Konfiguration erfolgreich als Standard gesetzt.",
    "defaultConfig": "Standard Konfiguration"
  }
}
</i18n>

<template>
  <v-container fill-height>
    <v-layout>
      <v-flex xl10 offset-xl1 lg12>
        <v-card class="tabbar-card">
          <v-tabs v-model="tabs" grow :dark="tabsDark" :background-color="tabsColor" :slider-color="tabsSliderColor">
              <v-tab><v-icon class="tabbar-tabicon">list</v-icon>{{ $t('configs') }}</v-tab>
              <v-tab><v-icon class="tabbar-tabicon">code</v-icon>{{ $t('entries') }}</v-tab>
          </v-tabs>
        </v-card>
        <v-tabs-items v-model="tabs" style="padding-bottom: 20px" touchless>
          <v-tab-item :transition="false" :reverse-transition="false">
            <v-subheader>{{ $t('configs') }}</v-subheader>
            <v-card>
              <data-table v-model="selectedConfigs" :headers="configHeaders" :items="configs" min-width="800px" @dblclick="editConfig($event)">
                <div slot="actions" slot-scope="row" style="display: flex; justify-content: flex-end; align-items: center">
                  <v-tooltip top v-if="row.item.isDefault">
                    <template #activator="{ on }">
                      <v-icon v-on="on" color="success" style="width: 36px">flag</v-icon>
                    </template>
                    <span>{{ $t('defaultConfig') }}</span>
                  </v-tooltip>
                  <v-btn v-else icon @click.stop="setAsDefault(row.item.id)">
                    <v-icon style="opacity: 0.2">outlined_flag</v-icon>
                  </v-btn>
                  <v-btn text small @click.stop="assignConfig(row.item)" class="mx-2">
                    <span class="mr-1">{{ row.item.groupCount }}</span><v-icon small>category</v-icon>
                    <span class="mx-1">/</span>
                    <span class="mr-1">{{ row.item.clientCount }}</span><v-icon small>computer</v-icon>
                  </v-btn>
                  <v-btn text icon @click.stop="editConfig(row.item)"><v-icon color="primary">edit</v-icon></v-btn>
                </div>
              </data-table>
            </v-card>
            <div class="text-right">
              <v-btn text color="error" @click="deleteSelectedConfigs" :disabled="selectedConfigs.length === 0" class="ma-2">
                <v-icon left>delete</v-icon>{{ $tc('deleteConfigs', selectedConfigs.length, [selectedConfigs.length]) }}
              </v-btn>
              <v-btn text color="success" @click="createConfig" class="ma-2">
                <v-icon left>create</v-icon>{{ $t('createConfig') }}
              </v-btn>
            </div>
          </v-tab-item>
          <v-tab-item :transition="false" :reverse-transition="false">
            <v-subheader>{{ $t('entries') }}</v-subheader>
            <v-card>
              <data-table v-model="selectedEntries" :headers="entryHeaders" :items="entries" min-width="600px" @dblclick="editEntry($event)">
                <div slot="actions" slot-scope="row" style="text-align: right">
                  <v-btn icon @click.stop="editEntry(row.item)"><v-icon color="primary">edit</v-icon></v-btn>
                </div>
              </data-table>
            </v-card>
            <div class="text-right">
              <v-btn text color="error" @click="deleteSelectedEntries" :disabled="selectedEntries.length === 0" class="ma-2">
                <v-icon left>delete</v-icon>{{ $tc('deleteEntries', selectedEntries.length, [selectedEntries.length]) }}
              </v-btn>
              <v-btn text color="success" @click="createEntry" class="ma-2">
                <v-icon left>create</v-icon>{{ $t('createEntry') }}
              </v-btn>
            </div>
          </v-tab-item>
        </v-tabs-items>
      </v-flex>
    </v-layout>
    <v-dialog
      :value="dialog.show"
      @input="setDialog({ show: $event })"
      :max-width="dialogWidthMap[dialog.type]"
      scrollable
      :persistent="dialog.type !== 'delete'"
      :fullscreen="$vuetify.breakpoint.smAndDown"
    >
      <configurator-module-delete v-if="dialog.type === 'delete'" />
      <configurator-module-config v-else-if="dialog.type === 'config'"/>
      <configurator-module-entry v-else-if="dialog.type === 'entry'" />
      <configurator-module-assign v-else-if="dialog.type === 'assign'" />
    </v-dialog>
  </v-container>
</template>

<script>
import DataTable from '@/components/DataTable'
import ConfiguratorModuleDelete from '@/components/ConfiguratorModuleDelete'
import ConfiguratorModuleConfig from '@/components/ConfiguratorModuleConfig'
import ConfiguratorModuleEntry from '@/components/ConfiguratorModuleEntry'
import ConfiguratorModuleAssign from '@/components/ConfiguratorModuleAssign'
import { mapState, mapGetters, mapMutations } from 'vuex'

export default {
  name: 'ConfiguratorModule',
  components: {
    DataTable,
    ConfiguratorModuleDelete,
    ConfiguratorModuleConfig,
    ConfiguratorModuleEntry,
    ConfiguratorModuleAssign
  },
  data () {
    return {
      tabs: 0,
      selectedConfigs: [],
      selectedEntries: [],
      dialogWidthMap: {
        'delete': '500px',
        'assign': '600px',
        'config': '1200px',
        'entry': '1200px'
      }
    }
  },
  computed: {
    ...mapGetters(['tabsDark', 'tabsColor', 'tabsSliderColor']),
    ...mapState('configurator', ['configs', 'entries', 'dialog']),
    configHeaders () {
      return [
        { key: 'id', text: this.$t('id'), width: '50px' },
        { key: 'name', text: this.$t('name'), width: '220px' },
        { key: 'description', text: this.$t('description') },
        { key: 'actions', width: '210px' }
      ]
    },
    entryHeaders () {
      return [
        { key: 'id', text: this.$t('id'), width: '50px' },
        { key: 'name', text: this.$t('name') },
        { key: 'actions', width: '60px' }
      ]
    }
  },
  watch: {
    configs () { this.selectedConfigs = [] },
    entries () { this.selectedEntries = [] }
  },
  methods: {
    ...mapMutations('configurator', ['setDialog']),
    deleteSelectedConfigs () {
      this.setDialog({ show: true, type: 'delete', info: { itemType: 'configs', selected: this.selectedConfigs } })
    },
    deleteSelectedEntries () {
      this.setDialog({ show: true, type: 'delete', info: { itemType: 'entries', selected: this.selectedEntries } })
    },
    createConfig () {
      this.setDialog({ show: true, type: 'config', info: {} })
    },
    createEntry () {
      this.setDialog({ show: true, type: 'entry', info: {} })
    },
    assignConfig (item) {
      this.setDialog({ show: true, type: 'assign', info: item })
    },
    editConfig (item) {
      this.setDialog({ show: true, type: 'config', info: item })
    },
    editEntry (item) {
      this.setDialog({ show: true, type: 'entry', info: item })
    },
    async setAsDefault (id) {
      await this.$http.put('/api/ipxeconfigs/' + id + '/default')
      this.$snackbar({ text: this.$t('successSetAsDefault'), color: 'success', timeout: 2000 })
      this.$store.dispatch('configurator/loadData')
    }
  },
  created () {
    this.$store.dispatch('configurator/loadData')
    this.$store.dispatch('groups/loadGroupList')
    this.$store.dispatch('groups/loadClientList')
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.narrow-td {
  width: 10px;
}
</style>
