window.copyContact=id=>{const l=allLeads.find(x=>x.id===id);if(!l)return;const t=(l.contact&&l.contact.name||'?')+'\n📞 '+(l.contact&&l.contact.phone||'?')+'\n✉️ '+(l.contact&&l.contact.email||'(pas d\'email)')+'\n\nDemande : '+(l.raw||'')+'\nVille : '+(l.ville||'?')+'\nUrgence : '+(l.urgence||'normal')+'\nSource : '+(l.site||'?')+'\n\nRéf : '+id.substring(0,10).toUpperCase();navigator.clipboard.writeText(t).then(()=>alert('📋 Copié'),()=>alert('Erreur copie'));}; document.querySelectorAll('.tab-btn').forEach(b=>{b.onclick=()=>{document.querySelectorAll('.tab-btn').forEach(x=>x.classList.remove('active'));document.querySelectorAll('.tab-content').forEach(x=>x.classList.remove('active'));b.classList.add('active');document.querySelector('[data-content="'+b.getAttribute('data-tab')+'"]').classList.add('active');};}); document.getElementById('profileForm').onsubmit=async e=>{ e.preventDefault(); const sb=document.getElementById('saveProfileBtn');const st=document.getElementById('saveProfileStatus'); sb.disabled=true;sb.textContent='Enregistrement...';st.textContent=''; const zones=Array.from(document.querySelectorAll('#zonesGrid input:checked')).map(i=>i.value); const cats=Array.from(document.querySelectorAll('#categoriesGrid input:checked')).map(i=>i.value); const data={proName:document.getElementById('proName').value.trim(),ridet:document.getElementById('proRidet').value.trim(),phone:document.getElementById('proPhone').value.trim(),address:document.getElementById('proAddress').value.trim(),description:document.getElementById('proDescription').value.trim(),zones,categories:cats,updatedAt:firebase.firestore.FieldValue.serverTimestamp()}; try{await firebase.firestore().collection('users').doc(currentUser.uid).set(data,{merge:true});userProfile=Object.assign({},userProfile,data);st.textContent='✅ Enregistré';setTimeout(()=>{st.textContent='';},3000);}catch(err){st.textContent='❌ '+err.message;st.style.color='var(--danger)';} sb.disabled=false;sb.textContent='💾 Enregistrer ma fiche'; }; document.getElementById('saveSettingsBtn').onclick=async()=>{ const st=document.getElementById('saveSettingsStatus'); try{await firebase.firestore().collection('users').doc(currentUser.uid).set({notifEmail:document.getElementById('notifEmail').checked,notifSMS:document.getElementById('notifSMS').checked,notifPush:document.getElementById('notifPush').checked},{merge:true});st.textContent='✅ Enregistré';setTimeout(()=>{st.textContent='';},3000);}catch(err){st.textContent='❌ '+err.message;} }; document.getElementById('logoutBtn').onclick=async()=>{if(leadsListener)leadsListener();await firebase.auth().signOut();window.location.replace('/');}; document.getElementById('deleteAccountBtn').onclick=async()=>{ if(!confirm('Êtes-vous sûr ? Cette action est irréversible.'))return; if(!confirm('Dernière confirmation : votre fiche et votre compte seront supprimés.'))return; try{await firebase.firestore().collection('users').doc(currentUser.uid).delete();await currentUser.delete();window.location.replace('/');}catch(err){alert('Erreur : '+err.message+'. Reconnectez-vous avant.');} };