Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
290 lines (242 loc) · 8.97 KB
/
Copy pathscript.js
File metadata and controls
290 lines (242 loc) · 8.97 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const DB_NAME = "PatientDatabase";
const DB_VERSION = 5;
const STORE_PATIENTS = "patients";
const STORE_MOBILE = "mobileIndex";
let db = null;
function openDatabase() {
return new Promise((resolve, reject) => {
const req = indexedDB.open(DB_NAME, DB_VERSION);
req.onupgradeneeded = (e) => {
const d = e.target.result;
if (!d.objectStoreNames.contains(STORE_PATIENTS)) {
d.createObjectStore(STORE_PATIENTS, { keyPath: "id" });
}
if (!d.objectStoreNames.contains(STORE_MOBILE)) {
d.createObjectStore(STORE_MOBILE, { keyPath: "mobile" });
}
};
req.onsuccess = (e) => {
db = e.target.result;
resolve(db);
};
req.onerror = (e) => {
console.error("IndexedDB error:", e.target.error);
reject(e.target.error);
};
});
}
function txStore(storeName, mode = "readonly") {
if (!db) throw new Error("Database is not open");
const tx = db.transaction(storeName, mode);
return tx.objectStore(storeName);
}
function putPatient(patient) {
return new Promise((res, rej) => {
try {
const store = txStore(STORE_PATIENTS, "readwrite");
const r = store.put(patient);
r.onsuccess = () => res(true);
r.onerror = (e) => rej(e.target.error);
} catch (err) {
rej(err);
}
});
}
function putMobileMapping(mobile, id) {
return new Promise((res, rej) => {
try {
const store = txStore(STORE_MOBILE, "readwrite");
const r = store.put({ mobile, id });
r.onsuccess = () => res(true);
r.onerror = (e) => rej(e.target.error);
} catch (err) {
rej(err);
}
});
}
function getPatientById(id) {
return new Promise((res, rej) => {
try {
const store = txStore(STORE_PATIENTS, "readonly");
const r = store.get(id);
r.onsuccess = () => res(r.result || null);
r.onerror = (e) => rej(e.target.error);
} catch (err) {
rej(err);
}
});
}
function getMobileRecord(mobile) {
return new Promise((res, rej) => {
try {
const store = txStore(STORE_MOBILE, "readonly");
const r = store.get(mobile);
r.onsuccess = () => res(r.result || null);
r.onerror = (e) => rej(e.target.error);
} catch (err) {
rej(err);
}
});
}
function makeCandidateId() {
return Math.floor(100000000000 + Math.random() * 900000000000).toString();
}
async function generateUniquePatientId(maxAttempts = 12) {
for (let i = 0; i < maxAttempts; i++) {
const candidate = makeCandidateId();
const existing = await getPatientById(candidate);
if (!existing) return candidate;
}
return Date.now().toString().slice(-12);
}
function show(elem) { if (elem) elem.classList.remove("hidden"); }
function hide(elem) { if (elem) elem.classList.add("hidden"); }
async function initApp() {
try {
await openDatabase();
} catch (err) {
console.error("DB open failed:", err);
alert("Unable to open database. Check console.");
return;
}
const searchType = document.getElementById("searchType");
const searchInputArea = document.getElementById("searchInputArea");
const searchLabel = document.getElementById("searchLabel");
const searchInput = document.getElementById("searchInput");
const searchForm = document.getElementById("searchForm");
if (searchInputArea) hide(searchInputArea);
if (searchType) {
searchType.addEventListener("change", () => {
if (!searchType.value) {
if (searchInputArea) hide(searchInputArea);
return;
}
if (searchInputArea) show(searchInputArea);
if (searchInput) searchInput.value = "";
if (searchLabel) {
if (searchType.value === "id") searchLabel.textContent = "Enter Patient ID:";
else searchLabel.textContent = "Enter Mobile Number:";
} else {
if (searchInput) {
searchInput.placeholder = searchType.value === "id" ? "Enter Patient ID" : "Enter Mobile Number";
}
}
});
}
if (searchForm) {
searchForm.addEventListener("submit", async (e) => {
e.preventDefault();
const type = searchType ? searchType.value : null;
const val = searchInput ? (searchInput.value || "").trim() : "";
if (!type) return alert("Please select search type first.");
try {
let patient = null;
if (type === "id") {
patient = await getPatientById(val);
if (!patient) return alert("No patient found for that ID.");
} else {
if (!/^[6-9]\d{9}$/.test(val)) return alert("Enter a valid 10-digit mobile number.");
const rec = await getMobileRecord(val);
if (!rec) return alert("No patient found for that mobile number.");
patient = await getPatientById(rec.id);
if (!patient) return alert("Patient record missing.");
}
const enteredPassword = prompt("Enter patient password:");
if (enteredPassword === null) return; // user cancelled
if (!enteredPassword) return alert("Password required.");
if (patient.password !== enteredPassword) {
return alert("Incorrect password! Access denied.");
}
displayPatient(patient);
} catch (err) {
console.error("Search error:", err);
alert("Search error. See console.");
}
});
}
const registerForm = document.getElementById("registerForm");
const regNameEl = document.getElementById("regName");
const regAgeEl = document.getElementById("regAge");
const regGenderEl = document.getElementById("regGender");
const regAllergiesEl = document.getElementById("regAllergies");
const regMedicationsEl = document.getElementById("regMedications");
const regMobileEl = document.getElementById("regMobile");
const regPasswordEl = document.getElementById("regPassword");
const regConfirmPasswordEl = document.getElementById("regConfirmPassword");
const mobileWarning = document.getElementById("mobileWarning");
const newIdBox = document.getElementById("newIdBox");
const generatedIdEl = document.getElementById("generatedId");
if (registerForm) {
registerForm.addEventListener("submit", async (e) => {
e.preventDefault();
const name = regNameEl ? (regNameEl.value || "").trim() : "";
const age = regAgeEl ? (regAgeEl.value || "").trim() : "";
const gender = regGenderEl ? (regGenderEl.value || "").trim() : "";
const allergies = regAllergiesEl ? (regAllergiesEl.value || "").trim() : "";
const medications = regMedicationsEl ? (regMedicationsEl.value || "").trim() : "";
const mobile = regMobileEl ? (regMobileEl.value || "").trim() : "";
const password = regPasswordEl ? (regPasswordEl.value || "") : "";
const confirm = regConfirmPasswordEl ? (regConfirmPasswordEl.value || "") : "";
if (!name || !age) {
return alert("Please enter at least Name and Age.");
}
if (!/^[6-9]\d{9}$/.test(mobile)) {
return alert("Enter a valid 10-digit mobile number.");
}
if (!password || !confirm) {
return alert("Please enter and confirm the password.");
}
if (password !== confirm) {
return alert("Passwords do not match.");
}
try {
const existing = await getMobileRecord(mobile);
if (existing) {
if (mobileWarning) mobileWarning.style.display = "block";
return;
}
if (mobileWarning) mobileWarning.style.display = "none";
const newId = await generateUniquePatientId();
const patient = {
id: newId,
name,
age,
gender,
allergies,
medications,
mobile,
password,
reports: "",
scans: ""
};
await putPatient(patient);
await putMobileMapping(mobile, newId);
if (generatedIdEl) generatedIdEl.textContent = newId;
if (newIdBox) show(newIdBox);
registerForm.reset();
} catch (err) {
console.error("Registration error:", err);
alert("Failed to register patient. See console.");
}
});
}
function displayPatient(patient) {
const set = (id, val) => {
const el = document.getElementById(id);
if (el) el.textContent = val ?? "";
};
set("patientIdDisplay", patient.id);
set("name", patient.name);
set("age", patient.age);
set("gender", patient.gender);
set("allergies", patient.allergies);
set("medications", patient.medications);
set("reports", patient.reports || "No reports");
set("scans", patient.scans || "No scans");
const pd = document.getElementById("patientData");
if (pd) show(pd);
}
}
window.addEventListener("DOMContentLoaded", () => {
initApp().catch((err) => console.error("initApp failed:", err));
});