52 lines
2.3 KiB
JavaScript
52 lines
2.3 KiB
JavaScript
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 = '<i class="bi bi-check-circle-fill"></i> SOS Sent';
|
|
toastBody.textContent = data.message;
|
|
} else {
|
|
toastHeader.classList.remove('text-success');
|
|
toastHeader.classList.add('text-danger');
|
|
toastHeader.innerHTML = '<i class="bi bi-exclamation-triangle-fill"></i> Error';
|
|
toastBody.textContent = data.message;
|
|
}
|
|
sosToast.show();
|
|
})
|
|
.catch(error => {
|
|
toastHeader.classList.remove('text-success');
|
|
toastHeader.classList.add('text-danger');
|
|
toastHeader.innerHTML = '<i class="bi bi-exclamation-triangle-fill"></i> 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
|
|
});
|
|
});
|
|
}
|
|
}); |