-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateappointment.js
More file actions
216 lines (178 loc) · 8.23 KB
/
updateappointment.js
File metadata and controls
216 lines (178 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
document.addEventListener('DOMContentLoaded', () => {
let dbAppointments;
let dbMedicines;
let dbPatients;
// Open AppointmentsDB
function openAppointmentsDB() {
const request = indexedDB.open('AppointmentsDB', 2);
request.onsuccess = function (event) {
console.log('AppointmentsDB opened.');
dbAppointments = event.target.result;
loadAppointments();
};
request.onerror = function (event) {
console.error('Error opening AppointmentsDB:', event.target.error);
};
}
// Open MedicinesDB
function openMedicinesDB() {
const request = indexedDB.open('MedicinesDB', 1);
request.onsuccess = function (event) {
console.log('MedicinesDB opened.');
dbMedicines = event.target.result;
loadMedicines();
};
request.onerror = function (event) {
console.error('Error opening MedicinesDB:', event.target.error);
};
}
// Open PatientsDB
function openPatientsDB() {
const request = indexedDB.open('patientsDB', 5); // Ensure version matches your actual DB version
request.onsuccess = function (event) {
console.log('PatientsDB opened.');
dbPatients = event.target.result;
};
request.onerror = function (event) {
console.error('Error opening PatientsDB:', event.target.error);
};
}
// Load appointments with patient names
function loadAppointments() {
const appointmentSelect = document.getElementById('appointmentSelect');
if (!appointmentSelect) {
console.error('Appointment dropdown not found.');
return;
}
const transaction = dbAppointments.transaction('appointments', 'readonly');
const objectStore = transaction.objectStore('appointments');
objectStore.openCursor().onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
const appointment = cursor.value;
const patientId = appointment.patient_id;
// Fetch patient details after ensuring PatientsDB is loaded
if (dbPatients) {
fetchPatientName(patientId, function(patientFullName) {
const option = document.createElement('option');
option.value = appointment.id;
option.textContent = `Appointment ID: ${appointment.id} - Patient: ${patientFullName}`;
appointmentSelect.appendChild(option);
});
} else {
console.error('PatientsDB is not loaded yet.');
}
cursor.continue();
}
};
transaction.onerror = function (event) {
console.error('Error loading appointments:', event.target.error);
};
}
// Fetch patient name (First and Last) from PatientsDB
function fetchPatientName(patientId, callback) {
const transaction = dbPatients.transaction('patients', 'readonly');
const objectStore = transaction.objectStore('patients');
const request = objectStore.get(patientId);
request.onsuccess = function (event) {
const patient = event.target.result;
if (patient) {
// Concatenate First and Last name to get full name
const patientFullName = `${patient.First} ${patient.Last}`;
console.log('Patient found:', patientFullName); // Log the full name
callback(patientFullName); // Pass the full name to the callback
} else {
console.error('Patient not found.');
callback('Unknown Patient'); // Fallback in case patient not found
}
};
request.onerror = function (event) {
console.error('Error fetching patient name:', event.target.error);
};
}
// Load medicines into the dropdown
function loadMedicines() {
const medicineSelect = document.getElementById('medicineSelect');
if (!medicineSelect) {
console.error('Medicine dropdown not found.');
return;
}
const transaction = dbMedicines.transaction('medicines', 'readonly');
const objectStore = transaction.objectStore('medicines');
objectStore.openCursor().onsuccess = function (event) {
const cursor = event.target.result;
if (cursor) {
const medicine = cursor.value;
// Log the medicine name to check if it's populated
console.log('Medicine:', medicine);
const option = document.createElement('option');
option.value = medicine.id;
// Check if the name is valid, and set text accordingly
if (medicine.Drug) {
option.textContent = medicine.Drug;
} else {
option.textContent = 'Unknown Medicine'; // Fallback if no name
}
medicineSelect.appendChild(option);
cursor.continue();
}
};
transaction.onerror = function (event) {
console.error('Error loading medicines:', event.target.error);
};
}
// Add selected medicine and notes to the selected appointment
document.getElementById('addMedicinesForm').addEventListener('submit', function (event) {
event.preventDefault();
const appointmentId = parseInt(document.getElementById('appointmentSelect').value);
const medicineId = parseInt(document.getElementById('medicineSelect').value);
const appointmentNotes = document.getElementById('appointmentNotes').value; // Get the notes from the textarea
// Fetch the selected appointment
const transaction = dbAppointments.transaction('appointments', 'readwrite');
const objectStore = transaction.objectStore('appointments');
const request = objectStore.get(appointmentId);
request.onsuccess = function (event) {
const appointment = event.target.result;
if (!appointment.medicines) {
appointment.medicines = []; // Initialize medicines array if not present
}
// Add the selected medicine to the medicines array
appointment.medicines.push(medicineId);
// Add notes to the appointment
appointment.notes = appointmentNotes; // Add the notes field
// Update the appointment in the database
const updateRequest = objectStore.put(appointment);
updateRequest.onsuccess = function () {
console.log(`Medicine ID ${medicineId} added to Appointment ID ${appointmentId}.`);
displayAppointmentDetails(appointment); // Display updated appointment details
// Append success message to the existing statusMessage element
const statusMessage = document.getElementById('message');
if (statusMessage) {
statusMessage.textContent = `Appointment ID ${appointmentId} successfully updated.`;
statusMessage.style.display = 'block';
}
};
updateRequest.onerror = function (event) {
console.error('Error updating appointment:', event.target.error);
};
};
request.onerror = function (event) {
console.error('Error fetching appointment:', event.target.error);
};
});
// Display updated appointment details including notes
function displayAppointmentDetails(appointment) {
const appointmentDetails = document.getElementById('appointmentDetails');
appointmentDetails.innerHTML = `
<p>Appointment ID: ${appointment.id}</p>
<p>Patient ID: ${appointment.patient_id}</p>
<p>Doctor ID: ${appointment.doctor_id}</p>
<p>Medicines: ${appointment.medicines.join(', ')}</p>
<p><strong>Notes:</strong> ${appointment.notes || 'No notes added'}</p>
`;
}
// Initialize all databases when the page loads
openAppointmentsDB();
openMedicinesDB();
openPatientsDB();
});