-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacprofile.php
More file actions
684 lines (606 loc) · 30.6 KB
/
Copy pathfacprofile.php
File metadata and controls
684 lines (606 loc) · 30.6 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<?php
// facility_profile_with_cert_beauty.php (Flat Able compact + Leaflet + BS4 DataTables)
include("assets/head/h.php"); // must start session and include $con (mysqli)
// include("assets/conn/db.php"); // uncomment if DB is not included above
if (!isset($_SESSION['u_facilityid'])) {
die("<div class='alert alert-danger m-3'>Access denied. Facility ID missing in session.</div>");
}
$fac_id = (int)$_SESSION['u_facilityid'];
$success = $error = $certMsg = "";
/* =========================
UPDATE Facility Record
========================= */
if (isset($_POST['save_facility']) && (int)($_POST['fac_id'] ?? 0) === $fac_id) {
$division_id = (int)($_POST['division_id'] ?? 0);
$dist_id = (int)($_POST['dist_id'] ?? 0);
$block_id = (int)($_POST['block_id'] ?? 0);
$Health_type = (int)($_POST['Health_facilty_type'] ?? 0);
$fac_name = trim($_POST['fac_name'] ?? '');
$lat = (float)($_POST['lat'] ?? 0);
$longit = (float)($_POST['longit'] ?? 0);
$NIN_no = trim($_POST['NIN_no'] ?? '');
if (!preg_match("/^[A-Za-z\s]+$/", $fac_name)) {
$error = "Facility Name should contain only letters and spaces.";
} elseif (!preg_match("/^\d{10}$/", $NIN_no)) {
$error = "NIN Number must be exactly 10 digits.";
}
if ($error === "") {
$division_name = $dist_name = $block_name = '';
if ($division_id) {
$stmt = $con->prepare("SELECT division_name FROM division WHERE iddivision=?");
$stmt->bind_param("i", $division_id);
$stmt->execute();
$stmt->bind_result($division_name);
$stmt->fetch();
$stmt->close();
}
if ($dist_id) {
$stmt = $con->prepare("SELECT Dist_name FROM dist_master WHERE Dist_id=?");
$stmt->bind_param("i", $dist_id);
$stmt->execute();
$stmt->bind_result($dist_name);
$stmt->fetch();
$stmt->close();
}
if ($block_id) {
$stmt = $con->prepare("SELECT block_name FROM block_master WHERE block_id=?");
$stmt->bind_param("i", $block_id);
$stmt->execute();
$stmt->bind_result($block_name);
$stmt->fetch();
$stmt->close();
}
$stmt = $con->prepare("UPDATE facilities
SET division_id=?, division=?, dist_id=?, Dist_Name=?, block_id=?, Block_Name=?,
fac_name=?, Health_facilty_type=?, lat=?, longit=?, NIN_no=?
WHERE fac_id=?");
$stmt->bind_param(
"isisissiddsi",
$division_id,
$division_name,
$dist_id,
$dist_name,
$block_id,
$block_name,
$fac_name,
$Health_type,
$lat,
$longit,
$NIN_no,
$fac_id
);
if ($stmt->execute()) { $success = "Facility details updated successfully."; }
else { $error = "Update failed: " . $stmt->error; }
$stmt->close();
}
}
/* =========================
ADD Certification Record
========================= */
if (isset($_POST['save_cert'])) {
$cert_type = trim($_POST['cert_type'] ?? '');
$cert_details = trim($_POST['cert_detailscol'] ?? '');
$cert_issue = trim($_POST['cert_issue'] ?? '');
$score = trim($_POST['score'] ?? '');
$lat_c = (float)($_POST['lat'] ?? 0);
$long_c = (float)($_POST['longi'] ?? 0);
$validity = $cert_issue ? date('Y-m-d', strtotime($cert_issue . ' +1 year')) : null;
$stmtF = $con->prepare("SELECT fac_name, Dist_Name, Block_Name, Health_facilty_type, dist_id, block_id FROM facilities WHERE fac_id=?");
$stmtF->bind_param("i", $fac_id);
$stmtF->execute();
$stmtF->bind_result($fac_name_db, $dist_name_db, $block_name_db, $fac_type_db, $dist_id_db, $block_id_db);
$stmtF->fetch();
$stmtF->close();
$stmt = $con->prepare("INSERT INTO cert_details
(dist, block, fac_name, fac_type, cert_type, cert_detailscol, cert_issue, validity, score, lat, longi, dist_id, block_id, fac_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param(
"sssssssssddiii",
$dist_name_db,
$block_name_db,
$fac_name_db,
$fac_type_db, // change to (int)$fac_type_db + 'i' if numeric column
$cert_type,
$cert_details,
$cert_issue,
$validity,
$score,
$lat_c,
$long_c,
$dist_id_db,
$block_id_db,
$fac_id
);
if ($stmt->execute()) { $certMsg = "Certification added successfully."; }
else { $certMsg = "Error adding certification: " . $stmt->error; }
$stmt->close();
}
/* =========================
FETCH Facility + Masters + Cert History
========================= */
$stmt = $con->prepare("SELECT * FROM facilities WHERE fac_id=?");
$stmt->bind_param("i", $fac_id);
$stmt->execute();
$res = $stmt->get_result();
$facility = $res->fetch_assoc();
$stmt->close();
if (!$facility) {
die("<div class='alert alert-warning m-3'>No facility found for this user.</div>");
}
$divisions = $con->query("SELECT iddivision, division_name FROM division ORDER BY division_name")->fetch_all(MYSQLI_ASSOC);
$districts = $con->query("SELECT Dist_id, Dist_name, division_id FROM dist_master ORDER BY Dist_name")->fetch_all(MYSQLI_ASSOC);
$blocks = $con->query("SELECT block_id, block_name, dist_id FROM block_master ORDER BY block_name")->fetch_all(MYSQLI_ASSOC);
$facilityTypes = $con->query("SELECT fac_type_id, facilities_type FROM facilities_type ORDER BY facilities_type")->fetch_all(MYSQLI_ASSOC);
$certHistory = $con->query("SELECT * FROM cert_details WHERE fac_id=" . (int)$fac_id . " ORDER BY cert_issue DESC");
$facTypeMap = [];
foreach ($facilityTypes as $ft) { $facTypeMap[(string)$ft['fac_type_id']] = $ft['facilities_type']; }
$facTypeName = $facTypeMap[(string)$facility['Health_facilty_type']] ?? '—';
?>
<!-- Extra vendor CSS for this page only -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" rel="stylesheet">
<!-- DataTables (Bootstrap 4 skin to match Flat Able) -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap4.min.css"/>
<!-- Leaflet -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin=""/>
<style>
/* Compact form controls (BS4) */
.form-group { margin-bottom: .6rem; }
.form-control, .custom-select {
height: calc(1.95rem + 2px);
padding: .25rem .5rem;
font-size: .85rem;
line-height: 1.2;
}
.form-control:read-only { background-color: #f8f9fa; }
.form-control-sm, .custom-select-sm { height: calc(1.8rem + 2px); font-size: .82rem; }
.floating-label { position: relative; }
.floating-label > label {
position: absolute; top: -0.55rem; left: .6rem; background: #fff; padding: 0 .25rem;
font-size: .70rem; color: #6c757d;
}
.pc-card { border-radius: 12px; box-shadow: 0 8px 28px rgba(15,23,42,.06); }
.pc-card .card-body { padding: 1rem 1rem 1.25rem; }
.leaflet-map { height: 220px; border-radius: 12px; overflow: hidden; border: 1px solid #e5e7eb; }
.btn-geo{ background:#0ea5e9; color:#fff; border:0; padding:.35rem .75rem; font-size:.85rem; }
.btn-save{ background:#0d6efd; color:#fff; border:0; padding:.4rem .9rem; font-size:.85rem; }
.btn-light { padding:.4rem .9rem; font-size:.85rem; }
.table thead th{ background:#f6f7fb; font-weight:600; }
.badge-muted { color:#6c757d; font-size:.8rem; }
</style>
<div class="pcoded-main-container mt-3">
<div class="pcoded-content">
<div class="d-flex align-items-center justify-content-between mb-3">
<h5 class="mb-0"><i class="fa-solid fa-hospital me-2"></i>Facility Profile</h5>
<div class="badge-muted">Logged facility ID: <strong><?= $fac_id ?></strong></div>
</div>
<?php if ($success): ?>
<div class="alert alert-success py-2 px-3 mb-3"><i class="fa-solid fa-circle-check mr-2"></i><?= htmlspecialchars($success) ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger py-2 px-3 mb-3"><i class="fa-solid fa-triangle-exclamation mr-2"></i><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if ($certMsg): ?>
<div class="alert alert-info py-2 px-3 mb-3"><i class="fa-solid fa-file-circle-plus mr-2"></i><?= htmlspecialchars($certMsg) ?></div>
<?php endif; ?>
<div class="card pc-card">
<div class="card-body">
<!-- BS4 tabs (use data-toggle, not data-bs-toggle) -->
<ul class="nav nav-tabs mb-3" id="facilityTabs" role="tablist">
<li class="nav-item">
<a class="nav-link active text-uppercase" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="true">
Profile
</a>
</li>
<li class="nav-item">
<a class="nav-link text-uppercase" id="certification-tab" data-toggle="tab" href="#certification" role="tab" aria-controls="certification" aria-selected="false">
Certification
</a>
</li>
</ul>
<div class="tab-content" id="facilityTabsContent">
<!-- PROFILE TAB -->
<div class="tab-pane fade show active" id="profile" role="tabpanel" aria-labelledby="profile-tab">
<div class="row">
<div class="col-12">
<div class="pc-card">
<div class="card-body">
<!-- View Mode: 3 fields per row -->
<div id="viewMode">
<div class="form-row">
<div class="form-group col-md-4">
<div class="floating-label">
<input type="text" class="form-control form-control-sm" value="<?= htmlspecialchars($facility['state_name']) ?>" readonly>
<label>State</label>
</div>
</div>
<div class="form-group col-md-4">
<div class="floating-label">
<input type="text" class="form-control form-control-sm" value="<?= htmlspecialchars($facility['division']) ?>" readonly>
<label>Division</label>
</div>
</div>
<div class="form-group col-md-4">
<div class="floating-label">
<input type="text" class="form-control form-control-sm" value="<?= htmlspecialchars($facility['Dist_Name']) ?>" readonly>
<label>District</label>
</div>
</div>
<div class="form-group col-md-4">
<div class="floating-label">
<input type="text" class="form-control form-control-sm" value="<?= htmlspecialchars($facility['Block_Name']) ?>" readonly>
<label>Block</label>
</div>
</div>
<div class="form-group col-md-4">
<div class="floating-label">
<input type="text" class="form-control form-control-sm" value="<?= htmlspecialchars($facility['fac_name']) ?>" readonly>
<label>Facility Name</label>
</div>
</div>
<div class="form-group col-md-4">
<div class="floating-label">
<input type="text" class="form-control form-control-sm" value="<?= htmlspecialchars($facTypeName) ?>" readonly>
<label>Facility Type</label>
</div>
</div>
<div class="form-group col-md-4">
<div class="floating-label">
<input type="text" class="form-control form-control-sm" value="<?= htmlspecialchars($facility['NIN_no']) ?>" readonly>
<label>NIN Number</label>
</div>
</div>
<div class="form-group col-md-4">
<div class="floating-label">
<input type="text" class="form-control form-control-sm" value="<?= htmlspecialchars($facility['lat']) ?>" readonly>
<label>Latitude</label>
</div>
</div>
<div class="form-group col-md-4">
<div class="floating-label">
<input type="text" class="form-control form-control-sm" value="<?= htmlspecialchars($facility['longit']) ?>" readonly>
<label>Longitude</label>
</div>
</div>
</div>
<button id="editBtn" class="btn btn-outline-primary btn-sm mt-2">
<i class="fa-solid fa-pen-to-square mr-1"></i> Edit
</button>
</div>
<!-- Edit Mode: 3 fields per row -->
<div id="editMode" style="display:none;">
<form method="post" action="" autocomplete="off" novalidate>
<input type="hidden" name="fac_id" value="<?= $fac_id ?>">
<div class="form-row">
<div class="form-group col-md-4">
<label class="mb-1 small">State (Read-only)</label>
<input class="form-control form-control-sm" type="text" name="state_name"
value="<?= htmlspecialchars($facility['state_name']) ?>" readonly>
</div>
<div class="form-group col-md-4">
<label class="mb-1 small">Division</label>
<select class="custom-select custom-select-sm" name="division_id" id="division_id" required>
<option value="">-- Select Division --</option>
<?php foreach ($divisions as $d): ?>
<option value="<?= $d['iddivision'] ?>" <?= ($d['iddivision'] == $facility['division_id']) ? 'selected' : '' ?>>
<?= htmlspecialchars($d['division_name']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group col-md-4">
<label class="mb-1 small">District</label>
<select class="custom-select custom-select-sm" name="dist_id" id="dist_id" required>
<option value="">-- Select District --</option>
</select>
</div>
<div class="form-group col-md-4">
<label class="mb-1 small">Block</label>
<select class="custom-select custom-select-sm" name="block_id" id="block_id" required>
<option value="">-- Select Block --</option>
</select>
</div>
<div class="form-group col-md-4">
<label class="mb-1 small">Facility Type</label>
<select class="custom-select custom-select-sm" name="Health_facilty_type" id="fac_type" required>
<option value="">-- Select Facility Type --</option>
<?php foreach ($facilityTypes as $ft): ?>
<option value="<?= $ft['fac_type_id'] ?>" <?= ($ft['fac_type_id'] == $facility['Health_facilty_type']) ? 'selected' : '' ?>>
<?= htmlspecialchars($ft['facilities_type']) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group col-md-4">
<label class="mb-1 small">Facility Name</label>
<input class="form-control form-control-sm" type="text" name="fac_name"
value="<?= htmlspecialchars($facility['fac_name']) ?>"
required pattern="[A-Za-z\s]+" title="Only letters and spaces are allowed">
</div>
<div class="form-group col-md-4">
<label class="mb-1 small">NIN Number (10 digits)*</label>
<input class="form-control form-control-sm" type="text" name="NIN_no" id="NIN_no"
maxlength="10" minlength="10" pattern="\d{10}"
title="Enter 10 digits only"
value="<?= htmlspecialchars($facility['NIN_no']) ?>" required
oninput="this.value=this.value.replace(/[^0-9]/g,'').slice(0,10)">
</div>
<div class="form-group col-md-4">
<label class="mb-1 small">Latitude</label>
<input class="form-control form-control-sm" type="text" name="lat" id="lat"
value="<?= htmlspecialchars($facility['lat']) ?>" readonly>
</div>
<div class="form-group col-md_4 col-md-4">
<label class="mb-1 small">Longitude</label>
<input class="form-control form-control-sm" type="text" name="longit" id="longit"
value="<?= htmlspecialchars($facility['longit']) ?>" readonly>
</div>
<div class="form-group col-12">
<div id="editMap" class="leaflet-map"></div>
<div class="mt-2 d-flex align-items-center">
<button type="button" id="useGeo" class="btn btn-geo mr-2">
<i class="fa-solid fa-location-crosshairs mr-1"></i> Use Current Location
</button>
<small class="text-muted">Click map or drag marker to update coordinates.</small>
</div>
</div>
</div>
<div class="mt-2 d-flex">
<button type="submit" name="save_facility" class="btn btn-save mr-2">
<i class="fa-solid fa-floppy-disk mr-1"></i> Save
</button>
<button type="button" id="cancelEdit" class="btn btn-light">Cancel</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- CERTIFICATION TAB -->
<div class="tab-pane fade" id="certification" role="tabpanel" aria-labelledby="certification-tab">
<div class="row">
<div class="col-lg-5">
<div class="pc-card">
<div class="card-body">
<h6 class="mb-2">Add Certification</h6>
<form method="post">
<div class="form-row">
<div class="form-group col-12">
<label class="mb-1 small">Certification Type</label>
<select name="cert_type" class="custom-select custom-select-sm" required>
<option value="">Select Certification Type</option>
<option value="National">National</option>
<option value="State">State</option>
</select>
</div>
<div class="form-group col-12">
<label class="mb-1 small">Certification Details</label>
<input type="text" name="cert_detailscol" class="form-control form-control-sm" required>
</div>
<div class="form-group col-md-6">
<label class="mb-1 small">Issue Date</label>
<input type="date" name="cert_issue" class="form-control form-control-sm" required>
</div>
<div class="form-group col-md-6">
<label class="mb-1 small">Score</label>
<input type="number" step="0.01" name="score" class="form-control form-control-sm" required>
</div>
<div class="form-group col-md-6">
<label class="mb-1 small">Latitude</label>
<input type="text" id="cert_lat" name="lat" class="form-control form-control-sm" value="<?= htmlspecialchars($facility['lat']) ?>" readonly>
</div>
<div class="form-group col-md-6">
<label class="mb-1 small">Longitude</label>
<input type="text" id="cert_long" name="longi" class="form-control form-control-sm" value="<?= htmlspecialchars($facility['longit']) ?>" readonly>
</div>
<div class="form-group col-12">
<div id="certMap" class="leaflet-map"></div>
<div class="mt-2 d-flex align-items-center">
<button type="button" id="useGeoCert" class="btn btn-geo mr-2">
<i class="fa-solid fa-location-dot mr-1"></i> Get Location
</button>
<small class="text-muted">Click map or drag marker to set certification coordinates.</small>
</div>
</div>
<div class="form-group col-12">
<button type="submit" name="save_cert" class="btn btn-success btn-sm">
<i class="fa-solid fa-plus mr-1"></i> Add Certification
</button>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="col-lg-7">
<div class="pc-card">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center mb-2">
<h6 class="mb-0">Previous Certifications</h6>
<small class="text-muted">Search & sort</small>
</div>
<div class="table-responsive">
<table id="certTable" class="table table-striped table-bordered table-sm">
<thead>
<tr>
<th>#</th>
<th>Type</th>
<th>Details</th>
<th>Issue Date</th>
<th>Validity</th>
<th>Score</th>
</tr>
</thead>
<tbody>
<?php if ($certHistory && $certHistory->num_rows > 0): $i = 1; ?>
<?php while ($c = $certHistory->fetch_assoc()): ?>
<tr>
<td><?= $i++ ?></td>
<td><?= htmlspecialchars($c['cert_type']) ?></td>
<td><?= htmlspecialchars($c['cert_detailscol']) ?></td>
<td><?= htmlspecialchars($c['cert_issue']) ?></td>
<td><?= htmlspecialchars($c['validity']) ?></td>
<td><?= htmlspecialchars($c['score']) ?></td>
</tr>
<?php endwhile; ?>
<?php endif; // IMPORTANT: no fallback row with colspan ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div><!-- /CERTIFICATION TAB -->
</div><!-- /tab-content -->
</div>
</div>
</div>
</div>
<?php include("assets/head/f.php"); ?>
<!-- Page JS (use BS4-compatible libs; Flat Able already loads jQuery & bootstrap.min.js) -->
<!-- DataTables core + BS4 integration -->
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap4.min.js"></script>
<!-- Leaflet -->
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" integrity="sha256-20nQC1N0C6G2Z3e1rV0CcdN1S9GKa3I1C2f2P3p3p3s=" crossorigin=""></script>
<script>
// preload master arrays for cascading selects
const districts = <?= json_encode($districts) ?>;
const blocks = <?= json_encode($blocks) ?>;
const currentDist = "<?= $facility['dist_id'] ?>";
const currentBlock = "<?= $facility['block_id'] ?>";
function loadDistricts(divId) {
const distSel = document.getElementById('dist_id');
if (!distSel) return;
distSel.innerHTML = '<option value="">-- Select District --</option>';
const filtered = districts.filter(d => String(d.division_id) === String(divId));
filtered.forEach(d => {
const opt = document.createElement('option');
opt.value = d.Dist_id;
opt.textContent = d.Dist_name;
if (String(d.Dist_id) === String(currentDist)) opt.selected = true;
distSel.appendChild(opt);
});
const chosen = distSel.value || (filtered[0]?.Dist_id ?? "");
loadBlocks(chosen);
}
function loadBlocks(distId) {
const blkSel = document.getElementById('block_id');
if (!blkSel) return;
blkSel.innerHTML = '<option value="">-- Select Block --</option>';
blocks.filter(b => String(b.dist_id) === String(distId)).forEach(b => {
const opt = document.createElement('option');
opt.value = b.block_id;
opt.textContent = b.block_name;
if (String(b.block_id) === String(currentBlock)) opt.selected = true;
blkSel.appendChild(opt);
});
}
// Leaflet helper
function initLeafletMap(containerId, lat, lng, onChange) {
const map = L.map(containerId, { scrollWheelZoom: true }).setView([lat, lng], 15);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap'
}).addTo(map);
const marker = L.marker([lat, lng], { draggable: true }).addTo(map);
function update(latlng) {
marker.setLatLng(latlng);
if (onChange) onChange(latlng);
}
map.on('click', (e) => update(e.latlng));
marker.on('dragend', (e) => update(e.target.getLatLng()));
// fix for hidden tab rendering
setTimeout(() => { map.invalidateSize(); }, 300);
return { map, marker, update };
}
$(function() {
// Hash support for BS4 tabs
const hash = window.location.hash;
if (hash && document.querySelector(`[href="${hash}"]`)) {
$('a[href="' + hash + '"]').tab('show');
}
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
if (e.target.getAttribute('href') === '#certification') {
if (window._certMap?.map) window._certMap.map.invalidateSize();
}
if (e.target.getAttribute('href') === '#profile') {
if (window._editMap?.map) window._editMap.map.invalidateSize();
}
});
// Toggle view/edit mode
$('#editBtn').on('click', function(){
$('#viewMode').hide();
$('#editMode').fadeIn(120, () => {
if (window._editMap?.map) window._editMap.map.invalidateSize();
});
});
$('#cancelEdit').on('click', function(){
$('#editMode').hide();
$('#viewMode').fadeIn(120);
});
// Cascading selects
const divisionEl = document.getElementById('division_id');
if (divisionEl) {
divisionEl.addEventListener('change', e => { loadDistricts(e.target.value); });
if (divisionEl.value) loadDistricts(divisionEl.value);
}
const distEl = document.getElementById('dist_id');
if (distEl) distEl.addEventListener('change', e => loadBlocks(e.target.value));
// Initial map centers
const initLat = parseFloat('<?= $facility['lat'] ?: 25.0 ?>') || 25.0;
const initLong = parseFloat('<?= $facility['longit'] ?: 85.0 ?>') || 85.0;
// Edit map -> updates #lat and #longit
window._editMap = initLeafletMap('editMap', initLat, initLong, (ll) => {
$('#lat').val(ll.lat.toFixed(6));
$('#longit').val(ll.lng.toFixed(6));
});
// Geolocation for edit map
$('#useGeo').on('click', function() {
if (!navigator.geolocation) return alert('Geolocation not supported');
navigator.geolocation.getCurrentPosition(function(pos) {
const lat = pos.coords.latitude;
const lon = pos.coords.longitude;
window._editMap.update({ lat, lng: lon });
$('#lat').val(lat.toFixed(6));
$('#longit').val(lon.toFixed(6));
window._editMap.map.setView([lat, lon], 16);
}, function(err) {
alert('Error: ' + err.message);
}, { enableHighAccuracy: true, timeout: 10000 });
});
// Certification map -> updates #cert_lat and #cert_long
const certLat = parseFloat($('#cert_lat').val()) || initLat;
const certLong = parseFloat($('#cert_long').val()) || initLong;
window._certMap = initLeafletMap('certMap', certLat, certLong, (ll) => {
$('#cert_lat').val(ll.lat.toFixed(6));
$('#cert_long').val(ll.lng.toFixed(6));
});
// Geolocation for cert map
$('#useGeoCert').onClick = null;
$('#useGeoCert').on('click', function() {
if (!navigator.geolocation) return alert('Geolocation not supported');
navigator.geolocation.getCurrentPosition(function(pos) {
const lat = pos.coords.latitude;
const lon = pos.coords.longitude;
window._certMap.update({ lat, lng: lon });
$('#cert_lat').val(lat.toFixed(6));
$('#cert_long').val(lon.toFixed(6));
window._certMap.map.setView([lat, lon], 16);
}, function(err) {
alert('Error: ' + err.message);
}, { enableHighAccuracy: true, timeout: 10000 });
});
// DataTable (BS4)
$('#certTable').DataTable({
pageLength: 8,
lengthChange: false,
ordering: true,
order: [[3, 'desc']],
language: { search: "", searchPlaceholder: "Search certifications..." },
dom: '<"row mb-2"<"col-sm-6"f><"col-sm-6 text-right"l>>t<"row mt-2"<"col-sm-6"i><"col-sm-6"p>>'
});
});
</script>