`;
}
function renderNutrients(trimester) {
const container = document.getElementById('nutrients-content');
const data = nutrientData[trimester];
container.innerHTML = data.map(nutrient => `
`).join('');
}
// --- PDF Generation ---
async function generatePDF() {
if (!state.planGenerated) return;
// Populate PDF data
document.getElementById('pdf-report-date').textContent = new Date().toLocaleDateString('en-US', { dateStyle: 'long' });
const trimester = document.getElementById('trimester-select').value;
const preferences = Array.from(document.querySelectorAll('[data-preference]:checked')).map(el => el.dataset.preference);
const health = Array.from(document.querySelectorAll('[data-health]:checked')).map(el => el.dataset.health);
document.getElementById('pdf-profile-summary').innerHTML = `
${nutrient.name}
${nutrient.detail}
Find it in: ${nutrient.foods}
Trimester: ${trimester}
Preferences: ${preferences.length > 0 ? preferences.join(', ').replace(/_/g, ' ') : 'None'}
Considerations: ${health.length > 0 ? health.join(', ').replace(/_/g, ' ') : 'None'}
`; document.getElementById('pdf-meal-plan').innerHTML = document.getElementById('meal-plan-content').innerHTML; document.getElementById('pdf-nutrients').innerHTML = document.getElementById('nutrients-content').innerHTML; // Generate PDF const { jsPDF } = window.jspdf; const originalButtonText = downloadPdfBtn.textContent; downloadPdfBtn.textContent = 'Generating...'; downloadPdfBtn.disabled = true; const content = document.getElementById('pdf-content'); const clone = content.cloneNode(true); clone.style.position = 'absolute'; clone.style.left = '-9999px'; clone.style.top = '0'; clone.style.display = 'block'; document.body.appendChild(clone); try { const canvas = await html2canvas(clone, { scale: 2, useCORS: true, logging: false }); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const imgWidth = canvas.width; const imgHeight = canvas.height; const ratio = imgWidth / imgHeight; let finalImgWidth = pdfWidth; let finalImgHeight = pdfWidth / ratio; if (finalImgHeight > pdfHeight) { finalImgHeight = pdfHeight; finalImgWidth = pdfHeight * ratio; } pdf.addImage(imgData, 'PNG', 0, 0, finalImgWidth, finalImgHeight); pdf.save('Prenatal-Nutrition-Plan.pdf'); } catch (error) { console.error("PDF Generation Error:", error); } finally { document.body.removeChild(clone); downloadPdfBtn.textContent = 'Download PDF Report'; downloadPdfBtn.disabled = false; } } // --- Event Listeners --- generatePlanBtn.addEventListener('click', generatePlan); downloadPdfBtn.addEventListener('click', generatePDF); // --- Initial Call --- showTab(0); });