document.addEventListener('DOMContentLoaded', function () { const sosButton = document.getElementById('sosButton'); const sosToastEl = document.getElementById('sosToast'); const sosToast = new bootstrap.Toast(sosToastEl); const toastBody = sosToastEl.querySelector('.toast-body'); const toastHeader = sosToastEl.querySelector('.toast-header .me-auto'); if (sosButton) { sosButton.addEventListener('click', function () { // Disable button to prevent multiple clicks sosButton.disabled = true; sosButton.querySelector('.sos-text').textContent = 'Sending...'; fetch('sos.php', { method: 'POST', headers: { 'Content-Type': 'application/json' } }) .then(response => response.json()) .then(data => { if (data.success) { toastHeader.classList.remove('text-danger'); toastHeader.classList.add('text-success'); toastHeader.innerHTML = ' SOS Sent'; toastBody.textContent = data.message; } else { toastHeader.classList.remove('text-success'); toastHeader.classList.add('text-danger'); toastHeader.innerHTML = ' Error'; toastBody.textContent = data.message; } sosToast.show(); }) .catch(error => { toastHeader.classList.remove('text-success'); toastHeader.classList.add('text-danger'); toastHeader.innerHTML = ' Network Error'; toastBody.textContent = 'Could not connect to the server. Please check your connection.'; sosToast.show(); console.error('SOS fetch error:', error); }) .finally(() => { // Re-enable the button after a delay setTimeout(() => { sosButton.disabled = false; sosButton.querySelector('.sos-text').textContent = 'SOS'; }, 5000); // 5-second cooldown }); }); } });