`;
});
}
}
/**
* Renders a clone for PDF generation
*/
function ctg_renderPdfClone() {
ctg_pdfRenderClone.innerHTML = `
Trivia Results
Answer Review
`;
ctg_renderResults(ctg_pdfRenderClone, true); // Render data into the clone
}
/**
* Generates and downloads a PDF of the results
*/
async function ctg_downloadPDF() {
if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') {
console.error("CTG Tool Error: jsPDF or html2canvas library not loaded.");
alert("Error: PDF libraries failed to load. Please check console.");
return;
}
ctg_renderPdfClone(); // Create and populate the clone
const { jsPDF } = window.jspdf;
try {
const canvas = await html2canvas(ctg_pdfRenderClone, {
scale: 2,
useCORS: true,
});
const imgData = canvas.toDataURL('image/png');
const imgWidth = canvas.width;
const imgHeight = canvas.height;
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const ratio = Math.min((pdfWidth - 80) / imgWidth, (pdfHeight - 80) / imgHeight);
const w = imgWidth * ratio;
const h = imgHeight * ratio;
const x = (pdfWidth - w) / 2;
const y = 40; // Top margin
pdf.addImage(imgData, 'PNG', x, y, w, h);
pdf.save('Career_Trivia_Results.pdf');
} catch (error) {
console.error("CTG Tool Error: PDF generation failed.", error);
alert("An error occurred while generating the PDF. Please try again.");
}
}
// --- EVENT LISTENERS ---
// Tab link clicks
ctg_tabLinks.forEach((link, index) => {
link.addEventListener('click', () => ctg_switchTab(index));
});
// Next/Prev button clicks
if (ctg_prevButton) {
ctg_prevButton.addEventListener('click', () => {
if (ctg_currentTab > 0) ctg_switchTab(ctg_currentTab - 1);
});
}
if (ctg_nextButton) {
ctg_nextButton.addEventListener('click', () => {
if (ctg_currentTab < ctg_tabLinks.length - 1) ctg_switchTab(ctg_currentTab + 1);
});
}
// PDF download
if (ctg_downloadPdfButton) {
ctg_downloadPdfButton.addEventListener('click', ctg_downloadPDF);
}
// Game Controls
if (ctg_startButton) {
ctg_startButton.addEventListener('click', ctg_startGame);
}
if (ctg_nextQuestionButton) {
ctg_nextQuestionButton.addEventListener('click', ctg_nextQuestion);
}
if (ctg_playAgainButton) {
ctg_playAgainButton.addEventListener('click', () => ctg_renderGameState('start'));
}
// --- INITIALIZATION ---
ctg_loadQuestions();
ctg_renderConfig(); // Display questions in config tab initially
ctg_renderGameState('start'); // Show start screen
// Set initial tab state
ctg_tabPanes.forEach((pane, index) => {
pane.classList.toggle('hidden', index !== 0);
pane.classList.toggle('ctg-active', index === 0);
});
});