<i18n>
{
  "en": {
    "id": "ID",
    "name": "Name",
    "ip": "IP Address",
    "mac": "MAC Address",
    "uuid": "UUID",
    "wakeClients": "Wake one client up | Wake {0} clients up",
    "removeClients": "Remove one client | Remove {0} clients",
    "addClients": "Add clients",
    "deleteClients": "Delete one client | Delete {0} clients",
    "createClient": "Create client"
  },
  "de": {
    "id": "ID",
    "name": "Name",
    "ip": "IP Adresse",
    "mac": "MAC Adresse",
    "uuid": "UUID",
    "wakeClients": "Einen Client aufwecken | {0} Clients aufwecken",
    "removeClients": "Einen Client entfernen | {0} Clients entfernen",
    "addClients": "Clients hinzufügen",
    "deleteClients": "Einen Client löschen | {0} Clients löschen",
    "createClient": "Client erstellen"
  }
}
</i18n>

<template>
  <div>
    <v-divider></v-divider>
    <data-table ref="table" v-model="selected" :headers="headers" :items="clients" :loading="loading" @dblclick="loadClient($event)" min-width="1300px" copy-button>
      <div slot="actions" slot-scope="row" style="text-align: right">
        <v-btn icon @click.stop @mousedown="loadClient(row.item)" style="margin: 0"><v-icon>keyboard_arrow_right</v-icon></v-btn>
      </div>
    </data-table>
    <v-divider></v-divider>
    <div class="text-right">
      <v-btn text color="primary" @click="wake" :disabled="selected.length === 0">
        <v-icon left>notifications_active</v-icon>{{ $tc('wakeClients', selected.length, [selected.length]) }}
      </v-btn>
      <template v-if="tabIndex === 0">
        <v-btn text color="error" @click="deleteSelected" :disabled="selected.length === 0">
          <v-icon left>delete</v-icon>{{ $tc('deleteClients', selected.length, [selected.length]) }}
        </v-btn>
        <v-btn text color="success" @click="newClient"><v-icon left>create</v-icon>{{ $t('createClient') }}</v-btn>
      </template>
      <template v-else>
        <v-btn text color="error" @click="removeSelected" :disabled="selected.length === 0">
          <v-icon left>remove_circle_outline</v-icon>{{ $tc('removeClients', selected.length, [selected.length]) }}
        </v-btn>
        <v-btn text color="success" @click="addExisting"><v-icon left>add_circle_outline</v-icon>{{ $t('addClients') }}</v-btn>
      </template>
    </div>
  </div>
</template>

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

export default {
  name: 'GroupModuleClientList',
  props: ['tabIndex', 'groupId', 'clients'],
  components: {
    DataTable
  },
  data () {
    return {
      selected: []
    }
  },
  computed: {
    headers () {
      return [
        { key: 'id', text: this.$t('id'), width: '50px', sortType: 'number' },
        { key: 'name', text: this.$t('name') },
        { key: 'ip', text: this.$t('ip'), width: '120px', sortType: 'ipv4' },
        { key: 'mac', text: this.$t('mac'), width: '160px' },
        { key: 'uuid', text: this.$t('uuid'), width: '300px' },
        { 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']),
    loadClient (item) {
      this.$store.dispatch('groups/loadClient', { id: item.id, name: item.name, tabIndex: this.tabIndex + 1, switchTab: true })
    },
    newClient () {
      this.setTab({ index: 1, item: { id: 'create', tabType: 'client' } })
      this.setActiveTab(1)
    },
    deleteSelected () {
      this.setDialog({ show: true, info: { action: 'delete', type: 'client', selected: this.selected } })
    },
    removeSelected () {
      this.setDialog({ show: true, info: { action: 'remove', type: 'client', selected: this.selected, tabIndex: this.tabIndex } })
    },
    addExisting () {
      this.setDialog({ show: true, info: { action: 'add', type: 'client', selected: this.selected, tabIndex: this.tabIndex } })
    },
    wake () {
      this.setDialog({ show: true, info: { action: 'wake', type: 'client', 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>
