🧘 ${state.config.sectionTitles.stress}
${data.recommendations.stress}
💬 ${state.config.sectionTitles.social}
${data.recommendations.social}
`;
planPlaceholderEl.classList.add('hidden');
planContentEl.classList.remove('hidden');
}
// --- CONFIGURATION TAB ---
function populateConfig() {
const container = document.getElementById('config-inputs');
container.innerHTML = Object.entries(state.config.sectionTitles).map(([key, value]) => `
${key.charAt(0).toUpperCase() + key.slice(1)} Section Title
`).join('');
container.addEventListener('change', handleConfigChange);
}
function handleConfigChange(e) {
if (e.target.dataset.configKey) {
const key = e.target.dataset.configKey;
state.config.sectionTitles[key] = e.target.value;
}
}
// --- PDF GENERATION ---
window.downloadPDF = async () => {
const { jsPDF } = window.jspdf;
if (!state.planData) return;
const { inputs, recommendations } = state.planData;
const reportContainer = document.createElement('div');
reportContainer.id = 'pdf-report';
reportContainer.className = 'w-[800px] bg-white p-10 text-gray-800';
reportContainer.style.position = 'absolute';
reportContainer.style.left = '-9999px';
reportContainer.style.fontFamily = 'Inter, sans-serif';
reportContainer.innerHTML = `
Brain Health Improvement Plan
Generated: ${new Date().toLocaleString('en-US', { dateStyle: 'full', timeStyle: 'short' })}
Your Assessment Summary
Exercise: ${inputs.exerciseFreq} days/week
Learning: ${inputs.learningFreq}
Diet: ${inputs.dietQuality.split(' ')[0]}
Stress: ${inputs.stressLevel}
Sleep: ${inputs.sleepHours} hours/night
Social: ${inputs.socialFreq}
Your Action Plan
🏃 ${state.config.sectionTitles.exercise} ${recommendations.exercise}
🥗 ${state.config.sectionTitles.nutrition} ${recommendations.nutrition}
😴 ${state.config.sectionTitles.sleep} ${recommendations.sleep}
🧠 ${state.config.sectionTitles.learning} ${recommendations.learning}
🧘 ${state.config.sectionTitles.stress} ${recommendations.stress}
💬 ${state.config.sectionTitles.social} ${recommendations.social}
This plan provides general recommendations. Consult with healthcare professionals for personalized medical advice. Consistency is key to improving brain health.
`;
document.body.appendChild(reportContainer);
try {
const canvas = await html2canvas(reportContainer, { scale: 2, useCORS: true, logging: false });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' });
const pageHeight = pdf.internal.pageSize.getHeight();
const pageWidth = pdf.internal.pageSize.getWidth();
const margin = 15;
const contentWidth = pageWidth - (margin * 2);
const imgProps = pdf.getImageProperties(imgData);
const contentHeight = (imgProps.height * contentWidth) / imgProps.width;
let heightLeft = contentHeight;
let position = 0;
pdf.addImage(imgData, 'PNG', margin, margin, contentWidth, contentHeight);
heightLeft -= (pageHeight - (margin * 2));
while (heightLeft > 0) {
position -= (pageHeight - (margin * 2));
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight);
heightLeft -= (pageHeight - (margin * 2));
}
pdf.save('Brain-Health-Improvement-Plan.pdf');
} catch (error) {
console.error("Error generating PDF:", error);
alert("Sorry, there was an error creating the PDF report.");
} finally {
document.body.removeChild(reportContainer);
}
};
// --- INITIALIZATION ---
switchTab(1);
populateConfig();
});