document.addEventListener('DOMContentLoaded', () => { const callForm = document.getElementById('callForm'); const callHistory = document.getElementById('callHistory'); let callTimeout; // Form validation function validatePhoneNumber(number) { return /^\+?[\d\s\-\(\)]{7, } // Initialize with some sample calls if empty if (callHistory.children.length === 1) { setTimeout(() => { simulateCall(Date.now()-5000, '+1 (555) 123-4567', '+1 (555) 987-6543'); simulateCall(Date.now()-10000, '+44 7911 123456', '+49 176 9876543'); }, 1000); } $/.test(number); } callForm.addEventListener('submit', (e) => { e.preventDefault(); const callerId = document.getElementById('callerId').value.trim(); const recipientNumber = document.getElementById('recipientNumber').value.trim(); // Clear any previous timeouts if (callTimeout) clearTimeout(callTimeout); if (!callerId || !recipientNumber) { showAlert('Please enter both caller ID and recipient number', 'error'); return; } if (!validatePhoneNumber(callerId) || !validatePhoneNumber(recipientNumber)) { showAlert('Please enter valid phone numbers', 'error'); return; } // Simulate call initiation const callId = Date.now(); // Unique ID for each call simulateCall(callId, callerId, recipientNumber); }); // Add service worker for PWA functionality if ('serviceWorker' in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/sw.js').then(registration => { console.log('ServiceWorker registration successful'); }).catch(err => { console.log('ServiceWorker registration failed: ', err); }); }); } function showAlert(message, type = 'success') { const alert = document.createElement('div'); alert.className = `fixed top-4 right-4 px-4 py-3 rounded-lg shadow-lg z-50 ${ type === 'error' ? 'bg-red-500' : 'bg-green-500' } text-white`; alert.textContent = message; document.body.appendChild(alert); setTimeout(() => { alert.remove(); }, 3000); } function simulateCall(callId, callerId, recipientNumber) { // Add to call history with unique ID const callItem = document.createElement('div'); callItem.dataset.callId = callId; callItem.className = 'bg-gray-800 p-4 rounded-lg flex justify-between items-center'; callItem.innerHTML = `
${callerId} ${recipientNumber}
Calling...
`; callHistory.prepend(callItem); feather.replace(); // Add event listeners to buttons callItem.querySelector('.end-call-btn').addEventListener('click', () => { endCall(callId, callItem); }); callItem.querySelector('.delete-call-btn').addEventListener('click', () => { callItem.remove(); showAlert('Call removed from history', 'info'); }); // Simulate call completion after random time (3-10 seconds) const callDuration = 3000 + Math.random() * 7000; callTimeout = setTimeout(() => { if (callItem.parentNode) { // Only if still in DOM callItem.querySelector('.text-green-400').textContent = 'Completed'; callItem.querySelector('.end-call-btn').remove(); feather.replace(); showAlert(`Call to ${recipientNumber} completed`); } }, callDuration); } function endCall(callId, callItem) { clearTimeout(callTimeout); callItem.querySelector('.text-green-400').textContent = 'Ended'; callItem.querySelector('.end-call-btn').remove(); feather.replace(); showAlert('Call ended manually', 'info'); } });