Spaces:
Running
Running
Download admin-dashboard.html from EHDEM/WASTELESS-V41: direct link, hf CLI and curl.
- Browser
- Download file 38 kB
-
https://huggingface.co/spaces/EHDEM/WASTELESS-V41/resolve/main/admin-dashboard.html
- Command line
-
hf download hf://spaces/EHDEM/WASTELESS-V41/admin-dashboard.html
-
curl -L -o admin-dashboard.html https://huggingface.co/spaces/EHDEM/WASTELESS-V41/resolve/main/admin-dashboard.html
38 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>WASTE-LESS - Admin Dashboard</title> | |
| <link rel="stylesheet" href="style.css"> | |
| <script src="https://cdn.tailwindcss.com"></script> | |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> | |
| <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | |
| </head> | |
| <body class="bg-gray-100 min-h-screen"> | |
| <div class="container mx-auto px-4 py-8 max-w-6xl"> | |
| <!-- Header with back button --> | |
| <header class="bg-blue-600 text-white rounded-lg shadow-lg p-6 mb-8"> | |
| <div class="flex items-center justify-between"> | |
| <div class="flex items-center"> | |
| <a href="index.html" class="text-blue-100 hover:text-white mr-4 text-xl"><i class="fas fa-home"></i></a> | |
| <div> | |
| <h1 class="text-3xl font-bold">Admin Dashboard</h1> | |
| <p class="text-blue-100">Manage food waste and attendance data</p> | |
| </div> | |
| </div> | |
| <button id="logout-btn" class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg"> | |
| <i class="fas fa-sign-out-alt mr-2"></i> Logout | |
| </button> | |
| </div> | |
| </header> | |
| <!-- Admin Controls --> | |
| <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8"> | |
| <!-- Quick Actions --> | |
| <div class="bg-white rounded-lg shadow-md p-6"> | |
| <h2 class="text-xl font-semibold mb-4 text-gray-800">Quick Actions</h2> | |
| <div class="grid grid-cols-2 gap-4"> | |
| <a href="attendance.html" class="bg-blue-50 hover:bg-blue-100 text-blue-700 p-4 rounded-lg flex flex-col items-center justify-center transition-colors"> | |
| <i class="fas fa-utensils text-2xl mb-2"></i> | |
| <span>Attendance Form A</span> | |
| </a> | |
| <a href="students-ate.html" class="bg-green-50 hover:bg-green-100 text-green-700 p-4 rounded-lg flex flex-col items-center justify-center transition-colors"> | |
| <i class="fas fa-check-circle text-2xl mb-2"></i> | |
| <span>Attendance Form B</span> | |
| </a> | |
| <a href="reports.html" class="bg-purple-50 hover:bg-purple-100 text-purple-700 p-4 rounded-lg flex flex-col items-center justify-center transition-colors"> | |
| <i class="fas fa-chart-bar text-2xl mb-2"></i> | |
| <span>View Reports</span> | |
| </a> | |
| <a href="student-management.html" class="bg-yellow-50 hover:bg-yellow-100 text-yellow-700 p-4 rounded-lg flex flex-col items-center justify-center transition-colors"> | |
| <i class="fas fa-users-cog text-2xl mb-2"></i> | |
| <span>Student Management</span> | |
| </a> | |
| <a href="#" class="bg-gray-100 hover:bg-gray-200 text-gray-700 p-4 rounded-lg flex flex-col items-center justify-center transition-colors"> | |
| <i class="fas fa-cog text-2xl mb-2"></i> | |
| <span>Settings</span> | |
| </a> | |
| </div> | |
| </div> | |
| <!-- System Overview --> | |
| <div class="bg-white rounded-lg shadow-md p-6"> | |
| <h2 class="text-xl font-semibold mb-4 text-gray-800">System Overview</h2> | |
| <div class="space-y-4"> | |
| <div class="flex justify-between items-center p-3 bg-gray-50 rounded-lg"> | |
| <span>Total Students</span> | |
| <span class="font-bold">1200</span> | |
| </div> | |
| <div class="flex justify-between items-center p-3 bg-gray-50 rounded-lg"> | |
| <span>Today's Attendance</span> | |
| <span class="font-bold">78%</span> | |
| </div> | |
| <div class="flex justify-between items-center p-3 bg-gray-50 rounded-lg"> | |
| <span>Food Waste Today</span> | |
| <span class="font-bold">15 kg</span> | |
| </div> | |
| <div class="flex justify-between items-center p-3 bg-gray-50 rounded-lg"> | |
| <span>Meals Served Today</span> | |
| <span class="font-bold">850</span> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- RFID Card Management --> | |
| <div class="bg-white rounded-lg shadow-md p-6 mb-8"> | |
| <div class="flex justify-between items-center mb-6"> | |
| <h2 class="text-xl font-semibold text-gray-800"><i class="fas fa-id-card mr-2 text-blue-600"></i>RFID Card Management</h2> | |
| <button id="refresh-rfid" class="text-blue-600 hover:text-blue-800"> | |
| <i class="fas fa-sync-alt mr-1"></i> Refresh | |
| </button> | |
| </div> | |
| <div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-6"> | |
| <!-- Assign / Update Card --> | |
| <div class="bg-blue-50 rounded-lg p-5"> | |
| <h3 class="font-bold text-blue-800 mb-4"><i class="fas fa-plus-circle mr-2"></i>Assign / Update Card</h3> | |
| <div class="space-y-3"> | |
| <input type="text" id="rfid-card-id" class="w-full px-3 py-2 border rounded-lg" placeholder="RFID Card ID (e.g. CARD001)"> | |
| <select id="rfid-admin-class" class="w-full px-3 py-2 border rounded-lg" onchange="loadRfidAdminStudents()"> | |
| <option value="">Select Class</option> | |
| </select> | |
| <select id="rfid-admin-form" class="w-full px-3 py-2 border rounded-lg" onchange="loadRfidAdminStudents()"> | |
| <option value="">Select Form</option> | |
| <option value="1">Form 1</option> | |
| <option value="2">Form 2</option> | |
| <option value="3">Form 3</option> | |
| <option value="4">Form 4</option> | |
| <option value="5">Form 5</option> | |
| </select> | |
| <select id="rfid-admin-student" class="w-full px-3 py-2 border rounded-lg"> | |
| <option value="">Select Student</option> | |
| </select> | |
| <button id="save-rfid-btn" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 rounded-lg transition"> | |
| <i class="fas fa-save mr-2"></i>Save Card | |
| </button> | |
| </div> | |
| <div id="rfid-save-status" class="mt-3 hidden p-2 rounded text-sm"></div> | |
| </div> | |
| <!-- Remove Card --> | |
| <div class="bg-red-50 rounded-lg p-5"> | |
| <h3 class="font-bold text-red-800 mb-4"><i class="fas fa-trash-alt mr-2"></i>Remove Card</h3> | |
| <div class="space-y-3"> | |
| <select id="remove-rfid-select" class="w-full px-3 py-2 border rounded-lg"> | |
| <option value="">Select Card ID</option> | |
| </select> | |
| <div id="remove-rfid-preview" class="text-sm text-gray-600 hidden"></div> | |
| <button id="remove-rfid-btn" class="w-full bg-red-600 hover:bg-red-700 text-white font-bold py-2 rounded-lg transition"> | |
| <i class="fas fa-trash mr-2"></i>Remove Card | |
| </button> | |
| </div> | |
| </div> | |
| <!-- Stats & Sync --> | |
| <div class="bg-green-50 rounded-lg p-5"> | |
| <h3 class="font-bold text-green-800 mb-4"><i class="fas fa-chart-pie mr-2"></i>Database Stats</h3> | |
| <div class="space-y-2 mb-4"> | |
| <div class="flex justify-between items-center p-2 bg-white rounded"> | |
| <span class="text-sm">Total Cards</span> | |
| <span class="font-bold text-lg" id="total-rfid-cards">0</span> | |
| </div> | |
| <div class="flex justify-between items-center p-2 bg-white rounded"> | |
| <span class="text-sm">Students w/o Card</span> | |
| <span class="font-bold text-lg" id="students-no-card">0</span> | |
| </div> | |
| </div> | |
| <button id="sync-rfid-btn" class="w-full bg-green-600 hover:bg-green-700 text-white font-bold py-2 rounded-lg transition text-sm"> | |
| <i class="fas fa-sync-alt mr-2"></i>Auto-Generate Missing Cards | |
| </button> | |
| <p class="text-xs text-green-700 mt-2">Creates cards for students without one using format: CLASS-FORM-###</p> | |
| </div> | |
| </div> | |
| <!-- Cards Table --> | |
| <div class="overflow-x-auto max-h-80 overflow-y-auto"> | |
| <table class="min-w-full divide-y divide-gray-200"> | |
| <thead class="bg-gray-50 sticky top-0"> | |
| <tr> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">RFID ID</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Student Name</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Class</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Form</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Action</th> | |
| </tr> | |
| </thead> | |
| <tbody id="rfidTableBody" class="bg-white divide-y divide-gray-200"> | |
| <tr><td colspan="5" class="px-4 py-4 text-center text-gray-500">Loading RFID data...</td></tr> | |
| </tbody> | |
| </table> | |
| </div> | |
| </div> | |
| <!-- Attendance Data Tables --> | |
| <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8"> | |
| <!-- Form A Data --> | |
| <div class="bg-white rounded-lg shadow-md p-6"> | |
| <div class="flex justify-between items-center mb-4"> | |
| <h2 class="text-xl font-semibold text-gray-800">Students Who Want To Eat (Form A)</h2> | |
| <button id="refresh-formA" class="text-blue-600 hover:text-blue-800"> | |
| <i class="fas fa-sync-alt mr-1"></i> Refresh | |
| </button> | |
| </div> | |
| <div class="overflow-x-auto max-h-96 overflow-y-auto"> | |
| <table class="min-w-full divide-y divide-gray-200"> | |
| <thead class="bg-gray-50 sticky top-0"> | |
| <tr> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Student</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Class</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Form</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Attendance</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Meal</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Time</th> | |
| </tr> | |
| </thead> | |
| <tbody class="bg-white divide-y divide-gray-200" id="formAData"> | |
| <tr> | |
| <td colspan="6" class="px-4 py-4 text-center text-gray-500">Loading data...</td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| </div> | |
| <div class="mt-4 text-sm text-gray-500"> | |
| <a href="https://github.com/SMSS-Waste-Less/Waste-LessV5/blob/main/Data%20Students%20who%20want%20to%20eat" target="_blank" class="text-blue-600 hover:underline"> | |
| View full data on GitHub | |
| </a> | |
| </div> | |
| </div> | |
| <!-- Form B Data --> | |
| <div class="bg-white rounded-lg shadow-md p-6"> | |
| <div class="flex justify-between items-center mb-4"> | |
| <h2 class="text-xl font-semibold text-gray-800">Students Who Have Eaten (Form B)</h2> | |
| <button id="refresh-formB" class="text-blue-600 hover:text-blue-800"> | |
| <i class="fas fa-sync-alt mr-1"></i> Refresh | |
| </button> | |
| </div> | |
| <div class="overflow-x-auto max-h-96 overflow-y-auto"> | |
| <table class="min-w-full divide-y divide-gray-200"> | |
| <thead class="bg-gray-50 sticky top-0"> | |
| <tr> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Student</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Class</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Form</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Meal</th> | |
| <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 uppercase">Time</th> | |
| </tr> | |
| </thead> | |
| <tbody class="bg-white divide-y divide-gray-200" id="formBData"> | |
| <tr> | |
| <td colspan="5" class="px-4 py-4 text-center text-gray-500">Loading data...</td> | |
| </tr> | |
| </tbody> | |
| </table> | |
| </div> | |
| <div class="mt-4 text-sm text-gray-500"> | |
| <a href="https://github.com/WaffleExecutors/Waste-LessV5/blob/main/data%20who%20has%20ate%20at%20dm" target="_blank" class="text-blue-600 hover:underline"> | |
| View full data on GitHub | |
| </a> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Charts --> | |
| <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8"> | |
| <div class="bg-white rounded-lg shadow-md p-6"> | |
| <h2 class="text-xl font-semibold mb-4 text-gray-800">Attendance Trend</h2> | |
| <canvas id="attendanceChart" height="250"></canvas> | |
| </div> | |
| <div class="bg-white rounded-lg shadow-md p-6"> | |
| <h2 class="text-xl font-semibold mb-4 text-gray-800">Food Waste Breakdown</h2> | |
| <canvas id="wasteChart" height="250"></canvas> | |
| </div> | |
| </div> | |
| </div> | |
| <script src="config.js"></script> | |
| <script> | |
| // Login check | |
| (function() { | |
| if (localStorage.getItem('isAdminLoggedIn') !== 'true') { | |
| alert('Please login first'); | |
| window.location.href = 'admin-login.html'; | |
| } | |
| })(); | |
| // Fetch data | |
| async function fetchAttendanceData() { | |
| try { | |
| document.getElementById('formAData').innerHTML = '<tr><td colspan="6" class="text-center py-4"><i class="fas fa-spinner fa-spin"></i> Loading...</td></tr>'; | |
| document.getElementById('formBData').innerHTML = '<tr><td colspan="5" class="text-center py-4"><i class="fas fa-spinner fa-spin"></i> Loading...</td></tr>'; | |
| // Form A | |
| const aUrl = `https://api.github.com/repos/${GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/${GITHUB_CONFIG.PATHS.FORM_A}`; | |
| const aResp = await fetch(aUrl, { | |
| headers: { 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`, 'Accept': 'application/vnd.github.v3+json' } | |
| }); | |
| let aRows = '<tr><td colspan="6" class="text-center py-4 text-gray-500">No data</td></tr>'; | |
| if (aResp.ok) { | |
| const aFile = await aResp.json(); | |
| const aContent = atob(aFile.content); | |
| const lines = aContent.split('\n').filter(l => l.trim()).reverse(); | |
| if (lines.length > 0) { | |
| aRows = lines.map(l => { | |
| try { | |
| const r = JSON.parse(l); | |
| const t = new Date(r.timestamp).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'}); | |
| const att = r.attendance === 'yes' ? 'Will Eat' : 'Will Not Eat'; | |
| return `<tr class="hover:bg-gray-50"> | |
| <td class="px-4 py-2 text-sm">${r.studentName || 'Unknown'}</td> | |
| <td class="px-4 py-2 text-sm">${r.class || '-'}</td> | |
| <td class="px-4 py-2 text-sm">${r.form || '-'}</td> | |
| <td class="px-4 py-2 text-sm">${att}</td> | |
| <td class="px-4 py-2 text-sm">${r.mealTime || '-'}</td> | |
| <td class="px-4 py-2 text-sm text-gray-500">${t}</td> | |
| </tr>`; | |
| } catch(e) { console.error('Parse error:', e); return ''; } | |
| }).join(''); | |
| } | |
| } else { | |
| console.error('Form A fetch failed:', aResp.status, await aResp.text()); | |
| aRows = '<tr><td colspan="6" class="text-center py-4 text-red-500">Failed to load Form A data (HTTP ' + aResp.status + ')</td></tr>'; | |
| } | |
| document.getElementById('formAData').innerHTML = aRows || '<tr><td colspan="6" class="text-center py-4 text-gray-500">No data</td></tr>'; | |
| // Form B | |
| const bUrl = `https://api.github.com/repos/${GITHUB_CONFIG.REPOS.CONSUMED}/contents/${GITHUB_CONFIG.PATHS.FORM_B}`; | |
| const bResp = await fetch(bUrl, { | |
| headers: { 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`, 'Accept': 'application/vnd.github.v3+json' } | |
| }); | |
| let bRows = '<tr><td colspan="5" class="text-center py-4 text-gray-500">No data</td></tr>'; | |
| if (bResp.ok) { | |
| const bFile = await bResp.json(); | |
| const bContent = atob(bFile.content); | |
| const lines = bContent.split('\n').filter(l => l.trim()).reverse(); | |
| if (lines.length > 0) { | |
| bRows = lines.map(l => { | |
| try { | |
| const r = JSON.parse(l); | |
| const t = new Date(r.timestamp).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'}); | |
| return `<tr class="hover:bg-gray-50"> | |
| <td class="px-4 py-2 text-sm">${r.studentName || 'Unknown'}</td> | |
| <td class="px-4 py-2 text-sm">${r.class || '-'}</td> | |
| <td class="px-4 py-2 text-sm">${r.form || '-'}</td> | |
| <td class="px-4 py-2 text-sm">${r.mealTime || '-'}</td> | |
| <td class="px-4 py-2 text-sm text-gray-500">${t}</td> | |
| </tr>`; | |
| } catch(e) { console.error('Parse error:', e); return ''; } | |
| }).join(''); | |
| } | |
| } else { | |
| console.error('Form B fetch failed:', bResp.status, await bResp.text()); | |
| bRows = '<tr><td colspan="5" class="text-center py-4 text-red-500">Failed to load Form B data (HTTP ' + bResp.status + ')</td></tr>'; | |
| } | |
| document.getElementById('formBData').innerHTML = bRows || '<tr><td colspan="5" class="text-center py-4 text-gray-500">No data</td></tr>'; | |
| } catch (err) { | |
| console.error('fetchAttendanceData error:', err); | |
| document.getElementById('formAData').innerHTML = '<tr><td colspan="6" class="text-center py-4 text-red-500">Error: ' + err.message + '</td></tr>'; | |
| document.getElementById('formBData').innerHTML = '<tr><td colspan="5" class="text-center py-4 text-red-500">Error: ' + err.message + '</td></tr>'; | |
| } | |
| } | |
| // Load data on start and refresh every 30 seconds | |
| fetchAttendanceData(); | |
| setInterval(fetchAttendanceData, 30000); | |
| // Refresh buttons | |
| document.getElementById('refresh-formA').onclick = fetchAttendanceData; | |
| document.getElementById('refresh-formB').onclick = fetchAttendanceData; | |
| // Initialize charts with empty data | |
| const attendanceChart = new Chart(document.getElementById('attendanceChart'), { | |
| type: 'line', | |
| data: { | |
| labels: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'], | |
| datasets: [{ | |
| label: 'Attendance Rate (%)', | |
| data: [], | |
| borderColor: '#3b82f6', | |
| backgroundColor: 'rgba(59, 130, 246, 0.1)', | |
| borderWidth: 2, | |
| tension: 0.3, | |
| fill: true | |
| }] | |
| }, | |
| options: { | |
| responsive: true, | |
| plugins: { | |
| tooltip: { | |
| callbacks: { | |
| label: function(context) { | |
| return `${context.dataset.label}: ${context.raw}%`; | |
| } | |
| } | |
| } | |
| }, | |
| scales: { | |
| y: { | |
| min: 0, | |
| max: 100, | |
| ticks: { | |
| callback: function(value) { | |
| return value + '%'; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| const wasteChart = new Chart(document.getElementById('wasteChart'), { | |
| type: 'bar', | |
| data: { | |
| labels: ['Breakfast','Lunch','Dinner','Supper'], | |
| datasets: [{ | |
| label: 'Food Waste (kg)', | |
| data: [], | |
| backgroundColor: [ | |
| 'rgba(234, 179, 8, 0.7)', | |
| 'rgba(22, 163, 74, 0.7)', | |
| 'rgba(99, 102, 241, 0.7)', | |
| 'rgba(168, 85, 247, 0.7)' | |
| ], | |
| borderColor: [ | |
| 'rgba(234, 179, 8, 1)', | |
| 'rgba(22, 163, 74, 1)', | |
| 'rgba(99, 102, 241, 1)', | |
| 'rgba(168, 85, 247, 1)' | |
| ], | |
| borderWidth: 1 | |
| }] | |
| }, | |
| options: { | |
| responsive: true, | |
| plugins: { | |
| tooltip: { | |
| callbacks: { | |
| label: function(context) { | |
| return `${context.dataset.label}: ${context.raw}kg`; | |
| } | |
| } | |
| } | |
| }, | |
| scales: { | |
| y: { | |
| beginAtZero: true, | |
| title: { | |
| display: true, | |
| text: 'Kilograms' | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| // RFID Management | |
| let rfidDb = {}; | |
| let rfidSha = ''; | |
| let studentListData = {}; | |
| let rfidAdminClasses = []; | |
| async function loadRfidDb() { | |
| try { | |
| const resp = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/${GITHUB_CONFIG.PATHS.RFID_DB}`, { | |
| headers: { 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`, 'Accept': 'application/vnd.github.v3+json' } | |
| }); | |
| if (resp.ok) { | |
| const data = await resp.json(); | |
| rfidDb = JSON.parse(atob(data.content)); | |
| rfidSha = data.sha; | |
| } else if (resp.status === 404) { | |
| rfidDb = {}; | |
| rfidSha = ''; | |
| } else { | |
| console.error('RFID DB fetch failed:', resp.status); | |
| } | |
| renderRfidTable(); | |
| updateRfidStats(); | |
| } catch (e) { | |
| console.error('RFID load error:', e); | |
| document.getElementById('rfidTableBody').innerHTML = '<tr><td colspan="5" class="text-center py-4 text-red-500">Failed to load RFID data</td></tr>'; | |
| } | |
| } | |
| async function saveRfidDb(message) { | |
| try { | |
| const content = btoa(JSON.stringify(rfidDb, null, 2)); | |
| const body = { | |
| message: message || `Update RFID DB - ${new Date().toLocaleString()}`, | |
| content: content, | |
| branch: 'main' | |
| }; | |
| if (rfidSha) body.sha = rfidSha; | |
| const resp = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.ATTENDANCE}/contents/${GITHUB_CONFIG.PATHS.RFID_DB}`, { | |
| method: 'PUT', | |
| headers: { | |
| 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`, | |
| 'Content-Type': 'application/json', | |
| 'Accept': 'application/vnd.github.v3+json' | |
| }, | |
| body: JSON.stringify(body) | |
| }); | |
| if (!resp.ok) throw new Error('Save failed'); | |
| const result = await resp.json(); | |
| rfidSha = result.content.sha; | |
| return true; | |
| } catch (e) { | |
| alert('Failed to save RFID DB: ' + e.message); | |
| return false; | |
| } | |
| } | |
| function renderRfidTable() { | |
| const tbody = document.getElementById('rfidTableBody'); | |
| const cards = Object.keys(rfidDb).sort(); | |
| if (cards.length === 0) { | |
| tbody.innerHTML = '<tr><td colspan="5" class="px-4 py-4 text-center text-gray-500">No RFID cards registered</td></tr>'; | |
| return; | |
| } | |
| tbody.innerHTML = cards.map(id => { | |
| const r = rfidDb[id]; | |
| return `<tr class="hover:bg-gray-50"> | |
| <td class="px-4 py-2 text-sm font-mono font-medium text-blue-600">${id}</td> | |
| <td class="px-4 py-2 text-sm">${r.studentName}</td> | |
| <td class="px-4 py-2 text-sm">${r.class}</td> | |
| <td class="px-4 py-2 text-sm">${r.form}</td> | |
| <td class="px-4 py-2 text-sm"> | |
| <button onclick="editRfidCard('${id}')" class="text-blue-600 hover:text-blue-800 mr-2"><i class="fas fa-edit"></i></button> | |
| <button onclick="deleteRfidCard('${id}')" class="text-red-600 hover:text-red-800"><i class="fas fa-trash"></i></button> | |
| </td> | |
| </tr>`; | |
| }).join(''); | |
| const removeSel = document.getElementById('remove-rfid-select'); | |
| removeSel.innerHTML = '<option value="">Select Card ID</option>'; | |
| cards.forEach(id => { | |
| const opt = document.createElement('option'); | |
| opt.value = id; | |
| opt.textContent = `${id} - ${rfidDb[id].studentName}`; | |
| removeSel.appendChild(opt); | |
| }); | |
| } | |
| function updateRfidStats() { | |
| document.getElementById('total-rfid-cards').textContent = Object.keys(rfidDb).length; | |
| let withoutCard = 0; | |
| Object.keys(studentListData).forEach(key => { | |
| const [cls, form] = key.split('-'); | |
| (studentListData[key] || []).forEach(name => { | |
| const hasCard = Object.values(rfidDb).some(r => r.studentName === name && r.class === cls && r.form === form); | |
| if (!hasCard) withoutCard++; | |
| }); | |
| }); | |
| document.getElementById('students-no-card').textContent = withoutCard; | |
| } | |
| async function loadStudentListForRfid() { | |
| try { | |
| const resp = await fetch(`https://api.github.com/repos/${GITHUB_CONFIG.REPOS.STUDENT_LIST}/contents/${GITHUB_CONFIG.PATHS.STUDENTS}`, { | |
| headers: { 'Authorization': `token ${GITHUB_CONFIG.TOKEN}`, 'Accept': 'application/vnd.github.v3+json' } | |
| }); | |
| if (resp.ok) { | |
| const data = await resp.json(); | |
| studentListData = JSON.parse(atob(data.content)); | |
| console.log('Student list loaded:', studentListData); | |
| // Extract classes - handles both flat {"BUDI-1": [...]} and nested {"BUDI": {"1": [...]}} | |
| const allKeys = Object.keys(studentListData); | |
| rfidAdminClasses = [...new Set(allKeys.map(key => { | |
| if (key.includes('-')) return key.split('-')[0]; | |
| return key; // nested: key is just the class name | |
| }))].sort(); | |
| const classSel = document.getElementById('rfid-admin-class'); | |
| classSel.innerHTML = '<option value="">Select Class</option>'; | |
| rfidAdminClasses.forEach(c => { | |
| const opt = document.createElement('option'); | |
| opt.value = c; opt.textContent = c; | |
| classSel.appendChild(opt); | |
| }); | |
| console.log('Classes populated:', rfidAdminClasses); | |
| } else { | |
| console.error('Student list fetch failed:', resp.status); | |
| document.getElementById('rfid-save-status').className = 'mt-3 p-2 rounded text-sm bg-red-100 text-red-700'; | |
| document.getElementById('rfid-save-status').innerHTML = '<i class="fas fa-exclamation-circle mr-1"></i>Failed to load class list (HTTP ' + resp.status + ')'; | |
| document.getElementById('rfid-save-status').classList.remove('hidden'); | |
| } | |
| } catch (e) { | |
| console.error('Student list load error:', e); | |
| document.getElementById('rfid-save-status').className = 'mt-3 p-2 rounded text-sm bg-red-100 text-red-700'; | |
| document.getElementById('rfid-save-status').innerHTML = '<i class="fas fa-exclamation-circle mr-1"></i>Error loading classes: ' + e.message; | |
| document.getElementById('rfid-save-status').classList.remove('hidden'); | |
| } | |
| } | |
| function loadRfidAdminStudents() { | |
| const cls = document.getElementById('rfid-admin-class').value; | |
| const form = document.getElementById('rfid-admin-form').value; | |
| const sel = document.getElementById('rfid-admin-student'); | |
| sel.innerHTML = '<option value="">Select Student</option>'; | |
| if (cls && form) { | |
| // Try flat structure first: "BUDI-1" | |
| let studentList = studentListData[`${cls}-${form}`]; | |
| // Fallback to nested structure: studentListData["BUDI"]["1"] | |
| if (!studentList && studentListData[cls] && studentListData[cls][form]) { | |
| studentList = studentListData[cls][form]; | |
| } | |
| if (studentList && studentList.length > 0) { | |
| console.log('Students for', cls, form, ':', studentList); | |
| studentList.forEach(item => { | |
| const opt = document.createElement('option'); | |
| let name = item; | |
| if (typeof item === 'object' && item !== null) { | |
| name = item.name || item.studentName || item.student || item.fullName || JSON.stringify(item); | |
| } | |
| opt.value = name; opt.textContent = name; | |
| sel.appendChild(opt); | |
| }); | |
| } else { | |
| console.log('No students found for', cls, form); | |
| } | |
| } | |
| } | |
| document.getElementById('save-rfid-btn').addEventListener('click', async () => { | |
| const id = document.getElementById('rfid-card-id').value.trim().toUpperCase(); | |
| const cls = document.getElementById('rfid-admin-class').value; | |
| const form = document.getElementById('rfid-admin-form').value; | |
| const student = document.getElementById('rfid-admin-student').value; | |
| if (!id) return alert('Enter RFID Card ID'); | |
| if (!cls || !form || !student) return alert('Select class, form and student'); | |
| rfidDb[id] = { studentName: student, class: cls, form: form }; | |
| const saved = await saveRfidDb(`Assign card ${id} to ${student}`); | |
| if (saved) { | |
| renderRfidTable(); | |
| updateRfidStats(); | |
| document.getElementById('rfid-card-id').value = ''; | |
| const status = document.getElementById('rfid-save-status'); | |
| status.className = 'mt-3 p-2 rounded text-sm bg-green-100 text-green-700'; | |
| status.innerHTML = '<i class="fas fa-check-circle mr-1"></i>Card saved'; | |
| status.classList.remove('hidden'); | |
| setTimeout(() => status.classList.add('hidden'), 3000); | |
| } | |
| }); | |
| document.getElementById('remove-rfid-btn').addEventListener('click', async () => { | |
| const id = document.getElementById('remove-rfid-select').value; | |
| if (!id) return alert('Select a card to remove'); | |
| if (!confirm(`Remove card ${id} assigned to ${rfidDb[id]?.studentName}?`)) return; | |
| delete rfidDb[id]; | |
| const saved = await saveRfidDb(`Remove card ${id}`); | |
| if (saved) { renderRfidTable(); updateRfidStats(); } | |
| }); | |
| document.getElementById('remove-rfid-select').addEventListener('change', function() { | |
| const preview = document.getElementById('remove-rfid-preview'); | |
| if (this.value && rfidDb[this.value]) { | |
| const r = rfidDb[this.value]; | |
| preview.textContent = `${r.studentName} (${r.class} Form ${r.form})`; | |
| preview.classList.remove('hidden'); | |
| } else { | |
| preview.classList.add('hidden'); | |
| } | |
| }); | |
| function editRfidCard(id) { | |
| const r = rfidDb[id]; | |
| document.getElementById('rfid-card-id').value = id; | |
| document.getElementById('rfid-admin-class').value = r.class; | |
| document.getElementById('rfid-admin-form').value = r.form; | |
| loadRfidAdminStudents(); | |
| setTimeout(() => { document.getElementById('rfid-admin-student').value = r.studentName; }, 100); | |
| window.scrollTo({ top: 0, behavior: 'smooth' }); | |
| } | |
| async function deleteRfidCard(id) { | |
| if (!confirm(`Delete card ${id}?`)) return; | |
| delete rfidDb[id]; | |
| await saveRfidDb(`Delete card ${id}`); | |
| renderRfidTable(); | |
| updateRfidStats(); | |
| } | |
| document.getElementById('sync-rfid-btn').addEventListener('click', async () => { | |
| let created = 0; | |
| Object.keys(studentListData).forEach(key => { | |
| const [cls, form] = key.split('-'); | |
| const students = studentListData[key] || []; | |
| students.forEach((name, idx) => { | |
| const hasCard = Object.entries(rfidDb).some(([id, r]) => r.studentName === name && r.class === cls && r.form === form); | |
| if (!hasCard) { | |
| const newId = `${cls}-${form}-${String(idx + 1).padStart(3, '0')}`; | |
| rfidDb[newId] = { studentName: name, class: cls, form: form }; | |
| created++; | |
| } | |
| }); | |
| }); | |
| if (created > 0) { | |
| const saved = await saveRfidDb(`Auto-generated ${created} RFID cards`); | |
| if (saved) { | |
| renderRfidTable(); | |
| updateRfidStats(); | |
| alert(`Created ${created} new RFID cards!`); | |
| } | |
| } else { | |
| alert('All students already have RFID cards assigned.'); | |
| } | |
| }); | |
| document.getElementById('refresh-rfid').addEventListener('click', () => { | |
| loadRfidDb(); | |
| loadStudentListForRfid(); | |
| }); | |
| // Load RFID data on start | |
| loadRfidDb(); | |
| loadStudentListForRfid(); | |
| // Logout | |
| document.getElementById('logout-btn').onclick = () => { | |
| localStorage.clear(); | |
| window.location.href = 'admin-login.html'; | |
| }; | |
| </script> | |
| </body> | |
| </html> |