<template>
<b-modal id="confirm-delete-invited-modal" title="Confirm action" ok-title="Delete" @ok="deleteInvitedRegistration" ok-variant="danger">
  <p v-if="invitedRegistration">Are you sure that you want to remove the invited registration for {{invitedRegistration.email}}?</p>
</b-modal>
</template>

<script>
import client from 'api-client';

export default {
  name: 'ConfirmDeleteInvitedModal',
  data: function() {
    return {
      invitedRegistration: null,
      groupId: null
    }
  },
  methods: {
    openConfirmDeleteInvitedModal(invitedRegistration, groupId) {
      this.invitedRegistration = invitedRegistration;
      this.groupId = groupId;
      this.$bvModal.show('confirm-delete-invited-modal');
    },
    deleteInvitedRegistration() {
      let regId = this.invitedRegistration.id;
      client.deleteInvitedRegistration(regId, this.groupId)
        .then(() => {
          let model = this.$store.state.model;
          this.$store.commit('removeInvitedRegistration', regId);
          if (model.invitedRegistrations.length === 0) {
            this.$store.commit('setTabIndex', model.leaf ? 1 : 0);
          }
        });
    }
  }
}
</script>
