Online Smart Business Contact Manager

Business Contact Manager

Company: ${contact.company}

`; contactList.appendChild(card); }); }; // --- Modal Logic --- const openModal = (mode = 'add', id = null) => { contactForm.reset(); contactIdInput.value = ''; if (mode === 'add') { modalTitle.textContent = 'Add New Contact'; } else { modalTitle.textContent = 'Edit Contact'; const contact = contacts.find(c => c.id === id); if (contact) { contactIdInput.value = contact.id; document.getElementById('name').value = contact.name; document.getElementById('email').value = contact.email; document.getElementById('phone').value = contact.phone; document.getElementById('company').value = contact.company; } } contactModal.style.display = 'flex'; }; const closeModal = () => { contactModal.style.display = 'none'; }; // --- CRUD Logic --- const saveContact = (e) => { e.preventDefault(); const id = parseInt(contactIdInput.value); const contactData = { name: document.getElementById('name').value, email: document.getElementById('email').value, phone: document.getElementById('phone').value, company: document.getElementById('company').value, }; if (id) { // Update contacts = contacts.map(c => c.id === id ? { ...c, ...contactData } : c); } else { // Add contactData.id = contacts.length > 0 ? Math.max(...contacts.map(c => c.id)) + 1 : 1; contacts.push(contactData); } renderContacts(searchBar.value); closeModal(); }; const deleteContact = (id) => { if (confirm('Are you sure you want to delete this contact?')) { contacts = contacts.filter(c => c.id !== id); renderContacts(searchBar.value); } }; // --- PDF Generation --- const generatePDF = () => { const { jsPDF } = window.jspdf; const doc = new jsPDF({ unit: 'pt', format: 'a4' }); const pageWidth = doc.internal.pageSize.getWidth(); const margin = 40; let cursorY = margin; doc.setFont('helvetica', 'bold'); doc.setFontSize(20); doc.text('Business Contacts Report', pageWidth / 2, cursorY, { align: 'center' }); cursorY += 30; contacts.forEach(contact => { if (cursorY > doc.internal.pageSize.getHeight() - margin) { doc.addPage(); cursorY = margin; } doc.setFont('helvetica', 'bold'); doc.setFontSize(12); doc.text(contact.name, margin, cursorY); cursorY += 15; doc.setFont('helvetica', 'normal'); doc.setFontSize(10); doc.text(`Email: ${contact.email}`, margin, cursorY); cursorY += 12; doc.text(`Phone: ${contact.phone}`, margin, cursorY); cursorY += 12; doc.text(`Company: ${contact.company}`, margin, cursorY); cursorY += 25; }); doc.save('Business-Contacts.pdf'); }; // --- Event Listeners --- addContactBtn.addEventListener('click', () => openModal('add')); cancelBtn.addEventListener('click', closeModal); contactForm.addEventListener('submit', saveContact); searchBar.addEventListener('input', (e) => renderContacts(e.target.value)); pdfBtn.addEventListener('click', generatePDF); contactList.addEventListener('click', (e) => { if (e.target.classList.contains('edit-btn')) { openModal('edit', parseInt(e.target.dataset.id)); } if (e.target.classList.contains('delete-btn')) { deleteContact(parseInt(e.target.dataset.id)); } }); // Initial Render renderContacts(); });
Scroll to Top