No reaction events logged yet.
';
} else {
html += `
Reaction Events (Chronological)
`;
let tableHtml = `
| Date |
Trigger |
Symptoms |
Severity |
Treatment |
`;
logData.forEach(item => {
const severityColor = item.severity >= 4 ? '#e74c3c' : (item.severity >= 2 ? '#f39c12' : '#2ecc71');
tableHtml += `
| ${item.date} |
${item.trigger} |
${item.symptoms} |
${item.severity} |
${item.treatment} |
`;
});
tableHtml += `
`;
html += tableHtml;
}
container.innerHTML = html;
}
function atgSwitchTab(tabId) {
document.querySelectorAll('.atg-tab-btn').forEach(b => b.classList.remove('active'));
document.querySelectorAll('.atg-content').forEach(c => c.classList.remove('active'));
const idx = tabId === 'builder' ? 0 : 1;
document.querySelectorAll('.atg-tab-btn')[idx].classList.add('active');
document.getElementById('atg-' + tabId).classList.add('active');
if (tabId === 'report') {
atgRenderReport();
}
}
function atgLoadExample() {
if(!confirm("Overwrite current data and load example allergy events?")) return;
document.getElementById('inp-patient').value = "Jane D. Smith";
document.getElementById('inp-allergen').value = "Tree Pollen / Shellfish";
// Clear and refill log entries
document.getElementById('atg-log-rows-container').innerHTML = '';
atgAddLogRow("2025-10-25", "Shrimp (Dinner)", "Throat scratchiness, stomach cramps (severe)", "5", "Called 911, two EpiPens");
atgAddLogRow("2025-11-01", "High Pollen Count", "Runny nose, constant sneezing (moderate)", "2", "Daily antihistamine");
atgAddLogRow("2025-11-15", "Unknown (Processed Food)", "Hives on torso and arms, swelling (moderate)", "3", "Benadryl, Cortisone cream");
atgRenderReport();
atgSwitchTab('report');
}
/* --- PDF Generation --- */
async function atgGeneratePDF() {
atgRenderReport(); // Final render check
const logData = atgGetLogData();
if (logData.length === 0) {
alert("Please add reaction entries before generating the PDF.");
return;
}
const meta = {
patient: document.getElementById('inp-patient').value || "Patient Name",
allergen: document.getElementById('inp-allergen').value || "Suspected Allergen"
};
const { jsPDF } = window.jspdf;
const doc = new jsPDF('l', 'mm', 'a4'); // Landscape for better table fit
const blue = [0, 123, 255];
let y = 20;
// Header
doc.setFillColor(...blue);
doc.rect(0, 0, 297, 20, 'F');
doc.setTextColor(255, 255, 255);
doc.setFontSize(16);
doc.text(`Allergy Reaction Log: ${meta.patient}`, 14, 13);
// Meta Data
doc.setTextColor(0, 0, 0);
doc.setFontSize(10);
doc.setFont("helvetica", "normal");
doc.text(`Patient: ${meta.patient}`, 14, y + 10);
doc.text(`Suspected Allergen: ${meta.allergen}`, 140, y + 10);
doc.text(`Date Exported: ${new Date().toLocaleDateString()}`, 250, y + 10);
y += 20;
// Log Table
doc.setFontSize(12);
doc.setFont("helvetica", "bold");
doc.setTextColor(...blue);
doc.text("Reaction Events (Chronological)", 14, y);
y += 5;
const tableBody = logData.map(item => [
item.date,
item.trigger,
item.symptoms,
item.severity,
item.treatment
]);
doc.autoTable({
startY: y,
head: [['Date', 'Suspected Trigger', 'Symptoms & Location', 'Severity (1-5)', 'Treatment']],
body: tableBody,
theme: 'grid',
headStyles: { fillColor: blue, fontSize: 10 },
styles: { fontSize: 9 },
columnStyles: {
0: { cellWidth: 25, fontStyle: 'bold' },
2: { cellWidth: 80, overflow: 'linebreak' },
3: { cellWidth: 20, halign: 'center' },
4: { cellWidth: 50, overflow: 'linebreak' }
},
didParseCell: function(data) {
// Color code Severity in PDF
if (data.section === 'body' && data.column.index === 3) {
const val = parseInt(data.cell.raw);
if (val >= 4) data.cell.styles.fillColor = [240, 210, 210]; // Severe Red
else if (val >= 3) data.cell.styles.fillColor = [255, 240, 200]; // Moderate Yellow
}
}
});
// Notes Block
let finalY = doc.lastAutoTable.finalY + 15;
if (finalY > 180) { doc.addPage(); finalY = 30; } // Check for page break in landscape
doc.setFontSize(10);
doc.text("Notes for Physician / Observations on Pattern:", 14, finalY);
doc.setDrawColor(200);
doc.rect(14, finalY + 5, 270, 20, 'S'); // Large box for notes
doc.save(`AllergyLog_${meta.patient.replace(/\s/g, '_')}.pdf`);
}