Sleep & Aging Connection Analyzer
Discover how your sleep habits may be influencing your aging process.
Error:
Your Basic Profile
This information helps tailor the analysis.
Your Nightly Sleep Patterns
Habits Affecting Sleep
Your Sleep Analysis
Sleep Wellness Report
Report Date:
Sleep-Related Aging Risk Score
0
Interpretation & Key Recommendations
Your Input Summary
Disclaimer
This report provides an estimation for educational purposes and is not a substitute for medical advice. Consult a healthcare professional for any health concerns.
${summaryText}
`; downloadPdfBtn.disabled = false; }; const generatePdf = () => { const score = calculateScore(); let levelText, levelColor, recommendations = []; // Populate PDF Date document.getElementById('pdf-report-date').textContent = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }); // Determine Risk Level and Color for PDF if (score < 0) { levelText = "Low Risk"; levelColor = 'bg-green-100 text-green-800'; } else if (score <= 15) { levelText = "Moderate Risk"; levelColor = 'bg-yellow-100 text-yellow-800'; } else { levelText = "High Risk"; levelColor = 'bg-red-100 text-red-800'; } // Set PDF Score and Level document.getElementById('pdf-risk-score').textContent = score; document.getElementById('pdf-risk-level-text').textContent = levelText; document.getElementById('pdf-risk-level-container').className = `mt-2 inline-block px-4 py-1 rounded-full text-lg font-semibold ${levelColor}`; // Generate Recommendations if (getNumericValue('sleep-duration') >= 5) recommendations.push('Aim for 7-8 hours of sleep per night, as this is the optimal range for most adults for cellular repair and cognitive function.'); if (getNumericValue('sleep-quality') >= 5) recommendations.push('Improve sleep quality by ensuring your bedroom is dark, quiet, and cool. Avoid large meals close to bedtime.'); if (getNumericValue('sleep-consistency') >= 5) recommendations.push('Maintain a consistent sleep schedule, even on weekends, to regulate your body\'s internal clock (circadian rhythm).'); if (getNumericValue('caffeine') >= 5) recommendations.push('Avoid caffeine at least 8 hours before bed, as it can disrupt deep sleep cycles even if you fall asleep easily.'); if (getNumericValue('screen-time') >= 5) recommendations.push('Stop using electronic devices 1-2 hours before bed. The blue light can suppress melatonin production.'); if (getNumericValue('evening-exercise') >= 5) recommendations.push('While daily exercise is beneficial, vigorous workouts close to bedtime can raise core body temperature and interfere with sleep. Opt for gentle stretching or yoga instead.'); const recommendationsEl = document.getElementById('pdf-recommendations'); if (recommendations.length > 0) { recommendationsEl.innerHTML = recommendations.map(rec => `➤
`).join('');
} else {
recommendationsEl.innerHTML = `${rec}
✓
`;
}
// 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);
});
