(function(){
  const modal = document.getElementById('confirmModal');
  const confirmText = document.getElementById('confirmText');
  const deleteForm = document.getElementById('deleteForm');
  const deleteId = document.getElementById('deleteId');
  const cancelBtn = document.getElementById('cancelBtn');

  if (!modal) return;

  function openModal(id, name, action) {
    confirmText.textContent = "Are you sure you want to delete " + name + "?";
    deleteId.value = id;
    deleteForm.action = action;
    modal.classList.remove('hidden');
  }

  document.querySelectorAll('.js-delete').forEach(btn=>{
    btn.addEventListener('click', ()=>{
      openModal(btn.dataset.id, btn.dataset.name, 'student_delete_handler.php');
    });
  });

  document.querySelectorAll('.js-delete-admin').forEach(btn=>{
    btn.addEventListener('click', ()=>{
      openModal(btn.dataset.id, btn.dataset.name, 'admin_delete_handler.php');
    });
  });

  cancelBtn.addEventListener('click', ()=>{
    modal.classList.add('hidden');
  });
})();
