// Close any existing modal
var modal = document.querySelector('.modal');
if (modal) {
  var modalInstance = bootstrap.Modal.getInstance(modal);
  modalInstance.hide();
}

// Show success message
var flashMessage = document.createElement('div');
flashMessage.className = 'alert alert-success alert-dismissible fade show';
flashMessage.setAttribute('role', 'alert');
flashMessage.innerHTML = `
  Student was successfully created.
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
`;

// Add the flash message to the page
var flashContainer = document.querySelector('.flash-messages') || document.querySelector('main');
flashContainer.insertBefore(flashMessage, flashContainer.firstChild);

// Redirect to the students list
window.location.href = '<%= institution_students_path(@institution) %>';