${tableHtml}
`;
};
// --- CORE LOGIC ---
const handleFind = () => {
findBtnSpinner.classList.remove('hidden');
findBtnText.textContent = 'Extracting...';
findBtn.disabled = true;
setTimeout(() => {
const seed = document.getElementById('seed-keyword-input').value.trim().toLowerCase();
if (!seed) {
findBtnSpinner.classList.add('hidden');
findBtnText.textContent = 'Extract Keywords';
findBtn.disabled = false;
return;
}
let foundKeywords = [];
const association = keywordAssociations.find(a => a.seed.toLowerCase() === seed);
const generateMetrics = (baseVolume, baseComp) => ({
volume: Math.floor(Math.random() * (baseVolume * 0.8) + (baseVolume * 0.2)),
competition: Math.random() * (baseComp * 0.4) + (baseComp * 0.6)
});
if (association) {
foundKeywords = association.related.split(',').map(k => ({
term: k.trim(),
...generateMetrics(50000, 0.8)
}));
} else {
foundKeywords = [
`${seed} guide`, `best ${seed} tools`, `what is ${seed}`, `${seed} for beginners`, `${seed} trends`
].map(k => ({
term: k,
...generateMetrics(10000, 0.5)
}));
}
const result = {
seed: document.getElementById('seed-keyword-input').value.trim(),
keywords: foundKeywords.sort((a,b) => b.volume - a.volume)
};
renderDashboard(result);
downloadPdfBtn.disabled = false;
findBtnSpinner.classList.add('hidden');
findBtnText.textContent = 'Extract Keywords';
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('Related-Keywords-Report.pdf');
});
};
// --- EVENT LISTENERS ---
window.switchTab = switchTab;
window.navigateTabs = navigateTabs;
findBtn.addEventListener('click', handleFind);
downloadPdfBtn.addEventListener('click', handlePdfDownload);
addAssociationBtn.addEventListener('click', () => {
keywordAssociations.push({ id: nextId++, seed: '', related: '' });
renderConfig();
});
configContainer.addEventListener('input', e => {
const id = parseInt(e.target.closest('[data-id]').dataset.id);
const prop = e.target.dataset.prop;
const item = keywordAssociations.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);
keywordAssociations = keywordAssociations.filter(a => a.id !== id);
renderConfig();
});
// --- INITIALIZATION ---
renderConfig();
updateNavButtons();
switchTab('dashboard');
});
