// LinkSpin Team: the line as a working table (dashboard + line + carry APIs from the engine).
(function () {
const $ = id => document.getElementById(id); const esc = CR.esc, fmt = CR.fmt;
let D = null, LINE = null, LEVEL = 1, FILTERS = new Set(), ME = null;
CR.mount({ active: 'team', title: 'Team', sub: 'loading' });
const dayStart = () => { const d = new Date(); d.setHours(0, 0, 0, 0); return d.getTime(); };
const ago = t => { if (!t) return 'never'; const d = Date.now() - t; if (d < 3600e3) return Math.max(1, Math.round(d / 60e3)) + ' min ago'; if (d < 86400e3) return Math.round(d / 3600e3) + ' h ago'; const days = Math.floor(d / 86400e3); return days === 1 ? 'yesterday' : days + ' d ago'; };
const pol = wei => { try { return (Number(BigInt(wei || '0') / 10000000000000000n) / 100).toFixed(2); } catch (e) { return '0.00'; } };
function rows() {
const L = (LINE && LINE.levels || []).find(x => x.level === LEVEL); let ms = L ? L.members.slice() : [];
const f = { today: m => (m.lastSeen || 0) >= dayStart(), wallet: m => !!m.wallet, nopay: m => !!m.wallet && !m.activated, nowallet: m => !m.wallet };
for (const k of FILTERS) ms = ms.filter(f[k]);
ms.sort((a, b) => (b.lastSeen || 0) - (a.lastSeen || 0));
return ms;
}
function render() {
const ms = rows(); const L = (LINE && LINE.levels || []).find(x => x.level === LEVEL); const total = L ? L.members.length : 0;
$('rows').innerHTML = ms.length ? ms.map(m => { const today = (m.lastSeen || 0) >= dayStart();
return '
' + esc(m.name) + (m.own ? ' YOURS' : '') + '
' + (m.memberId ? '#' + m.memberId + ' · ' : '') + 'joined ' + new Date(m.joined).toLocaleDateString() + (m.bought ? ' · $25+ buy' : '') + (m.buyers ? ' · ' + m.buyers + ' qualifying' : '') + '
'
+ '
' + (today ? 'today' : ago(m.lastSeen)) + ''
+ '
' + (m.activated ? 'payouts on' : m.wallet ? 'wallet, payouts off' : 'no wallet') + ''
+ '
' + (m.earnedWei && m.earnedWei !== '0' ? '+' + pol(m.earnedWei) + ' POL' : '0') + ''
+ '
' + (m.sponsor ? 'via ' + esc(m.sponsor) : (m.email ? esc(m.email) : '')) + ' '; }).join('')
: '' + (total ? 'Nobody matches those filters.' : 'Nobody on this level yet. Share your link, or put it in a spin.') + '
';
$('foot').textContent = 'level ' + LEVEL + ' · ' + ms.length + (ms.length !== total ? ' of ' + total : '') + ' · surf earnings and full activity are in the classic dashboard for now';
}
async function load() {
try { [D, LINE, ME] = await Promise.all([CR.get('/api/my/dashboard'), CR.get('/api/my/line'), CR.me()]); } catch (e) { CR.sub('could not load'); return; }
const counts = (LINE.counts || []); const refs = D.referrals || [];
CR.sub((counts[0] || 0) + ' direct' + (counts[0] === 1 ? '' : 's') + ' · ' + ((counts[1] || 0) + (counts[2] || 0)) + ' deeper');
$('tiles').innerHTML = [['Directs', fmt(counts[0] || 0), refs.filter(r => r.activated).length + ' with payouts on'], ['Levels 2 and 3', fmt((counts[1] || 0) + (counts[2] || 0)), (counts[1] || 0) + ' · ' + (counts[2] || 0)], ['Qualifying buyers', fmt(D.buyerCount || 0), (D.buyerCount || 0) >= 5 ? 'level 3 open' : (D.buyerCount || 0) >= 2 ? 'level 2 open' : 'level 2 at 2 buyers'], ['Paid by the line', pol(D.earnedWei || '0') + ' POL', (D.earnCount || 0) + ' payouts']]
.map(([k, v, n], i) => '' + k + '' + v + '' + n + '
').join('');
const inv = 'https://' + location.host + '/join/' + (ME.username || ME.refCode || ME.memberId || ''); $('invite').textContent = inv; $('copyInvite').onclick = () => CR.copy(inv);
$('sponsor').innerHTML = ME.sponsorName ? '' + esc(ME.sponsorName) + '' + (ME.sponsorId ? ' #' + ME.sponsorId + '' : '') + 'Carried over from the network where you first joined. Nothing to set up, nothing anyone can change.
' : (ME.sponsorId ? 'member #' + ME.sponsorId + '' : 'Not set. Your first purchase locks a sponsor.');
try { const c = await CR.get('/api/my/carry'); const cl = (c.claims || []).filter(x => x.sponsor === (ME.email || '').toLowerCase()); if (cl.length) { $('claimPanel').hidden = false; $('claims').innerHTML = cl.length + ' of your people can use every tool now; their first purchase waits up to ' + (c.claimDays || 3) + ' days for you to link a wallet and switch on payouts, then walks up your line.'; } } catch (e) {}
render();
}
document.querySelectorAll('.lvl [data-l]').forEach(b => b.addEventListener('click', () => { LEVEL = Number(b.dataset.l); document.querySelectorAll('.lvl [data-l]').forEach(x => x.classList.toggle('on', x === b)); render(); }));
$('chips').querySelectorAll('[data-f]').forEach(b => b.addEventListener('click', () => { const k = b.dataset.f; if (FILTERS.has(k)) FILTERS.delete(k); else FILTERS.add(k); b.classList.toggle('on', FILTERS.has(k)); render(); }));
CR.requireSession(load);
})();