Temperature
${latestVitals.temp || 'N/A'} °F
Next Appointment
${state.appointments.length > 0 ? state.appointments[0].date : 'None'}
`;
};
const renderVitalsChart = () => {
const sortedVitals = [...state.vitals].sort((a,b) => new Date(a.date) - new Date(b.date));
const labels = sortedVitals.map(v => v.date);
if (vitalsChart) vitalsChart.destroy();
vitalsChart = new Chart(vitalsChartCanvas.getContext('2d'), {
type: 'line',
data: {
labels: labels,
datasets: [
{ label: 'Systolic', data: sortedVitals.map(v => v.bp_sys), borderColor: '#ef4444', tension: 0.1 },
{ label: 'Diastolic', data: sortedVitals.map(v => v.bp_dia), borderColor: '#3b82f6', tension: 0.1 }
]
},
options: {
responsive: true, maintainAspectRatio: false,
scales: { x: { type: 'time', time: { unit: 'day' } }, y: { beginAtZero: false, title: { display: true, text: 'mmHg' } } }
}
});
};
const renderAppointments = () => {
appointmentsList.innerHTML = '';
if(state.appointments.length === 0) {
appointmentsList.innerHTML = `
No upcoming appointments.
`;
return;
}
state.appointments.forEach(a => {
appointmentsList.innerHTML += `
${a.description}
${a.date} at ${a.time}
`;
});
};
const renderMedications = () => {
medicationsList.innerHTML = '';
if(state.medications.length === 0) {
medicationsList.innerHTML = `
No current medications.
`;
return;
}
state.medications.forEach(m => {
medicationsList.innerHTML += `
${m.name}
${m.dosage}, ${m.frequency}
`;
});
};
const renderConfigRows = () => {
// Vitals
vitalsConfig.innerHTML = '';
[...state.vitals].sort((a,b) => new Date(b.date) - new Date(a.date)).forEach(v => {
vitalsConfig.innerHTML += `
`.replaceAll('class="config-input"', 'class="config-input w-full border-gray-300 rounded-md shadow-sm"');
});
// Appointments
apptConfig.innerHTML = '';
[...state.appointments].sort((a,b) => new Date(b.date) - new Date(a.date)).forEach(a => {
apptConfig.innerHTML += `
`.replaceAll('class="config-input"', 'class="config-input w-full border-gray-300 rounded-md shadow-sm"');
});
// Medications
medConfig.innerHTML = '';
state.medications.forEach(m => {
medConfig.innerHTML += `
`.replaceAll('class="config-input"', 'class="config-input w-full border-gray-300 rounded-md shadow-sm"');
});
addConfigEventListeners();
};
// --- EVENT HANDLERS ---
const handleConfigChange = (e) => {
const id = parseInt(e.target.dataset.id);
const type = e.target.dataset.type;
const field = e.target.dataset.field;
const value = (e.target.type === 'number') ? parseFloat(e.target.value) || 0 : e.target.value;
const entry = state[type].find(item => item.id === id);
if (entry) entry[field] = value;
renderAll();
};
const handleAdd = (type) => {
const today = new Date().toISOString().split('T')[0];
const newId = state[type].length > 0 ? Math.max(...state[type].map(item => item.id)) + 1 : 1;
if (type === 'vitals') {
state.vitals.push({ id: newId, date: today, bp_sys: 120, bp_dia: 80, hr: 70, temp: 98.6 });
} else if (type === 'appointments') {
state.appointments.push({ id: newId, date: today, time: "12:00", description: "New Appointment" });
} else { // medications
state.medications.push({ id: newId, name: "New Medication", dosage: "10mg", frequency: "Once daily" });
}
renderAll();
};
const handleRemove = (e) => {
const id = parseInt(e.target.dataset.id);
const type = e.target.dataset.type;
state[type] = state[type].filter(item => item.id !== id);
renderAll();
};
const addConfigEventListeners = () => {
document.querySelectorAll('.config-input').forEach(input => input.addEventListener('change', handleConfigChange));
document.querySelectorAll('.remove-btn').forEach(button => button.addEventListener('click', handleRemove));
};
const handleDownloadPdf = () => {
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content');
document.querySelectorAll('.no-print').forEach(el => el.style.visibility = 'hidden');
Chart.defaults.animation = false;
html2canvas(pdfContent, { scale: 2, useCORS: true, backgroundColor: '#ffffff' }).then(canvas => {
document.querySelectorAll('.no-print').forEach(el => el.style.visibility = 'visible');
Chart.defaults.animation = true;
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const imgWidth = pdfWidth - 20;
const imgHeight = canvas.height * imgWidth / canvas.width;
pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight);
pdf.save('Patient-Dashboard-Summary.pdf');
});
};
// --- TABBING LOGIC ---
let currentTabIndex = 0;
const updateTabButtons = () => {
prevTabBtn.disabled = currentTabIndex === 0;
nextTabBtn.disabled = currentTabIndex === tabContents.length - 1;
};
const switchTab = (index) => {
tabButtons.forEach(btn => btn.classList.remove('active'));
tabContents.forEach(content => content.classList.remove('active'));
tabButtons[index].classList.add('active');
tabContents[index].classList.add('active');
currentTabIndex = index;
updateTabButtons();
};
tabButtons.forEach((button, index) => button.addEventListener('click', () => switchTab(index)));
prevTabBtn.addEventListener('click', () => { if (currentTabIndex > 0) switchTab(currentTabIndex - 1); });
nextTabBtn.addEventListener('click', () => { if (currentTabIndex < tabContents.length - 1) switchTab(currentTabIndex + 1); });
// --- INITIALIZATION ---
if (kpiContainer && addVitalsBtn && addApptBtn && addMedBtn && downloadPdfBtn) {
addVitalsBtn.addEventListener('click', () => handleAdd('vitals'));
addApptBtn.addEventListener('click', () => handleAdd('appointments'));
addMedBtn.addEventListener('click', () => handleAdd('medications'));
downloadPdfBtn.addEventListener('click', handleDownloadPdf);
renderAll();
updateTabButtons();
} else {
console.error("Essential dashboard elements could not be found.");
}
});