<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": "Eine Untergruppe entfernen | {0} Untergruppen entfernen",
    "addSubgroups": "Füge Untergruppen hinzu",
    "deleteGroups": "Eine Gruppe löschen | {0} Gruppen löschen",
    "createGroup": "Gruppe erstellen"
  }
}
</i18n>

<template>
  <div>
    <v-divider></v-divider>
    <data-table ref="table" v-model="selected" :headers="headers" :items="groups" :loading="loading" @dblclick="loadGroup($event)" copy-button>
      <div slot="actions" slot-scope="row" style="text-align: right">
        <v-btn icon @click.stop @mousedown="loadGroup(row.item)" style="margin: 0"><v-icon>keyboard_arrow_right</v-icon></v-btn>
      </div>
    </data-table>
    <v-divider></v-divider>
    <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 DataTable from '@/components/DataTable'
import { mapMutations } from 'vuex'

export default {
  name: 'GroupModuleGroupList',
  props: ['tabIndex', 'groupId', 'groups'],
  components: {
    DataTable
  },
  data () {
    return {
      selected: []
    }
  },
  computed: {
    headers () {
      return [
        { key: 'id', text: this.$t('id'), width: '50px', sortType: 'number' },
        { key: 'name', text: this.$t('name'), width: '200px' },
        { key: 'description', text: this.$t('description') },
        { key: 'actions', width: '60px' }
      ]
    },
    loading () {
      return this.$store.state.groups.tabChain[this.tabIndex].loading
    }
  },
  watch: {
    groupId () {
      if (this.$refs.table) this.$refs.table.resetData()
    }
  },
  methods: {
    ...mapMutations('groups', ['setActiveTab', 'setTab', 'setDialog']),
    loadGroup (item) {
      this.$store.dispatch('groups/loadGroup', { id: item.id, name: item.name, tabIndex: this.tabIndex + 1, switchTab: true, autoGoto: true })
    },
    newGroup () {
      this.setTab({ index: 1, item: { id: 'create', 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>
.next-arrow {
  margin-left: 20px;
  margin-right: -10px;
}
</style>
