Location: ${entry.location}
Type: ${entry.type}
Triggers: ${entry.triggers}
Relief Measures: ${entry.relief}
${entry.notes ? `
Notes: ${entry.notes}
` : ''}
`).join('');
}
};
// --- Form & Entry Logic ---
const resetAndPrefillForm = () => {
painEntryForm.reset();
const now = new Date();
const year = now.getFullYear();
const month = (now.getMonth() + 1).toString().padStart(2, '0');
const day = now.getDate().toString().padStart(2, '0');
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
entryDate.value = `${year}-${month}-${day}`;
entryTime.value = `${hours}:${minutes}`;
painLevelSlider.value = 5;
painLevelValue.textContent = '5';
};
painLevelSlider.addEventListener('input', (e) => {
painLevelValue.textContent = e.target.value;
});
const saveEntry = () => {
// Basic validation
if (!entryDate.value || !entryTime.value || !painLocation.value) {
alert('Please fill in at least the date, time, and pain location.');
return;
}
const newEntry = {
id: Date.now(), // Unique ID
date: entryDate.value,
time: entryTime.value,
level: parseInt(painLevelSlider.value, 10),
location: painLocation.value,
type: painType.value,
triggers: painTriggers.value,
relief: reliefMeasures.value,
notes: entryNotes.value
};
painLog.push(newEntry);
switchTab(0); // Go back to the log view
};
// --- PDF Generation ---
const generatePdf = () => {
const { jsPDF } = window.jspdf;
if (typeof jsPDF === 'undefined' || typeof jsPDF.API.autoTable !== 'function') {
console.error("jsPDF or autoTable library not loaded correctly.");
alert("Could not generate PDF. Please try again.");
return;
}
if (painLog.length === 0) {
alert("There are no entries to download.");
return;
}
const doc = new jsPDF();
doc.setFont('helvetica', 'bold');
doc.setFontSize(20);
doc.text('Chronic Pain Diary Report', 105, 20, { align: 'center' });
const tableData = painLog.map(entry => [
`${entry.date} ${entry.time}`,
entry.level,
entry.location,
entry.type,
entry.triggers,
entry.relief,
entry.notes
]);
doc.autoTable({
startY: 30,
head: [['Date & Time', 'Level', 'Location', 'Type', 'Triggers', 'Relief', 'Notes']],
body: tableData,
theme: 'grid',
headStyles: { fillColor: [59, 130, 246] }, // Blue header
styles: { fontSize: 8, cellPadding: 2 },
columnStyles: {
0: { cellWidth: 25 },
1: { cellWidth: 10 },
2: { cellWidth: 25 },
3: { cellWidth: 20 },
4: { cellWidth: 'auto' },
5: { cellWidth: 'auto' },
6: { cellWidth: 'auto' },
}
});
doc.save('Chronic_Pain_Diary.pdf');
};
downloadPdfBtn.addEventListener('click', generatePdf);
// --- Initial Load ---
updateUI();
});