👥 Carnet d’adresses
0
0
// ========================================
// 🔥 GESTION DES CONTACTS
// ========================================
function getContacts() {
return JSON.parse(localStorage.getItem(‘montemix_contacts’) || ‘[]’);
}
function saveContacts(contacts) {
localStorage.setItem(‘montemix_contacts’, JSON.stringify(contacts));
}
// Ajouter un contact
document.getElementById(‘addContactForm’).addEventListener(‘submit’, function(e) {
e.preventDefault();
const contact = {
id: Date.now(),
name: document.getElementById(‘newContactName’).value.trim(),
company: document.getElementById(‘newContactCompany’).value.trim(),
phone: document.getElementById(‘newContactPhone’).value.trim(),
email: document.getElementById(‘newContactEmail’).value.trim(),
priority: document.querySelector(‘input[name= »priority »]:checked’).value,
notes: document.getElementById(‘newContactNotes’).value.trim(),
status: ‘toCall’, // ou ‘called’
timestamp: Date.now()
};
let contacts = getContacts();
contacts.push(contact);
saveContacts(contacts);
this.reset();
document.getElementById(‘priority-medium’).checked = true;
displayContactsLists();
alert(‘✅ Contact ajouté avec succès !’);
});
// Afficher les listes
function displayContactsLists() {
const contacts = getContacts();
const toCall = contacts.filter(c => c.status === ‘toCall’);
const called = contacts.filter(c => c.status === ‘called’);
document.getElementById(‘toCallCount’).textContent = toCall.length;
document.getElementById(‘calledCount’).textContent = called.length;
displayContactList(‘toCallList’, toCall, ‘toCall’);
displayContactList(‘calledList’, called, ‘called’);
updateSelectionCounts();
}
function displayContactList(containerId, contacts, listType) {
const container = document.getElementById(containerId);
if (contacts.length === 0) {
container.innerHTML = ‘
Aucun contact
‘;
return;
}
const priorityLabels = {
‘high’: ‘🔴 Haute’,
‘medium’: ‘🟡 Moyenne’,
‘low’: ‘🟢 Basse’
};
container.innerHTML = contacts.map(contact => `
${contact.name}
${contact.company ? `
` : »}
${contact.email ? `
` : »}
${priorityLabels[contact.priority]}
${contact.notes ? `
` : »}
`).join( »);
}
// Déplacer un contact
function moveContact(id, newStatus) {
let contacts = getContacts();
contacts = contacts.map(c => c.id === id ? { …c, status: newStatus } : c);
saveContacts(contacts);
displayContactsLists();
}
// Supprimer un contact
function deleteContact(id) {
if (!confirm(‘Supprimer ce contact ?’)) return;
let contacts = getContacts();
contacts = contacts.filter(c => c.id !== id);
saveContacts(contacts);
displayContactsLists();
}
// Sélection multiple
function selectAllToCall() {
document.querySelectorAll(‘.toCall-checkbox’).forEach(cb => cb.checked = true);
updateSelectionCounts();
}
function selectAllCalled() {
document.querySelectorAll(‘.called-checkbox’).forEach(cb => cb.checked = true);
updateSelectionCounts();
}
function updateSelectionCounts() {
const toCallSelected = document.querySelectorAll(‘.toCall-checkbox:checked’).length;
const calledSelected = document.querySelectorAll(‘.called-checkbox:checked’).length;
document.getElementById(‘toCallSelection’).textContent = `${toCallSelected} sélectionné(s)`;
document.getElementById(‘calledSelection’).textContent = `${calledSelected} sélectionné(s)`;
}
// Export VCard
function exportSelectedToCall() {
exportVCard(‘.toCall-checkbox:checked’);
}
function exportSelectedCalled() {
exportVCard(‘.called-checkbox:checked’);
}
function exportVCard(selector) {
const checkedBoxes = document.querySelectorAll(selector);
if (checkedBoxes.length === 0) {
alert(‘⚠️ Aucun contact sélectionné’);
return;
}
const selectedIds = Array.from(checkedBoxes).map(cb => parseInt(cb.dataset.id));
const contacts = getContacts().filter(c => selectedIds.includes(c.id));
let vcardContent = »;
contacts.forEach(contact => {
vcardContent += ‘BEGIN:VCARD\n’;
vcardContent += ‘VERSION:3.0\n’;
vcardContent += `FN:${contact.name}\n`;
if (contact.company) {
vcardContent += `ORG:${contact.company}\n`;
}
vcardContent += `TEL;TYPE=CELL:${contact.phone}\n`;
if (contact.email) {
vcardContent += `EMAIL:${contact.email}\n`;
}
if (contact.notes) {
vcardContent += `NOTE:${contact.notes.replace(/\n/g, ‘\\n’)}\n`;
}
vcardContent += ‘END:VCARD\n\n’;
});
// Télécharger le fichier
const blob = new Blob([vcardContent], { type: ‘text/vcard;charset=utf-8’ });
const url = window.URL.createObjectURL(blob);
const a = document.createElement(‘a’);
a.href = url;
a.download = `contacts_montemix_${Date.now()}.vcf`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
alert(`✅ ${contacts.length} contact(s) exporté(s) en VCard !`);
}
// Initialiser l’affichage des contacts
displayContactsLists();