`;
summaryContent.innerHTML = summaryHtml;
};
// --- PDF Download Logic ---
downloadPdfBtn.addEventListener('click', () => {
if (typeof window.jspdf === 'undefined' || !window.jspdf.jsPDF.autoTable) return;
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'pt', format: 'letter' });
const getValue = id => document.getElementById(id).value;
const page = { width: doc.internal.pageSize.getWidth(), margin: 72 };
let y = page.margin;
// Title Page
doc.setFont('times', 'bold');
doc.setFontSize(24);
doc.text('E-commerce Demand Prediction Report', page.width / 2, y, { align: 'center' });
y += 40;
doc.setFontSize(16);
doc.setFont('times', 'normal');
doc.text(`For Product: ${getValue('productName')}`, page.width / 2, y, { align: 'center' });
y += 20;
doc.text(`Date: ${new Date().toLocaleDateString()}`, page.width / 2, y, { align: 'center' });
doc.addPage();
y = page.margin;
// Executive Summary
doc.setFont('times', 'bold');
doc.setFontSize(16);
doc.text('Executive Summary', page.margin, y);
y += 20;
doc.setFont('times', 'normal');
doc.setFontSize(11);
const summaryText = `Based on the provided historical data and market factors, the total predicted demand for the next 12 months is ${document.getElementById('metricTotalSales').textContent}. This represents a predicted growth of ${document.getElementById('metricGrowth').textContent} over the previous year. The peak demand is expected in ${document.getElementById('metricPeakMonth').textContent}.`;
doc.text(doc.splitTextToSize(summaryText, page.width - page.margin * 2), page.margin, y);
y += 80;
// Prediction Table
const head = [['Month', 'Predicted Sales ($)']];
const body = predictionData.map(d => [d.month, d.sales.toLocaleString()]);
doc.setFont('times', 'bold');
doc.setFontSize(16);
doc.text('Monthly Demand Forecast', page.margin, y);
y += 20;
doc.autoTable({
startY: y, head, body, theme: 'grid',
headStyles: { fillColor: [13, 148, 136] },
styles: { font: 'times', fontSize: 10 }
});
doc.save(`Demand-Forecast-${getValue('productName')}.pdf`);
});
// --- Initial Setup ---
setupHistoricalData();
runPrediction();
switchTab('dashboard');
});
