<i18n>
{
  "en": {
    "id": "ID",
    "name": "Name",
    "ip": "IP Address",
    "mac": "MAC Address",
    "uuid": "UUID",
    "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",
    "removeClients": "Entferne einen Clienten | Entferne {0} Clienten",
    "addClients": "Füge Clienten hinzu",
    "deleteClients": "Lösche einen Clienten | Lösche {0} Clienten",
    "createClient": "Client erstellen"
  }
}
</i18n>

<template>
  <div>
    <v-card>
      <component-search-table v-model="selected" :headers="headers" :items="clients" select-all :loading="loading">
        <template slot="items" slot-scope="row">
          <tr :style="row.color" @click="row.data.selected = !row.data.selected" @dblclick="loadClient(row.data.item)">
            <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.ip }}</td>
            <td>{{ row.data.item.mac }}</td>
            <td>{{ row.data.item.uuid }}</td>
            <td class="narrow-td">
              <v-btn class="next-arrow" icon @click.stop="loadClient(row.data.item)"><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('deleteClients', selected.length, [selected.length]) }}
      </v-btn>
      <v-btn flat color="success" @click="newClient"><v-icon left>create</v-icon>{{ $t('createClient') }}</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('removeClients', selected.length, [selected.length]) }}
        </v-btn>
        <v-btn flat color="success" @click="addExisting"><v-icon left>add_circle_outline</v-icon>{{ $t('addClients') }}</v-btn>
      </div>
  </div>
</template>

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

export default {
  name: 'GroupModuleClientList',
  props: ['tabIndex', 'groupId', 'clients'],
  components: {
    ComponentSearchTable
  },
  data () {
    return {
      selected: []
    }
  },
  computed: {
    headers () {
      return [
        { text: this.$t('id'), value: 'id' },
        { text: this.$t('name'), value: 'name' },
        { text: this.$t('ip'), value: 'ip' },
        { text: this.$t('mac'), value: 'mac' },
        { text: this.$t('uuid'), value: 'uuid' },
        { sortable: false }
      ]
    },
    loading () {
      return this.$store.state.groups.tabChain[this.tabIndex].loading
    }
  },
  watch: {
    clients () {
      this.selected = []
    }
  },
  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 } })
    }
  }
}
</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>
