// Fire Alarm Management App - Common JavaScript // Format currency function formatCurrency(value) { return '$' + (value || 0).toLocaleString('en-US', { minimumFractionDigits: 0, maximumFractionDigits: 0 }); } // Format date function formatDate(dateStr) { if (!dateStr) return '-'; const date = new Date(dateStr); return date.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' }); } // Get progress bar class based on completion percentage function getProgressClass(completion) { if (completion >= 100) return 'bg-success'; if (completion > 50) return 'bg-info'; if (completion > 0) return 'bg-warning'; return 'bg-secondary'; } // Show toast notification function showToast(message, type = 'success') { const toastContainer = document.getElementById('toastContainer') || createToastContainer(); const toast = document.createElement('div'); toast.className = `toast align-items-center text-white bg-${type} border-0`; toast.setAttribute('role', 'alert'); toast.innerHTML = `
${message}
`; toastContainer.appendChild(toast); const bsToast = new bootstrap.Toast(toast); bsToast.show(); toast.addEventListener('hidden.bs.toast', () => toast.remove()); } function createToastContainer() { const container = document.createElement('div'); container.id = 'toastContainer'; container.className = 'toast-container position-fixed bottom-0 end-0 p-3'; document.body.appendChild(container); return container; } // Confirm dialog function confirmAction(message) { return new Promise((resolve) => { resolve(confirm(message)); }); } // API helper functions async function apiGet(url) { const response = await fetch(url); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return response.json(); } async function apiPost(url, data) { const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return response.json(); } async function apiPut(url, data) { const response = await fetch(url, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return response.json(); } async function apiDelete(url) { const response = await fetch(url, { method: 'DELETE' }); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return true; } // Debounce function for search inputs function debounce(func, wait) { let timeout; return function executedFunction(...args) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; }