function showSendModal() {
document.getElementById('notificationModal').style.display = 'flex';
}
function closeModal() {
document.getElementById('notificationModal').style.display = 'none';
}
function sendNotification() {
const form = document.getElementById('notificationForm');
const formData = new FormData(form);
const btn = document.querySelector('#notificationModal .btn-primary');
btn.disabled = true;
btn.innerHTML = ' Sending...';
fetch('/admin/notifications/send', {
method: 'POST',
headers: {
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]')?.content || '',
},
body: new URLSearchParams(formData),
})
.then(r => r.json())
.then(data => {
if (data.success) {
showToast('success', data.message);
setTimeout(() => location.reload(), 1000);
} else {
showToast('error', data.message);
}
closeModal();
btn.disabled = false;
btn.innerHTML = ' Send';
})
.catch(() => {
showToast('error', 'Failed to send notification');
btn.disabled = false;
btn.innerHTML = ' Send';
});
}
function confirmDelete(id, title) {
if (!confirm('Delete notification "' + title + '"? This action cannot be undone.')) return;
const csrf = document.querySelector('meta[name="csrf-token"]')?.content || '';
fetch('/admin/notifications/' + id + '/delete', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-CSRF-Token': csrf,
},
body: '_csrf_token=' + encodeURIComponent(csrf),
})
.then(r => r.json())
.then(data => {
showToast(data.success ? 'success' : 'error', data.message);
if (data.success) location.reload();
})
.catch(() => showToast('error', 'Failed to delete notification'));
}