`).join('');
} else {
recommendationsEl.innerHTML = `
✓
`;
}
// Populate Input Summary Table
const summaryData = {
"Age": getInputValue('chrono-age'),
"Avg. Sleep Duration": getSelectedText('sleep-duration'),
"Sleep Quality": getSelectedText('sleep-quality'),
"Sleep Consistency": getSelectedText('sleep-consistency'),
"Late Caffeine": getSelectedText('caffeine'),
"Late Screen Time": getSelectedText('screen-time'),
"Late Vigorous Exercise": getSelectedText('evening-exercise'),
};
document.getElementById('pdf-input-summary').innerHTML = Object.entries(summaryData).map(([key, value]) => `Your sleep hygiene is excellent. Your habits strongly support healthy aging and daily recovery. Continue these beneficial practices.
${key}: ${value}
`).join('');
// --- Generate PDF ---
document.body.classList.add('pdf-generating');
setTimeout(() => {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
html2canvas(document.getElementById('pdf-report'), { scale: 3, useCORS: true, backgroundColor: '#ffffff' })
.then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save('Sleep-Wellness-Report.pdf');
document.body.classList.remove('pdf-generating');
}).catch(err => {
console.error("PDF Generation Error:", err);
document.body.classList.remove('pdf-generating');
});
}, 100);
};
// --- Event Listeners (Declared after functions) ---
tabs.forEach(tab => tab.addEventListener('click', () => switchTab(parseInt(tab.dataset.tab))));
prevBtn.addEventListener('click', () => { if (currentTab > 1) switchTab(currentTab - 1); });
nextBtn.addEventListener('click', () => { if (currentTab < 4) switchTab(currentTab + 1); });
downloadPdfBtn.addEventListener('click', generatePdf);
// --- Initialization ---
switchTab(1);
});
