`;
resultsSection.style.display = 'block';
resultsSection.scrollIntoView({ behavior: 'smooth' });
}
/**
* Generates and downloads a PDF report.
*/
async function generatePdf() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const results = calculateTransitTime();
if (!results) return;
const userName = document.getElementById('user-name').value || 'N/A';
const testDate = testDateInput.value ? new Date(testDateInput.value).toLocaleDateString() : 'N/A';
const pageHeight = doc.internal.pageSize.height;
const pageWidth = doc.internal.pageSize.width;
const margin = 15;
let y = margin;
// Header
doc.setFont('helvetica', 'bold');
doc.setFontSize(20);
doc.text('Digestive Transit Time Report', margin, y);
y += 15;
// User Info
doc.setFont('helvetica', 'bold');
doc.setFontSize(11);
doc.text('Name:', margin, y);
doc.setFont('helvetica', 'normal');
doc.text(userName, margin + 20, y);
doc.setFont('helvetica', 'bold');
doc.text('Test Date:', pageWidth / 2 + 20, y);
doc.setFont('helvetica', 'normal');
doc.text(testDate, pageWidth / 2 + 40, y);
y += 10;
doc.setDrawColor(226, 232, 240);
doc.line(margin, y, pageWidth - margin, y);
y += 15;
// Result Summary
doc.setFont('helvetica', 'bold');
doc.setFontSize(14);
doc.text('Calculation Result', margin, y);
y += 10;
doc.setFontSize(12);
doc.text('Your Transit Time:', margin, y);
doc.setFont('helvetica', 'bold');
doc.text(`${results.hours.toFixed(1)} hours`, margin + 50, y);
y += 15;
// Analysis
doc.setFillColor(243, 244, 246);
doc.rect(margin, y, pageWidth - 2 * margin, 40, 'F');
doc.setFont('helvetica', 'bold');
doc.setFontSize(12);
doc.text('Status:', margin + 5, y + 8);
if (results.status.includes('Optimal')) doc.setTextColor(34, 197, 94);
else if (results.status.includes('Fast')) doc.setTextColor(249, 115, 22);
else doc.setTextColor(220, 38, 38);
doc.text(results.status, margin + 25, y + 8);
doc.setTextColor(0, 0, 0);
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
const recLines = doc.splitTextToSize(results.recommendation, pageWidth - (margin * 2) - 10);
doc.text(recLines, margin + 5, y + 18);
y += 50;
// Footer
doc.setFontSize(8);
doc.setTextColor(148, 163, 184);
doc.text(`This calculator is an informational tool and not a substitute for a medical diagnosis.`, margin, pageHeight - 15);
doc.text(`Consult with a qualified healthcare professional for any persistent digestive concerns.`, margin, pageHeight - 10);
doc.save(`Digestive_Transit_Time_Report_${userName.replace(/ /g, '_')}.pdf`);
}
// --- EVENT LISTENERS ---
if(calculateBtn) calculateBtn.addEventListener('click', displayResults);
if(downloadPdfBtn) downloadPdfBtn.addEventListener('click', generatePdf);
// --- INITIALIZATION ---
testDateInput.valueAsDate = new Date();
});
