`;
}
const recList = document.getElementById('recommendationsList');
recList.innerHTML = auditResults.recommendations.length > 0
? auditResults.recommendations.map(rec => `${rec} `).join('')
: 'No high-priority recommendations found. Great job! ';
};
// --- PDF Generation ---
const downloadPdf = () => {
if (!window.jspdf || !window.jspdf.jsPDF.autoTable) return;
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'letter' });
const page = { width: doc.internal.pageSize.getWidth(), margin: 72 };
let y = page.margin;
// Title Page
doc.setFont('times', 'bold');
doc.setFontSize(24);
doc.text('Website SEO Audit Report', page.width / 2, y, { align: 'center' });
y += 40;
doc.setFont('times', 'normal');
doc.setFontSize(16);
doc.text(`For: ${document.getElementById('websiteUrl').value}`, page.width / 2, y, { align: 'center' });
y += 20;
doc.text(`Date: ${new Date().toLocaleDateString()}`, page.width / 2, y, { align: 'center' });
y += 80;
doc.setFontSize(14);
doc.text(`Overall Score: ${auditResults.overall}/100`, page.width/2, y, {align: 'center'});
// Recommendations
doc.addPage();
y = page.margin;
doc.setFont('times', 'bold');
doc.setFontSize(18);
doc.text('Priority Recommendations', page.margin, y);
y += 25;
doc.setFont('times', 'normal');
doc.setFontSize(11);
auditResults.recommendations.forEach(rec => {
doc.text(`• ${rec}`, page.margin + 10, y, { maxWidth: page.width - page.margin * 2 - 10 });
y += doc.getTextDimensions(`• ${rec}`, { maxWidth: page.width - page.margin * 2 - 10 }).h + 10;
});
// Detailed Breakdown Tables
for (const category in auditConfig) {
doc.addPage();
y = page.margin;
doc.setFont('times', 'bold');
doc.setFontSize(18);
doc.text(`${category.charAt(0).toUpperCase() + category.slice(1)} Audit`, page.margin, y);
const head = [['Audit Point', 'Your Status', 'Recommendation']];
const body = auditResults.reportData
.filter(d => d.category === category)
.map(d => [d.item, d.answer.toUpperCase(), d.rec]);
doc.autoTable({
startY: y + 20, head, body, theme: 'grid',
headStyles: { fillColor: [29, 78, 216] },
styles: { font: 'times', fontSize: 10 },
didParseCell: function(data) {
if (data.cell.section === 'body' && data.column.index === 1) {
if (data.cell.raw === 'YES') data.cell.styles.textColor = [0, 128, 0];
if (data.cell.raw === 'NO') data.cell.styles.textColor = [255, 0, 0];
}
}
});
}
doc.save(`SEO-Audit-${document.getElementById('websiteUrl').value.replace(/https?:\/\//, '')}.pdf`);
};
downloadPdfBtn.addEventListener('click', downloadPdf);
// --- Initial Setup ---
switchTab('setup');
});
