// Personal invite page (/join/): the full bridge-page story, personalized // to one sponsor. Pulls the sponsor's live position via /api/public/member, // pins their ID on every CTA, and submits new IDs with sponsorId locked to // the page. Prospects who land on a bad/unregistered ID get routed to /start // (the team rotation) instead of a dead end. (function(){ const esc=s=>String(s??'').replace(/[&<>'"]/g,c=>({'&':'&','<':'<','>':'>',"'":''','"':'"'}[c])); const fmt=n=>Number(n||0).toLocaleString(undefined,{maximumFractionDigits:2}); const date=ts=>ts?new Date(ts*1000).toLocaleDateString(undefined,{year:'numeric',month:'short',day:'numeric'}):'—'; function pathId(){const m=location.pathname.match(/^\/join\/(\d+)$/);return m?m[1]:''} const id=pathId(); function setIds(idStr){ document.getElementById('invId').textContent='#'+idStr; document.getElementById('invIdJoin').textContent='#'+idStr; document.getElementById('invIdBtn').textContent='#'+idStr; document.querySelectorAll('.inv-id-inline').forEach(el=>el.textContent='#'+idStr); } function fallbackToRotation(msg){ const facts=document.getElementById('invFacts'); facts.innerHTML=`
${esc(msg)} Open the team rotation instead →
`; const jb=document.getElementById('joinBtn');jb.textContent='Open the Team Sponsor Page →';jb.href='/start';jb.removeAttribute('target'); } async function load(){ if(!id){fallbackToRotation('This invite link is missing its member ID.');return} setIds(id); try{ const r=await fetch('/api/public/member?id='+id); const d=await r.json(); if(!r.ok||!d.registered)throw new Error('This invite ID isn’t registered on the smart contract.'); render(d); }catch(x){fallbackToRotation(x.message)} } function render(d){ const q=document.getElementById('invQualified'); q.outerHTML=d.directCount>=2 ?'★ Qualified' :`Building — ${d.directCount}/2 directs`; document.getElementById('invFacts').innerHTML= `
Level${esc(d.levelName)}
`+ `
Total received${fmt(d.totalEarnedPol)} POL
`+ `
Team below them${d.subtree?`${d.subtree.downCount} member${d.subtree.downCount===1?'':'s'}`:'—'}
`+ `
Directs${d.directCount}/2
`+ `
Tier${esc(d.tierName)}
`+ `
Member since${date(d.joinedAt)}
`; // Qualified sponsor: still a valid join (doctrine: late signups pay them // and spill down), but surface the team wave so nobody's surprised. const note=document.getElementById('invQualNote'); if(d.directCount>=2&&d.nextInLine){ note.innerHTML=`
This position is already qualified — joining here still works (you'll be placed in their team leg and your entry pays them directly). The team's current focus is helping Member #${d.nextInLine.id} get their 2 — either way, you're on the same team.
`; } const jb=document.getElementById('joinBtn'); if(d.referralUrl)jb.href=d.referralUrl; const cp=document.getElementById('copySponsor'); cp.addEventListener('click',async()=>{try{await navigator.clipboard.writeText(String(d.id));cp.textContent='Copied ✓';setTimeout(()=>cp.textContent='Copy Sponsor ID',1400)}catch(e){}}); } document.getElementById('joinForm').addEventListener('submit',async e=>{ e.preventDefault(); const form=e.currentTarget,msg=document.getElementById('joinMsg'); const newId=form.elements.newId.value.trim(),memberName=form.elements.memberName.value.trim(); if(!/^\d{1,10}$/.test(newId)){msg.style.color='var(--danger)';msg.textContent='Numbers only — the ID shown by the RM Circle dApp.';return} if(!memberName){msg.style.color='var(--danger)';msg.textContent='Add your name or Telegram handle.';return} msg.style.color='var(--muted)';msg.textContent='Verifying on the blockchain…'; try{ const r=await fetch('/api/public/submit-id',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({newId,memberName,sponsorId:id||'?',source:'invite-'+(id||'?'),clickid:window.ctbGetClickId?window.ctbGetClickId():''})}); const d=await r.json(); if(!r.ok)throw new Error(d.error||'Submission failed'); form.classList.add('hidden'); msg.style.color='var(--ok)'; const dash=`
Open your own dashboard →`; if(d.duplicate)msg.innerHTML=`✓ ID ${esc(newId)} was already submitted — you're on the list.${dash}`; else if(d.onchain&&d.onchain.registered)msg.innerHTML=`✓ Verified on the blockchain! ID ${esc(newId)} is registered under sponsor #${d.onchain.referrerId} (${esc(d.onchain.tier)}). The team has been notified — welcome aboard.${dash}`; else if(d.path==='notfound'){msg.style.color='var(--danger)';msg.innerHTML=`⚠ ID ${esc(newId)} isn't on the smart contract yet — double-check the number from the RM Circle dApp. Your submission is saved and the team will verify it manually.`;} else msg.innerHTML=`✓ Got it — submitted. The team will verify your placement.${dash}`; }catch(x){msg.style.color='var(--danger)';msg.textContent=x.message} }); // matrix dots (same as bridge) for(const pid of ['p8','p16']){ const node=document.getElementById(pid);if(!node)continue; const count=pid==='p8'?8:16;for(let i=0;i