/* ============================================================ ADMIN MODE + DELETE QUESTION + BETTER DASHBOARD ============================================================ */ (function() { const params = new URLSearchParams(location.search); const isAdmin = params.get('admin') === '1'; if (isAdmin) document.body.classList.add('admin-mode'); function delKey() { const s = params.get('stage') || ''; const l = params.get('lesson') || params.get('unit') || ''; return `mq_del_${s}_${l}`; } function getDeleted() { try { return JSON.parse(localStorage.getItem(delKey()) || '[]'); } catch(e) { return []; } } function saveDeleted(arr) { localStorage.setItem(delKey(), JSON.stringify(arr)); } // Filter out deleted questions from state when quiz loads if (typeof state !== 'undefined' && state.questions && state.questions.length) { const deleted = getDeleted(); if (deleted.length) { state.questions = state.questions.filter(q => !deleted.includes(q.text)); state.answers = state.answers.slice(0, state.questions.length); } } // Add delete button in admin mode function addDeleteButton() { if (!isAdmin) return; const qEl = document.querySelector('#questionContainer .question'); if (!qEl || qEl.querySelector('.mq-del-btn')) return; const btn = document.createElement('button'); btn.className = 'mq-del-btn'; btn.innerHTML = '🗑️ حذف السؤال ده نهائياً (Admin)'; btn.onclick = function() { if (!confirm('تحذف السؤال ده؟ مش هيظهر تاني للطلاب.')) return; const q = state.questions[state.currentIndex]; const deleted = getDeleted(); deleted.push(q.text); saveDeleted(deleted); state.questions.splice(state.currentIndex, 1); state.answers.splice(state.currentIndex, 1); if (!state.questions.length) { alert('تم حذف كل الأسئلة. ارفع الملف من الأول.'); return; } if (state.currentIndex >= state.questions.length) { state.currentIndex = state.questions.length - 1; } renderQuestion(); }; qEl.appendChild(btn); } // Watch question rendering const observer = new MutationObserver(addDeleteButton); observer.observe(document.body, { childList: true, subtree: true }); // --- Better dashboard with stats + medals --- async function enhanceDashboard() { const box = document.getElementById('leaderboardContainer'); const statsBox = document.getElementById('dashStats'); if (!box) return; try { const results = await loadResults(); const best = {}; results.forEach(r => { const key = (r.name || '').trim().toLowerCase() + '|' + (r.exam || ''); if (!best[key] || r.percentage > best[key].percentage) best[key] = r; }); const sorted = Object.values(best) .sort((a, b) => b.percentage - a.percentage || (b.correct - a.correct)) .slice(0, 50); if (statsBox) { const total = results.length; const avg = total ? Math.round(results.reduce((s, r) => s + (r.percentage || 0), 0) / total) : 0; statsBox.innerHTML = `
${total}
محاولة
${sorted.length}
طالب
${avg}%
متوسط
`; } if (!sorted.length) { box.innerHTML = '
📭 لا توجد نتائج بعد
'; return; } box.innerHTML = ''; sorted.forEach((r, i) => { const div = document.createElement('div'); div.className = 'leader-item'; if (i === 0) div.classList.add('gold'); else if (i === 1) div.classList.add('silver'); else if (i === 2) div.classList.add('bronze'); const medal = i === 0 ? '🥇' : i === 1 ? '🥈' : i === 2 ? '🥉' : (i + 1); const date = r.timestamp ? new Date(r.timestamp).toLocaleDateString('ar-EG') : ''; div.innerHTML = ` ${medal} ${r.name || 'طالب'}${r.examTitle || ''} · ${date} ${r.percentage}% `; box.appendChild(div); }); } catch (e) { console.warn('Dashboard enhancement failed', e); } } // Hook into dashboard button clicks with capture document.addEventListener('click', function(e) { const t = e.target; if (t.id === 'refreshLeaderboard' || t.id === 'showLeaderboard') { setTimeout(enhanceDashboard, 150); } }, true); // If dashboard opened directly if (location.hash === '#dashboard') { setTimeout(enhanceDashboard, 300); } })();