<i18n>
{
  "en": {
    "id": "ID",
    "name": "Name",
    "description": "Description",
    "removeSubgroups": "Remove one subgroup | Remove {0} subgroups",
    "addSubgroups": "Add subgroups",
    "deleteGroups": "Delete one group | Delete {0} groups",
    "createGroup": "Create group"
  },
  "de": {
    "id": "ID",
    "name": "Name",
    "description": "Beschreibung",
    "removeSubgroups": "Entferne eine Untergruppe | Entferne {0} Untergruppen",
    "addSubgroups": "Füge Untergruppen hinzu",
    "deleteGroups": "Lösche eine Gruppe | Lösche {0} Gruppen",
    "createGroup": "Gruppe erstellen"
  }
}
</i18n>

<template>
  <div>
    <v-card>
      <component-search-table v-model="selected" :headers="headers" :items="groups" select-all>
        <template slot="items" slot-scope="row">
          <tr :style="row.color" @click="row.data.selected = !row.data.selected" @dblclick="loadGroup(row.data.item.id)">
            <td class="narrow-td">
              <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 class="next-arrow" icon @click.stop="loadGroup(row.data.item.id)"><v-icon>keyboard_arrow_right</v-icon></v-btn>
            </td>
          </tr>
        </template>
      </component-search-table>
    </v-card>
    <div v-if="tabIndex === 0" class="text-xs-right">
      <v-btn flat color="error" @click="deleteSelected" :disabled="selected.length === 0">
        <v-icon left>delete</v-icon>{{ $tc('deleteGroups', selected.length, [selected.length]) }}
      </v-btn>
      <v-btn flat color="success" @click="newGroup"><v-icon left>create</v-icon>{{ $t('createGroup') }}</v-btn>
    </div>
    <div v-else class="text-xs-right">
      <v-btn flat color="error" @click="removeSelected" :disabled="selected.length === 0">
        <v-icon left>remove_circle_outline</v-icon>{{ $tc('removeSubgroups', selected.length, [selected.length]) }}
      </v-btn>
      <v-btn flat color="success" @click="addExisting"><v-icon left>add_circle_outline</v-icon>{{ $t('addSubgroups') }}</v-btn>
    </div>
  </div>
</template>

<script>
import ComponentSearchTable from '@/components/ComponentSearchTable'
import { mapMutations } from 'vuex'

export default {
  name: 'GroupModuleGroupList',
  props: ['tabIndex', 'groupId', 'groups'],
  components: {
    ComponentSearchTable
  },
  data () {
    return {
      selected: []
    }
  },
  computed: {
    headers () {
      return [
        { text: this.$t('id'), value: 'id' },
        { text: this.$t('name'), value: 'name' },
        { text: this.$t('description'), value: 'description' },
        { sortable: false }
      ]
    }
  },
  watch: {
    groups () {
      this.selected = []
    }
  },
  methods: {
    ...mapMutations('groups', ['setActiveTab', 'setTab', 'setDialog']),
    loadGroup (id) {
      this.$store.dispatch('groups/loadGroup', { id, tabIndex: this.tabIndex + 1, switchTab: true })
    },
    newGroup () {
      this.setTab({ index: 1, item: { tabType: 'group' } })
      this.setActiveTab(1)
    },
    deleteSelected () {
      this.setDialog({ show: true, info: { action: 'delete', type: 'group', selected: this.selected } })
    },
    removeSelected () {
      this.setDialog({ show: true, info: { action: 'remove', type: 'group', selected: this.selected, tabIndex: this.tabIndex } })
    },
    addExisting () {
      this.setDialog({ show: true, info: { action: 'add', type: 'group', selected: this.selected, tabIndex: this.tabIndex } })
    }
  }
}
</script>

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

.next-arrow {
  margin-left: 20px;
  margin-right: -10px;
}
</style>
