${tableHtml}
`;
};
// --- CORE LOGIC ---
const handleFind = () => {
findBtnSpinner.classList.remove('hidden');
findBtnText.textContent = 'Analyzing...';
findBtn.disabled = true;
setTimeout(() => {
const seed = document.getElementById('seed-keyword-input').value.trim().toLowerCase();
if (!seed) {
findBtnSpinner.classList.add('hidden');
findBtnText.textContent = 'Find LSI Keywords';
findBtn.disabled = false;
return;
}
let foundKeywords = [];
const association = keywordAssociations.find(a => a.seed.toLowerCase() === seed);
if (association) {
foundKeywords = association.lsi.split(',').map(k => ({ term: k.trim(), relevance: Math.random() * 0.3 + 0.7 })); // 0.7 - 1.0
} else {
// Generate generic keywords if none are found
foundKeywords = [
`${seed} services`, `best ${seed}`, `top ${seed} companies`, `${seed} strategies`, `what is ${seed}`
].map(k => ({ term: k, relevance: Math.random() * 0.4 + 0.3 })); // 0.3 - 0.7
}
const result = {
seed: document.getElementById('seed-keyword-input').value.trim(),
keywords: foundKeywords.sort((a,b) => b.relevance - a.relevance)
};
renderDashboard(result);
downloadPdfBtn.disabled = false;
findBtnSpinner.classList.add('hidden');
findBtnText.textContent = 'Find LSI 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('LSI-Keyword-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: '', lsi: '' });
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');
});
