<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"
  },
  "de": {
    "id": "ID",
    "name": "Name",
    "description": "Beschreibung",
    "entries": "Einträge",
    "configs": "Konfigurationen",
    "deleteConfigs": "Lösche eine Konfiguration | Lösche {0} Konfigurationen",
    "createConfig": "Konfiguration erstellen",
    "deleteEntries": "Lösche einen Eintrag | Lösche {0} Einträge",
    "createEntry": "Eintrag erstellen"
  }
}
</i18n>

<template>
  <v-container fill-height>
    <v-layout>
      <v-flex class="tabs-wrapper" xl10 offset-xl1 lg12>
        <v-card>
          <v-tabs v-model="tabs" centered :dark="tabsDark" :color="tabsColor" :slider-color="tabsSliderColor">
              <v-tab>{{ $t('configs') }}</v-tab>
              <v-tab>{{ $t('entries') }}</v-tab>
          </v-tabs>
        </v-card>
        <v-tabs-items v-model="tabs" style="padding-bottom: 20px">
          <v-tab-item>
            <v-subheader>{{ $t('configs') }}</v-subheader>
            <v-card>
              <component-search-table v-model="selectedConfigs" :headers="configHeaders" :items="configs" select-all>
                <template slot="items" slot-scope="row">
                  <tr :style="row.color" @click="row.data.selected = !row.data.selected" @dblclick="editConfig(row.data.item)">
                    <td style="width: 10px">
                      <v-checkbox
                        color="primary"
                        v-model="row.data.selected"
                        hide-details
                      ></v-checkbox>
                    </td>
                    <td class="narrow-td">{{ row.data.item.id }}</td>
                    <td>{{ row.data.item.name }}</td>
                    <td>{{ row.data.item.description }}</td>
                    <td class="narrow-td">
                      <v-btn icon @click.stop="editConfig(row.data.item)"><v-icon color="primary">edit</v-icon></v-btn>
                    </td>
                  </tr>
                </template>
              </component-search-table>
            </v-card>
            <div class="text-xs-right">
              <v-btn flat color="error" @click="deleteSelectedConfigs" :disabled="selectedConfigs.length === 0">
                <v-icon left>delete</v-icon>{{ $tc('deleteConfigs', selectedConfigs.length, [selectedConfigs.length]) }}
              </v-btn>
              <v-btn flat color="success" @click="createConfig"><v-icon left>create</v-icon>{{ $t('createConfig') }}</v-btn>
            </div>
          </v-tab-item>
          <v-tab-item>
            <v-subheader>{{ $t('entries') }}</v-subheader>
            <v-card>
              <component-search-table v-model="selectedEntries" :headers="entryHeaders" :items="entries" select-all>
                <template slot="items" slot-scope="row">
                  <tr :style="row.color" @click="row.data.selected = !row.data.selected" @dblclick="editEntry(row.data.item)">
                    <td style="width: 10px">
                      <v-checkbox
                        color="primary"
                        v-model="row.data.selected"
                        hide-details
                      ></v-checkbox>
                    </td>
                    <td class="narrow-td">{{ row.data.item.id }}</td>
                    <td>{{ row.data.item.name }}</td>
                    <td class="narrow-td">
                      <v-btn icon @click.stop="editEntry(row.data.item)"><v-icon color="primary">edit</v-icon></v-btn>
                    </td>
                  </tr>
                </template>
              </component-search-table>
            </v-card>
            <div class="text-xs-right">
              <v-btn flat color="error" @click="deleteSelectedEntries" :disabled="selectedEntries.length === 0">
                <v-icon left>delete</v-icon>{{ $tc('deleteEntries', selectedEntries.length, [selectedEntries.length]) }}
              </v-btn>
              <v-btn flat color="success" @click="createEntry"><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="dialog.type === 'delete' ? '500px' : '1000px'"
      scrollable
      :persistent="dialog.type !== 'delete'"
    >
      <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'" />
    </v-dialog>
  </v-container>
</template>

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

export default {
  name: 'ConfiguratorModule',
  components: {
    ComponentSearchTable,
    ConfiguratorModuleDelete,
    ConfiguratorModuleConfig,
    ConfiguratorModuleEntry
  },
  data () {
    return {
      tabs: 0,
      selectedConfigs: [],
      selectedEntries: []
    }
  },
  computed: {
    ...mapGetters(['tabsDark', 'tabsColor', 'tabsSliderColor']),
    ...mapState('configurator', ['configs', 'entries', 'dialog']),
    configHeaders () {
      return [
        { text: this.$t('id'), value: 'id' },
        { text: this.$t('name'), value: 'name' },
        { text: this.$t('description'), value: 'description' },
        { sortable: false }
      ]
    },
    entryHeaders () {
      return [
        { text: this.$t('id'), value: 'id' },
        { text: this.$t('name'), value: 'name' },
        { sortable: false }
      ]
    }
  },
  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: {} })
    },
    editConfig (item) {
      this.setDialog({ show: true, type: 'config', info: item })
    },
    editEntry (item) {
      this.setDialog({ show: true, type: 'entry', info: item })
    }
  },
  created () {
    this.$store.dispatch('configurator/loadData')
  }
}
</script>

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