`;
}).join('')}
${appState.quizTitle.toUpperCase()}
${appState.quizDate} | Total Questions: ${appState.questions.length}
`;
if (roundsHtml === '') {
quizContentDiv.innerHTML = 'Error: No questions have been added to the quiz sheet.
'; } else { quizContentDiv.innerHTML = metaHtml + roundsHtml; } } // --- Event Handlers --- // Update Dashboard Button updateBtn.addEventListener("click", function() { renderDashboardTab(); showTab(1); // Switch to Dashboard }); // Add Question addQBtn.addEventListener("click", function() { var type = qTypeSelect.value; var category = qCategorySelect.value; var difficulty = qDifficultySelect.value; var round = qRoundInput.value.trim(); var text = qTextInput.value.trim(); var answer = qAnswerInput.value.trim(); if (!text || !answer || !round) { alert("Please fill in the Question Text, Answer, and Round Number."); return; } var newQuestion = { id: Date.now(), type: type, category: category, difficulty: difficulty, round: round, text: text, answer: answer }; appState.questions.push(newQuestion); renderConfigTab(); // Clear form fields qTextInput.value = ""; qAnswerInput.value = ""; }); // Remove Question (Event Delegation) questionListDiv.addEventListener("click", function(e) { if (e.target.classList.contains("qbg-remove-btn")) { var id = parseInt(e.target.dataset.id); appState.questions = appState.questions.filter(function(q) { return q.id !== id; }); renderConfigTab(); } }); // PDF Download pdfBtn.addEventListener("click", function() { var jsPDF = window.jspdf.jsPDF; var titleSlug = quizTitleInput.value.replace(/[^a-zA-Z0-9\s]/g, '').replace(/\s/g, '_').substring(0, 30) || 'Quiz_Sheet'; var fileName = `${titleSlug}_Sheet.pdf`; html2canvas(exportArea, { scale: 2, useCORS: true, backgroundColor: '#ffffff' }).then(function(canvas) { var imgData = canvas.toDataURL('image/png'); var doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'letter' }); var pdfWidth = doc.internal.pageSize.getWidth(); var pdfHeight = doc.internal.pageSize.getHeight(); var imgProps = doc.getImageProperties(imgData); var imgWidth = imgProps.width; var imgHeight = imgProps.height; var margin = 40; var usableWidth = pdfWidth - (2 * margin); var ratio = usableWidth / imgWidth; var scaledHeight = imgHeight * ratio; // Handle multi-page if content exceeds page height if (scaledHeight > pdfHeight - (2 * margin)) { var pageHeight = pdfHeight - (2 * margin); var heightLeft = scaledHeight; var position = 0; while (heightLeft > 0) { doc.addImage(imgData, 'PNG', margin, position + margin, usableWidth, scaledHeight); heightLeft -= pageHeight; position -= pageHeight; if (heightLeft > 0) { doc.addPage(); } } } else { // Single page doc.addImage(imgData, 'PNG', margin, margin, usableWidth, scaledHeight); } doc.save(fileName); }).catch(function(err) { console.error("QBG PDF Error:", err); // alert("An error occurred while generating the PDF."); // Per spec }); }); // --- Initial Load --- renderConfigTab(); renderDashboardTab(); showTab(0); // Start on Config tab });