<!doctype html>
const pickId = `pick_${dep.departure_id}`;
card.innerHTML=`
<div class=\"row\"><div class=\"time\">${dep.depart_time}</div>
<div class=\"status ${dep.status}\">${dep.status.replaceAll('_',' ')}</div></div>
<div class=\"row\"><label class=\"pick\"><input id=\"${pickId}\" type=\"radio\" name=\"depPick\" value=\"${dep.departure_id}\"> Use for boarding</label><div>On board: <b>${dep.onboard_count}</b></div></div>
<div class=\"row btns\">
<button data-s=\"not_ready\" class=\"ghost\">Not Ready</button>
<button data-s=\"boarding\">Boarding</button>
<button data-s=\"about_to_depart\">About to depart</button>
<button data-s=\"departed\">Departed</button>
<button data-s=\"cancelled\" class=\"warn\">Cancel</button>
<button data-arrive class=\"ok full\">Mark Arrived</button>
</div>`;
card.querySelectorAll('button[data-s]').forEach(b=>b.onclick=()=>updateStatus(dep.departure_id,b.dataset.s));
card.querySelector('button[data-arrive]').onclick=()=>markArrived(dep.departure_id);
card.querySelector('input[type=radio]').onchange=()=>{
selectedDepartureId = dep.departure_id;
document.querySelectorAll('#departures .card').forEach(c=>c.classList.toggle('selected', c.dataset.departureId===String(selectedDepartureId)));
};
wrap.appendChild(card);
});
});
}


function updateStatus(departure_id,status){ api('update_departure_status',{departure_id,status},'POST').then(()=>loadDepartures()); }
function markArrived(departure_id){ api('mark_arrived',{departure_id},'POST').then(()=>{loadDepartures();refreshTotals();loadPeople();}); }


function loadPeople(){
const q=document.getElementById('search').value||'';
api('list_people',{q}).then(d=>{
const wrap=document.getElementById('people'); wrap.innerHTML='';
const list = (d.data||[]).slice().sort((a,b)=>{
const ao = a.location==='onboard'?1:0; const bo=b.location==='onboard'?1:0;
if(ao!==bo) return ao-bo; // non-onboard first
return (a.last_name+a.first_name).localeCompare(b.last_name+b.first_name);
});
list.forEach(p=>{
const div=document.createElement('div'); div.className='person';
div.innerHTML=`<button class=\"pill ${p.location}\">${p.location}</button> <span>${p.last_name}, ${p.first_name}</span>
<button class=\"board\">Toggle board</button>`;
div.querySelector('.board').onclick=()=>{
if(!currentShuttle){ alert('Select a shuttle first'); return; }
const depId = selectedDepartureId ? Number(selectedDepartureId) : (document.querySelector('#departures .card:not([data-status=\"departed\"]):not([data-status=\"cancelled\"])')?.dataset.departureId|0);
if(!depId){ alert('Pick a departure (radio button)'); return; }
api('toggle_board',{departure_id:Number(depId), person_id:p.person_id},'POST').then(()=>{ loadDepartures(); refreshTotals(); loadPeople(); });
};
wrap.appendChild(div);
});
});
}


// New shuttle & departure flows
function newShuttle(){
const name=prompt('Shuttle name (e.g., Restaurant → Hotels – Fri)'); if(!name) return;
const origin=prompt('Origin?'); if(!origin) return; const dest=prompt('Destination?'); if(!dest) return;
const sd=document.getElementById('svcDate').value || new Date().toISOString().slice(0,10);
api('create_shuttle',{name: name, origin, destination: dest, service_date: sd},'POST').then(()=>loadShuttles());
}
function newDeparture(){
if(!currentShuttle) return alert('Select a shuttle first');
const t=prompt('Departure time (HH:MM)'); if(!t) return;
api('create_departure',{shuttle_id: currentShuttle.shuttle_id, depart_time: t},'POST').then(()=>loadDepartures());
}


// Bulk status buttons
btnAllHotel.onclick=()=>api('bulk_set_location',{location:'hotel',scope:'all'},'POST').then(()=>{refreshTotals();loadPeople();});
btnAllVenue.onclick=()=>api('bulk_set_location',{location:'venue',scope:'all'},'POST').then(()=>{refreshTotals();loadPeople();});
btnAllUnknown.onclick=()=>api('bulk_set_location',{location:'unknown',scope:'all'},'POST').then(()=>{refreshTotals();loadPeople();});


// Wire up
btnLoad.onclick=()=>{ loadShuttles(); refreshTotals(); loadPeople(); };
btnNewShuttle.onclick=newShuttle; btnAddDepart.onclick=newDeparture; search.oninput=()=>loadPeople();


// defaults
svcDate.value = new Date().toISOString().slice(0,10);
refreshTotals(); loadShuttles(); loadPeople();
</script>
</body>
</html>
