<i18n>
{
  "en": {
    "grant-role": "Grant Role",
    "revoke-role": "Revoke Role",
    "username": "Username",
    "name": "Name",
    "roles": "Roles"
  },
  "de": {
    "grant-role": "Rolle zuweisen",
    "revoke-role": "Rolle entziehen",
    "username": "Nutzername",
    "name": "Name",
    "roles": "Rollen"
  }
}
</i18n>

<template>
  <div>
    <v-card>
      <component-search-table :value="selectedUsers"  @input="$store.commit('permissions/setSelectedUsers', $event)" :headers="headers" :items="users" select-all>
        <template slot="items" slot-scope="props">
          <tr :style="props.color" @click="props.data.selected = !props.data.selected">
            <td>
              <v-checkbox
                color="primary"
                v-model="props.data.selected"
                hide-details
              ></v-checkbox>
            </td>
            <td>{{ props.data.item.username }}</td>
            <td>{{ props.data.item.name }}</td>
            <td>
              <template v-for="role in props.data.item.roles">
                <v-tooltip bottom :key="role.id">
                  <v-chip small slot="activator">{{ role.name }}</v-chip>
                  <span>{{ role.descr }}</span>
                </v-tooltip>
              </template>
            </td>
          </tr>
        </template>
      </component-search-table>
    </v-card>
    <div class="text-xs-right">
      <v-btn color="error" flat @click="$store.commit('permissions/grantRevoke', { show: true, grant: false } )" :disabled="selectedUsers.length === 0">
        <v-icon left>remove_circle_outline</v-icon>{{ $t('revoke-role') }}
      </v-btn>
      <v-btn color="success" flat @click="$store.commit('permissions/grantRevoke', { show: true, grant: true } )" :disabled="selectedUsers.length === 0">
        <v-icon left>add_circle_outline</v-icon>{{ $t('grant-role') }}
      </v-btn>
    </div>
  </div>
</template>

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

export default {
  name: 'PermissionModuleUserList',
  components: {
    ComponentSearchTable
  },
  data () {
    return {
      headers: [
        { text: this.$t('username'), value: 'username' },
        { text: this.$t('name'), value: 'name' },
        { text: this.$t('roles'), value: 'id' }
      ]
    }
  },
  computed: {
    ...mapState('permissions', ['selectedUsers', 'users'])
  },
  methods: {
  },
  created () {
    this.$store.dispatch('permissions/loadUserData')
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
