Students List & Invitations

⬅ Back to Dashboard
Full Name Class QR Preview Usage Print Options
ink.setAttribute("href", url); link.setAttribute("download", "Students_Template.csv"); document.body.appendChild(link); link.click(); document.body.removeChild(link); } async function importFile(input) { const file = input.files[0]; if (!file) return; const formData = new FormData(); formData.append('file', file); try { const response = await fetch('/students/import', { method: 'POST', body: formData }); if (!response.ok) { const errData = await response.json(); throw new Error(errData.message || 'Import failed'); } const result = await response.json(); alert(result.message || 'Import successful'); loadStudents(); } catch (err) { alert('Error: ' + err.message + '\n\nMake sure to rebuild the project in Visual Studio.'); } input.value = ''; } function printSingleInvitation(id) { const printUrl = `/students/${id}/invitation-image`; const printWin = window.open(printUrl, '_blank'); printWin.onload = function() { printWin.print(); }; } async function printAllInvitations() { const printArea = document.getElementById('printArea'); printArea.innerHTML = '
Preparing invitations for printing...
'; printArea.style.display = 'block'; try { const response = await fetch('/students'); const students = await response.json(); let html = ''; students.forEach((s, index) => { const isLast = index === students.length - 1; const pageBreak = isLast ? '' : 'style="page-break-after: always;"'; html += '
'; }); printArea.innerHTML = html; const images = printArea.getElementsByTagName('img'); let loadedCount = 0; if (images.length === 0) { alert('No students found to print invitations.'); printArea.style.display = 'none'; return; } for (let img of images) { img.onload = () => { loadedCount++; if (loadedCount === images.length) { window.print(); printArea.style.display = 'none'; printArea.innerHTML = ''; } }; img.onerror = () => { loadedCount++; if (loadedCount === images.length) { window.print(); printArea.style.display = 'none'; printArea.innerHTML = ''; } }; } } catch (err) { alert('An error occurred while preparing for printing.'); printArea.style.display = 'none'; } } async function loadStudents() { const tbody = document.getElementById('studentsTableBody'); tbody.innerHTML = 'Loading data...'; try { const response = await fetch('/students'); if (!response.ok) throw new Error('Failed to fetch data'); const students = await response.json(); tbody.innerHTML = ''; if (students.length === 0) { tbody.innerHTML = 'No students registered yet.'; return; } students.forEach(s => { const row = document.createElement('tr'); const nameCell = `${s.name}`; const classCell = `${s.class}`; const qrId = `qr-${s.id}`; const tokenText = s.qrCode ? s.qrCode.token : 'None'; const qrCell = `
${tokenText}
`; const scans = s.qrCode ? s.qrCode.scans : 0; const max = s.qrCode ? s.qrCode.max : 0; const counterCell = `${scans} / ${max}`; const actionsCell = `
💾 Save Image
`; row.innerHTML = nameCell + classCell + qrCell + counterCell + actionsCell; tbody.appendChild(row); if (s.qrCode && s.qrCode.token) { const qrContent = `QRINV:${s.qrCode.token}|${s.name}|${s.qrCode.max}`; new QRCode(document.getElementById(qrId), { text: qrContent, width: 80, height: 80, colorDark : "#1e293b", colorLight : "#ffffff", correctLevel : QRCode.CorrectLevel.H }); } }); } catch (error) { tbody.innerHTML = `Error: ${error.message}`; } } window.onload = loadStudents; rLight : "#ffffff", correctLevel : QRCode.CorrectLevel.H }); } }); } catch (error) { tbody.innerHTML = `خطأ: ${error.message}`; } } window.onload = loadStudents;
Web hosting by Somee.com