${tableHtml}
`;
};
// --- CORE LOGIC ---
const handleFind = () => {
findBtnSpinner.classList.remove('hidden');
findBtnText.textContent = 'Searching...';
findBtn.disabled = true;
setTimeout(() => {
const topic = document.getElementById('topic-input').value.trim().toLowerCase();
if (!topic) {
findBtnSpinner.classList.add('hidden');
findBtnText.textContent = 'Find Questions';
findBtn.disabled = false;
return;
}
let foundQuestions = [];
const association = questionAssociations.find(a => a.topic.toLowerCase() === topic);
if (association) {
foundQuestions = association.questions.split('\n').map(q => ({ text: q.trim(), difficulty: (Math.random() * 40 + 50).toFixed(0) })); // 50-90
} else {
// Generate generic questions if none are found
const prefixes = ['What is', 'How to', 'Why is', 'Best', 'Alternatives to'];
foundQuestions = prefixes.map(p => ({ text: `${p} ${topic}?`, difficulty: (Math.random() * 30 + 20).toFixed(0) })); // 20-50
}
const result = {
topic: document.getElementById('topic-input').value.trim(),
questions: foundQuestions
};
renderDashboard(result);
downloadPdfBtn.disabled = false;
findBtnSpinner.classList.add('hidden');
findBtnText.textContent = 'Find Questions';
findBtn.disabled = false;
}, 800);
};
// --- UI & EVENT HANDLERS ---
const switchTab = (tabId) => {
currentTab = tabId;
Object.values(tabPanes).forEach(pane => pane.classList.add('hidden'));
tabPanes[tabId].classList.remove('hidden');
Object.values(tabButtons).forEach(btn => btn.classList.replace('tab-active', 'tab-inactive'));
tabButtons[tabId].classList.replace('tab-inactive', 'tab-active');
updateNavButtons();
};
const navigateTabs = (direction) => {
const currentIndex = tabs.indexOf(currentTab);
const newIndex = direction === 'next' ? currentIndex + 1 : currentIndex - 1;
if (newIndex >= 0 && newIndex < tabs.length) switchTab(tabs[newIndex]);
};
const updateNavButtons = () => {
const currentIndex = tabs.indexOf(currentTab);
prevBtn.disabled = currentIndex === 0;
nextBtn.disabled = currentIndex === tabs.length - 1;
prevBtn.classList.toggle('opacity-50', prevBtn.disabled);
nextBtn.classList.toggle('opacity-50', nextBtn.disabled);
};
const handlePdfDownload = () => {
const pdfRenderContainer = document.getElementById('pdf-render-content');
const pdfContent = document.getElementById('pdf-content').innerHTML;
const header = `';
html2canvas(pdfRenderContainer, { scale: 2 }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth(), margin = 40;
const contentWidth = pdfWidth - margin * 2;
const pdfHeight = (canvas.height * contentWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', margin, margin, contentWidth, pdfHeight);
pdf.save('Question-Keyword-Report.pdf');
});
};
// --- EVENT LISTENERS ---
window.switchTab = switchTab;
window.navigateTabs = navigateTabs;
findBtn.addEventListener('click', handleFind);
downloadPdfBtn.addEventListener('click', handlePdfDownload);
addAssociationBtn.addEventListener('click', () => {
questionAssociations.push({ id: nextId++, topic: '', questions: '' });
renderConfig();
});
configContainer.addEventListener('input', e => {
const id = parseInt(e.target.closest('[data-id]').dataset.id);
const prop = e.target.dataset.prop;
const item = questionAssociations.find(a => a.id === id);
if (item) item[prop] = e.target.value;
});
configContainer.addEventListener('click', e => {
if (!e.target.classList.contains('rm-btn')) return;
const id = parseInt(e.target.closest('[data-id]').dataset.id);
questionAssociations = questionAssociations.filter(a => a.id !== id);
renderConfig();
});
// --- INITIALIZATION ---
renderConfig();
updateNavButtons();
switchTab('dashboard');
});
