<template>
<b-modal id="add-permission-modal" title="Add permission" ok-title="Add" @ok="addPermission">
  <SearchUser ref="searchUser" />
</b-modal>
</template>

<script>
import client from 'api-client';
import SearchUser from './SearchUser.vue'

export default {
  name: 'AddPermissionModal',
  components: {
    SearchUser
  },
  methods: {
    addPermission: function(event) {
      // Prevent modal from closing
      event.preventDefault();

      let userId = this.$refs.searchUser.selectedUser;
      let permission = this.$refs.searchUser.permission;

      client.addPermission(userId, permission, this.$store.state.input)
        .then(res => {
          this.$store.commit('updatePermissionsPanel', res);
          this.$bvModal.hide('add-permission-modal');
        });
    }
  }
};
</script>
