import { apiGet } from '../api.js';
import { showToast } from '../app.js';
let coachesList = [];
let expandedCoachId = null;
let recentByCoach = {};
let filterSearch = '';
export async function coachesPage() {
const app = document.getElementById('app');
app.innerHTML = `
`;
try {
const data = await apiGet('coaches.php');
coachesList = data.coaches || [];
renderCoaches();
} catch (e) {
showToast(e.message, 'error');
document.getElementById('coachesContent').innerHTML =
`${esc(e.message)}
`;
}
}
function coachSearchText(c) {
return [c.username, c.coachID, c.supervisorUsername].filter(Boolean).join(' ');
}
function coachHasUploads(c) {
return (c.totalSubmissions ?? 0) > 0;
}
function filteredCoaches() {
const q = filterSearch.trim().toLowerCase();
if (!q) return coachesList;
return coachesList.filter(c => coachSearchText(c).toLowerCase().includes(q));
}
function renderCoaches() {
const el = document.getElementById('coachesContent');
if (!coachesList.length) {
el.innerHTML = `No coaches
`;
return;
}
el.innerHTML = `
`;
document.getElementById('coachListSearch').addEventListener('input', (e) => {
filterSearch = e.target.value;
const filtered = filteredCoaches();
if (
expandedCoachId &&
!filtered.some(c => c.coachID === expandedCoachId && coachHasUploads(c))
) {
expandedCoachId = null;
}
renderCoachTableBody();
});
renderCoachTableBody();
}
function coachRowHTML(c) {
const canExpand = coachHasUploads(c);
const expanded = canExpand && expandedCoachId === c.coachID;
const recent = recentByCoach[c.coachID] || [];
const expandControl = canExpand
? ` `
: '';
return `
| ${expandControl}${esc(c.username)} |
${esc(c.supervisorUsername || '—')} |
${c.clientCount ?? 0} |
${c.totalSubmissions ?? 0} |
${c.submissionsLast7d ?? 0} |
${c.submissionsLast30d ?? 0} |
${esc(c.lastSubmissionAt || '—')} |
${expanded ? `
${recent.length ? `
| When | Client | Questionnaire | Ver. |
${recent.map(s => `
| ${esc(s.submittedAt)} |
${esc(s.clientCode)} |
${esc(s.questionnaireName)} |
${s.version} |
`).join('')}
` : ' Loading… '}
|
` : ''}`;
}
function renderCoachTableBody() {
const body = document.getElementById('coachTableBody');
const countEl = document.getElementById('coachListCount');
if (!body) return;
const filtered = filteredCoaches();
if (!filtered.length) {
body.innerHTML = `
|
No coaches match
|
`;
if (countEl) {
countEl.textContent = filterSearch.trim()
? `0 of ${coachesList.length} coaches`
: '';
}
return;
}
body.innerHTML = filtered.map(c => coachRowHTML(c)).join('');
if (countEl) {
const q = filterSearch.trim();
countEl.textContent = q
? `${filtered.length} of ${coachesList.length} coaches`
: `${coachesList.length} coach${coachesList.length === 1 ? '' : 'es'}`;
}
body.querySelectorAll('.coach-expand-btn').forEach(btn => {
btn.addEventListener('click', () => toggleCoach(btn.dataset.coach));
});
}
async function toggleCoach(coachID) {
const coach = coachesList.find(c => c.coachID === coachID);
if (!coach || !coachHasUploads(coach)) {
return;
}
if (expandedCoachId === coachID) {
expandedCoachId = null;
renderCoachTableBody();
return;
}
expandedCoachId = coachID;
renderCoachTableBody();
if (!recentByCoach[coachID]) {
try {
const data = await apiGet(
`coaches.php?coachID=${encodeURIComponent(coachID)}&recent=1`
);
recentByCoach[coachID] = data.submissions || [];
} catch (e) {
showToast(e.message, 'error');
recentByCoach[coachID] = [];
}
renderCoachTableBody();
}
}
function esc(s) {
const d = document.createElement('div');
d.textContent = s ?? '';
return d.innerHTML;
}