${st.a?"Calculé sur "+st.a+" réponse"+(st.a>1?"s":""):"Chacun doit cocher ses dates"}
Budget max commun
${st.a?st.bud+" €":"—"}
par pers., transport inclus
Ont répondu
${st.a}/${st.n}
France ${st.pf?.fr||0} · Étranger ${st.pf?.et||0} · Égal ${st.pf?.egal||0}
La bande
${Object.values(D.participants).map(p=>`
${esc(p.nom[0]?.toUpperCase())}${esc(p.nom)}
`).join("")}
${db?"Envoie le lien de cette page aux potes : dispos et votes se synchronisent en direct.":"Le lien contient le séjour. Chaque pote l’ouvre, ajoute ses dispos et te renvoie son lien."}
`:``}`;
app.querySelectorAll("[data-f]").forEach(b=>b.onclick=()=>{flt=b.dataset.f;render()});
app.querySelectorAll("[data-v]").forEach(b=>b.onclick=()=>{const s=new Set(myv);s.has(b.dataset.v)?s.delete(b.dataset.v):s.add(b.dataset.v);put("votes",me,{ids:[...s]})});
app.querySelectorAll("[data-rm]").forEach(b=>b.onclick=()=>put("spots",b.dataset.rm,{deleted:1}));
app.querySelectorAll("[data-ai]").forEach(b=>b.onclick=()=>suggest(b.dataset.ai));
if($("#add"))$("#add").onclick=()=>{form=true;render()};
if($("#fa"))$("#fa").onclick=()=>{const n=$("#fn").value.trim();if(!n)return;form=false;put("spots","u"+Date.now(),{kind:$("#fk").value,name:n,place:$("#fp").value.trim(),time:"",tags:[],pp:+$("#fc").value||0,q:$("#fp").value.trim()||n})}}
app.querySelectorAll("[data-go]").forEach(b=>b.onclick=()=>{tab=b.dataset.go;render()});if($("#sh"))$("#sh").onclick=share;
}
function saveLocal(){try{localStorage.setItem("pt",JSON.stringify({me,D}))}catch(e){}}
function loadLocal(){try{let s={};try{s=JSON.parse(localStorage.getItem("pt")||"{}")}catch(e){}
if(s.me)me=s.me;const m=location.hash.match(/^#s=(.+)$/);
if(m){const j=JSON.parse(decodeURIComponent(escape(atob(m[1]))));Object.keys(D).forEach(c=>Object.assign(D[c],j[c]||{}))}
Object.keys(D).forEach(c=>{const o=s.D?.[c]||{};Object.keys(o).forEach(k=>{if(!(k in D[c])||k===me||c==='spots')D[c][k]=o[k]})});saveLocal()}catch(e){}render()}
async function share(){if(db){$("#shm").textContent="Envoie le lien de cette page aux potes : dispos et votes se synchronisent en direct.";return}
const u=location.href.split("#")[0]+"#s="+btoa(unescape(encodeURIComponent(JSON.stringify(D))));
try{if(navigator.share){await navigator.share({title:"PotesTrip",text:"Rejoins notre séjour !",url:u});return}}catch(e){return}
try{await navigator.clipboard.writeText(u);$("#shm").textContent="Lien copié. Colle-le dans le groupe WhatsApp."}catch(e){prompt("Copie ce lien :",u)}}
async function suggest(kind){
if(!sample){msg="Suggestions IA indisponibles ici : ajoute des spots à la main.";render();return}
busy=kind;msg="";render();const st=stats(),all=Object.values(D.participants);
const p=`Un groupe de ${Math.max(st.n,2)} amis habite à Toulouse et veut un séjour d’environ 4 nuits. Fenêtre de dates : ${st.ok?st.s+" au "+st.e:"été 2027"}. Budget max par personne, transport inclus : ${st.a?st.bud:300} €. Envies : ${[...new Set(all.flatMap(x=>x.envies||[]))].join(", ")||"baignade, isolement"}. ${kind==="fr"?"Propose 4 lieux en France à moins de 2 h de Toulouse, isolés, avec lac, ruisseau ou piscine pour se baigner.":"Propose 4 destinations à l’étranger accessibles depuis Toulouse."} Réponds uniquement par un tableau JSON d’objets {name, place, time (durée depuis Toulouse), tags (3 mots-clés), desc (1 phrase), pp (estimation entière en € par personne), q (requête de recherche Airbnb)}. Ne reprends pas : ${spots().map(x=>x[1].name).join(", ")}.`;
try{const r=await sample.json(p);for(const x of r.slice(0,4))await put("spots","ai"+Date.now()+Math.random().toString(36).slice(2,5),{kind,name:String(x.name),place:String(x.place),time:String(x.time||""),tags:(x.tags||[]).slice(0,3).map(String),desc:String(x.desc||""),pp:+x.pp||0,q:String(x.q||x.place)})}catch(e){msg="L’IA n’a pas répondu. Réessaie dans un instant."}
busy="";render()}
document.querySelectorAll("nav button").forEach(b=>b.onclick=()=>{tab=b.dataset.t;msg="";render()});
me="local-"+Math.random().toString(36).slice(2,8);render();
(async()=>{try{
const u=await claude.use("user"),d=await claude.use("db");sample=await claude.use("sample");
if(!u||!d){loadLocal();return}me=await u.id();db=d;
for(const c of Object.keys(D))d.collection(c).onSnapshot(s=>{D[c]=Object.fromEntries(s.docs.map(x=>[x.id,x.data()]));render()},()=>{});
}catch(e){loadLocal()}})();