File size: 37,349 Bytes
278d806
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MDC Warehouse Hazard Tracker</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
    <style>
        .logo-container {
            height: 150px;
            overflow: hidden;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #f3f4f6;
            border: 1px dashed #ccc;
        }
        .logo-container img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }
        .hazard-table {
            width: 100%;
            border-collapse: collapse;
        }
        .hazard-table th, .hazard-table td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .hazard-table th {
            background-color: #f2f2f2;
        }
        .hazard-table tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .hazard-table tr:hover {
            background-color: #f1f1f1;
        }
        .chart-container {
            position: relative;
            margin: auto;
            height: 300px;
            width: 100%;
        }
        .status-open {
            background-color: #fef3c7;
            color: #92400e;
        }
        .status-closed {
            background-color: #d1fae5;
            color: #065f46;
        }
        .status-pending {
            background-color: #e0e7ff;
            color: #3730a3;
        }
        .status-ongoing {
            background-color: #fee2e2;
            color: #991b1b;
        }
        .big-text {
            font-size: 1.2rem;
            font-weight: bold;
        }
    </style>
</head>
<body class="bg-gray-50 p-4">
    <div class="max-w-7xl mx-auto bg-white rounded-lg shadow-md p-6">
        <div class="flex justify-between items-center mb-6">
            <div class="flex items-center">
                <div class="logo-container w-48 mr-4" id="logoContainer">
                    <img id="logoPreview" src="" alt="Logo" class="hidden">
                </div>
                <h1 class="text-3xl font-bold text-green-800">MDC Warehouse Hazard Tracker</h1>
            </div>
            <div>
                <input type="file" id="logoUpload" accept="image/*" class="hidden">
                <button onclick="document.getElementById('logoUpload').click()" class="bg-gray-200 hover:bg-gray-300 text-gray-800 font-bold py-2 px-4 rounded inline-flex items-center">
                    <svg class="w-4 h-4 mr-2" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                        <path fill-rule="evenodd" d="M4 3a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V5a2 2 0 00-2-2H4zm12 12H4l4-8 3 6 2-4 3 6z" clip-rule="evenodd"></path>
                    </svg>
                    Upload Logo
                </button>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
            <div class="bg-white p-4 rounded-lg shadow">
                <h2 class="text-xl font-semibold mb-4">Top 3 Most Reported Hazards</h2>
                <div class="chart-container">
                    <canvas id="topHazardsChart"></canvas>
                </div>
            </div>
            <div class="bg-white p-4 rounded-lg shadow">
                <h2 class="text-xl font-semibold mb-4">Hazard Status by Area</h2>
                <div class="chart-container">
                    <canvas id="areaStatusChart"></canvas>
                </div>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
            <div class="bg-white p-4 rounded-lg shadow">
                <h2 class="text-xl font-semibold mb-4">Areas with Most Reports</h2>
                <div class="chart-container">
                    <canvas id="areaReportsChart"></canvas>
                </div>
            </div>
            <div class="bg-white p-4 rounded-lg shadow">
                <h2 class="text-xl font-semibold mb-4">Areas with Most Corrected Hazards</h2>
                <div class="chart-container">
                    <canvas id="areaCorrectedChart"></canvas>
                </div>
            </div>
        </div>

        <div class="mb-8">
            <h2 class="text-2xl font-bold mb-4 text-green-800">Hazard Report Summary</h2>
            <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
                <div class="bg-gray-100 p-4 rounded-lg">
                    <h3 class="font-semibold mb-2">Total Reports</h3>
                    <p class="big-text" id="totalReports">0</p>
                </div>
                <div class="bg-gray-100 p-4 rounded-lg">
                    <h3 class="font-semibold mb-2">Open</h3>
                    <p class="big-text" id="openReports">0</p>
                </div>
                <div class="bg-gray-100 p-4 rounded-lg">
                    <h3 class="font-semibold mb-2">Closed</h3>
                    <p class="big-text" id="closedReports">0</p>
                </div>
                <div class="bg-gray-100 p-4 rounded-lg">
                    <h3 class="font-semibold mb-2">Pending/Ongoing</h3>
                    <p class="big-text" id="pendingReports">0</p>
                </div>
            </div>
        </div>

        <div class="mb-8">
            <h2 class="text-2xl font-bold mb-4 text-green-800">Add New Hazard Report</h2>
            <form id="hazardForm" class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <label class="block text-gray-700 font-medium mb-2" for="reportDate">Date of Report</label>
                    <input type="date" id="reportDate" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500" required>
                </div>
                <div>
                    <label class="block text-gray-700 font-medium mb-2" for="hazardType">Hazard/Issue Observed</label>
                    <select id="hazardType" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500" required>
                        <option value="">Select Hazard Type</option>
                        <option value="MHE Related">MHE Related</option>
                        <option value="Fire Hazard">Fire Hazard</option>
                        <option value="WEM Related">WEM Related</option>
                        <option value="GWP Related">GWP Related</option>
                        <option value="Not wearing PPE">Not wearing Personal protective Equipment</option>
                        <option value="Specialized activity without permit">Performing Specialized activity without work permit</option>
                        <option value="Dilapidated Pallet">Dilapidated Pallet</option>
                        <option value="Kissing Product">Kissing Product</option>
                        <option value="Over-extended stacks">Over-extended stacks</option>
                        <option value="Unauthorized Operation of MHE">Unauthorized Operation of MHE</option>
                        <option value="Unsecured Stacks">Unsecured Stacks</option>
                        <option value="Push-Pull Spotter without tool">Push-Pull Spotter without tool</option>
                        <option value="Improper placement/Unattended Jack pallet">Improper placement / Unattended Jack pallet</option>
                        <option value="Uncleared Rack Guards">Uncleared Rack Guards</option>
                        <option value="Maximum Pallet Stacking">Maximum Pallet Stacking</option>
                        <option value="Emergency Preparedness (EPRT) Related">Emergency Preparedness (EPRT) Related</option>
                        <option value="Spillage/Leakage">Spillage / Leakage</option>
                        <option value="Others">Others</option>
                        <option value="Put away without Stretch film">Put away without Stretch film</option>
                        <option value="Electrocution Hazard">Electrocution Hazard</option>
                        <option value="Unattended Safety Tools and Equipment">Unattended Safety Tools and Equipment</option>
                    </select>
                </div>
                <div class="md:col-span-2">
                    <label class="block text-gray-700 font-medium mb-2" for="hazardDetails">Complete Details of Hazard</label>
                    <textarea id="hazardDetails" rows="3" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500" required></textarea>
                </div>
                <div>
                    <label class="block text-gray-700 font-medium mb-2" for="status">Status</label>
                    <select id="status" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500" required>
                        <option value="">Select Status</option>
                        <option value="Open">Open</option>
                        <option value="Closed">Closed</option>
                        <option value="Pending">Pending</option>
                        <option value="Ongoing">Ongoing</option>
                    </select>
                </div>
                <div>
                    <label class="block text-gray-700 font-medium mb-2" for="area">Area</label>
                    <select id="area" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500" required>
                        <option value="">Select Area</option>
                        <option value="Domestic">Domestic</option>
                        <option value="Export">Export</option>
                        <option value="QMS">QMS</option>
                        <option value="Security">Security</option>
                        <option value="PMD">PMD</option>
                        <option value="Others">Others</option>
                    </select>
                </div>
                <div class="md:col-span-2 flex justify-end space-x-4">
                    <button type="submit" class="bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-6 rounded">
                        Add Report
                    </button>
                    <button type="reset" class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-6 rounded">
                        Clear
                    </button>
                </div>
            </form>
        </div>

        <div class="mb-8">
            <div class="flex justify-between items-center mb-4">
                <h2 class="text-2xl font-bold text-green-800">Hazard Reports</h2>
                <div class="flex space-x-2">
                    <button id="resetFilters" class="bg-gray-500 hover:bg-gray-600 text-white font-bold py-2 px-4 rounded inline-flex items-center">
                        <svg class="w-4 h-4 mr-2" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clip-rule="evenodd"></path>
                        </svg>
                        Reset Filters
                    </button>
                    <button id="exportExcel" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded inline-flex items-center">
                        <svg class="w-4 h-4 mr-2" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd" d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd"></path>
                        </svg>
                        Export to Excel
                    </button>
                    <button id="exportPDF" class="bg-red-600 hover:bg-red-700 text-white font-bold py-2 px-4 rounded inline-flex items-center">
                        <svg class="w-4 h-4 mr-2" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd" d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd"></path>
                        </svg>
                        Export to PDF
                    </button>
                </div>
            </div>
            <div class="overflow-x-auto">
                <table class="hazard-table w-full">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Hazard Type</th>
                            <th>Details</th>
                            <th>Area</th>
                            <th>Status</th>
                            <th>Aging Days</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody id="hazardTableBody">
                        <!-- Reports will be added here dynamically -->
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
        // Initialize data from localStorage if available
        let hazardReports = JSON.parse(localStorage.getItem('hazardReports')) || [];
        let logoData = localStorage.getItem('logoData');

        // DOM elements
        const hazardForm = document.getElementById('hazardForm');
        const hazardTableBody = document.getElementById('hazardTableBody');
        const logoUpload = document.getElementById('logoUpload');
        const logoPreview = document.getElementById('logoPreview');
        const logoContainer = document.getElementById('logoContainer');
        const exportExcelBtn = document.getElementById('exportExcel');
        const exportPDFBtn = document.getElementById('exportPDF');

        // Summary elements
        const totalReportsEl = document.getElementById('totalReports');
        const openReportsEl = document.getElementById('openReports');
        const closedReportsEl = document.getElementById('closedReports');
        const pendingReportsEl = document.getElementById('pendingReports');

        // Charts
        let topHazardsChart, areaStatusChart, areaReportsChart, areaCorrectedChart;

        // Initialize the page
        document.addEventListener('DOMContentLoaded', function() {
            // Load logo if exists
            if (logoData) {
                logoPreview.src = logoData;
                logoPreview.classList.remove('hidden');
            }

            // Render existing reports
            renderHazardReports();
            updateSummary();
            renderCharts();

            // Set today's date as default
            document.getElementById('reportDate').valueAsDate = new Date();
        });

        // Handle form submission
        hazardForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            const reportDate = document.getElementById('reportDate').value;
            const hazardType = document.getElementById('hazardType').value;
            const hazardDetails = document.getElementById('hazardDetails').value;
            const status = document.getElementById('status').value;
            const area = document.getElementById('area').value;

            // Create new report
            const newReport = {
                id: Date.now(),
                date: reportDate,
                hazardType: hazardType,
                details: hazardDetails,
                status: status,
                area: area,
                createdAt: new Date().toISOString()
            };

            // Add to reports array
            hazardReports.push(newReport);

            // Save to localStorage
            saveData();

            // Render the new report
            renderHazardReports();
            updateSummary();
            renderCharts();

            // Reset form
            hazardForm.reset();
            document.getElementById('reportDate').valueAsDate = new Date();
        });

        // Handle logo upload
        logoUpload.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(event) {
                    logoPreview.src = event.target.result;
                    logoPreview.classList.remove('hidden');
                    logoData = event.target.result;
                    localStorage.setItem('logoData', logoData);
                };
                reader.readAsDataURL(file);
            }
        });

        // Handle export to Excel
        exportExcelBtn.addEventListener('click', function() {
            exportToExcel();
        });

        // Handle export to PDF
        exportPDFBtn.addEventListener('click', function() {
            exportToPDF();
        });

        // Filter elements
        const startDateFilter = document.getElementById('startDate');
        const endDateFilter = document.getElementById('endDate');
        const areaFilter = document.getElementById('areaFilter');
        const hazardFilter = document.getElementById('hazardFilter');
        const resetFiltersBtn = document.getElementById('resetFilters');

        // Set default date range (last 30 days)
        const defaultEndDate = new Date();
        const defaultStartDate = new Date();
        defaultStartDate.setDate(defaultStartDate.getDate() - 30);
        
        startDateFilter.valueAsDate = defaultStartDate;
        endDateFilter.valueAsDate = defaultEndDate;

        // Add event listeners for filters
        startDateFilter.addEventListener('change', applyFilters);
        endDateFilter.addEventListener('change', applyFilters);
        areaFilter.addEventListener('change', applyFilters);
        hazardFilter.addEventListener('change', applyFilters);
        resetFiltersBtn.addEventListener('click', resetFilters);

        // Function to apply filters
        function applyFilters() {
            renderHazardReports();
            updateSummary();
            renderCharts();
        }

        // Function to reset all filters
        function resetFilters() {
            startDateFilter.valueAsDate = defaultStartDate;
            endDateFilter.valueAsDate = defaultEndDate;
            areaFilter.value = '';
            hazardFilter.value = '';
            applyFilters();
        }

        // Function to get filtered reports
        function getFilteredReports() {
            const startDate = startDateFilter.value ? new Date(startDateFilter.value) : null;
            const endDate = endDateFilter.value ? new Date(endDateFilter.value) : null;
            const area = areaFilter.value;
            const hazardType = hazardFilter.value;

            return hazardReports.filter(report => {
                const reportDate = new Date(report.date);
                
                // Date filter
                if (startDate && reportDate < startDate) return false;
                if (endDate && reportDate > endDate) return false;
                
                // Area filter
                if (area && report.area !== area) return false;
                
                // Hazard type filter
                if (hazardType && report.hazardType !== hazardType) return false;
                
                return true;
            });
        }

        // Function to render hazard reports in the table
        function renderHazardReports() {
            const filteredReports = getFilteredReports();
            hazardTableBody.innerHTML = '';

            if (hazardReports.length === 0) {
                hazardTableBody.innerHTML = '<tr><td colspan="7" class="text-center py-4">No hazard reports found</td></tr>';
                return;
            }

            filteredReports.forEach(report => {
                const row = document.createElement('tr');
                
                // Calculate aging days
                const reportDate = new Date(report.date);
                const today = new Date();
                const diffTime = Math.abs(today - reportDate);
                const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

                // Status class
                let statusClass = '';
                if (report.status === 'Open') statusClass = 'status-open';
                else if (report.status === 'Closed') statusClass = 'status-closed';
                else if (report.status === 'Pending') statusClass = 'status-pending';
                else if (report.status === 'Ongoing') statusClass = 'status-ongoing';

                row.innerHTML = `
                    <td>${report.date}</td>
                    <td>${report.hazardType}</td>
                    <td>${report.details}</td>
                    <td>${report.area}</td>
                    <td class="${statusClass}">${report.status}</td>
                    <td>${diffDays}</td>
                    <td>
                        <button onclick="editReport(${report.id})" class="text-blue-600 hover:text-blue-800 mr-2">Edit</button>
                        <button onclick="deleteReport(${report.id})" class="text-red-600 hover:text-red-800">Delete</button>
                    </td>
                `;
                
                hazardTableBody.appendChild(row);
            });
        }

        // Function to edit a report
        function editReport(id) {
            const report = hazardReports.find(r => r.id === id);
            if (!report) return;

            document.getElementById('reportDate').value = report.date;
            document.getElementById('hazardType').value = report.hazardType;
            document.getElementById('hazardDetails').value = report.details;
            document.getElementById('status').value = report.status;
            document.getElementById('area').value = report.area;

            // Remove the report from the array
            hazardReports = hazardReports.filter(r => r.id !== id);

            // Save changes
            saveData();
            renderHazardReports();
            updateSummary();
            renderCharts();
        }

        // Function to delete a report
        function deleteReport(id) {
            if (confirm('Are you sure you want to delete this report?')) {
                hazardReports = hazardReports.filter(r => r.id !== id);
                saveData();
                renderHazardReports();
                updateSummary();
                renderCharts();
            }
        }

        // Function to save data to localStorage
        function saveData() {
            localStorage.setItem('hazardReports', JSON.stringify(hazardReports));
        }

        // Function to update summary statistics
        function updateSummary() {
            const filteredReports = getFilteredReports();
            totalReportsEl.textContent = filteredReports.length;
            openReportsEl.textContent = filteredReports.filter(r => r.status === 'Open').length;
            closedReportsEl.textContent = filteredReports.filter(r => r.status === 'Closed').length;
            pendingReportsEl.textContent = filteredReports.filter(r => r.status === 'Pending' || r.status === 'Ongoing').length;
        }

        // Function to render charts
        function renderCharts() {
            // Destroy existing charts if they exist
            if (topHazardsChart) topHazardsChart.destroy();
            if (areaStatusChart) areaStatusChart.destroy();
            if (areaReportsChart) areaReportsChart.destroy();
            if (areaCorrectedChart) areaCorrectedChart.destroy();

            // Get data for charts
            const filteredReports = getFilteredReports();
            const hazardCounts = {};
            const areaCounts = {};
            const areaStatusCounts = {};
            const areaCorrectedCounts = {};

            filteredReports.forEach(report => {
                // Count hazards for top hazards chart
                hazardCounts[report.hazardType] = (hazardCounts[report.hazardType] || 0) + 1;
                
                // Count reports per area
                areaCounts[report.area] = (areaCounts[report.area] || 0) + 1;
                
                // Count corrected (closed) reports per area
                if (report.status === 'Closed') {
                    areaCorrectedCounts[report.area] = (areaCorrectedCounts[report.area] || 0) + 1;
                }
                
                // Initialize area status counts if not exists
                if (!areaStatusCounts[report.area]) {
                    areaStatusCounts[report.area] = {
                        Open: 0,
                        Closed: 0,
                        Pending: 0,
                        Ongoing: 0
                    };
                }
                // Increment status count for the area
                areaStatusCounts[report.area][report.status]++;
            });

            // Prepare data for top hazards chart (top 3)
            const sortedHazards = Object.entries(hazardCounts)
                .sort((a, b) => b[1] - a[1])
                .slice(0, 3);
            const topHazardLabels = sortedHazards.map(item => item[0]);
            const topHazardData = sortedHazards.map(item => item[1]);

            // Prepare data for area reports chart
            const areaLabels = Object.keys(areaCounts);
            const areaReportData = areaLabels.map(area => areaCounts[area]);

            // Prepare data for area corrected chart
            const areaCorrectedData = areaLabels.map(area => areaCorrectedCounts[area] || 0);

            // Prepare data for area status chart (stacked bar chart)
            const areaStatusLabels = Object.keys(areaStatusCounts);
            const statusTypes = ['Open', 'Closed', 'Pending', 'Ongoing'];
            const statusColors = {
                'Open': 'rgba(234, 179, 8, 0.7)',
                'Closed': 'rgba(16, 185, 129, 0.7)',
                'Pending': 'rgba(99, 102, 241, 0.7)',
                'Ongoing': 'rgba(239, 68, 68, 0.7)'
            };

            const statusDatasets = statusTypes.map(status => ({
                label: status,
                data: areaStatusLabels.map(area => areaStatusCounts[area][status]),
                backgroundColor: statusColors[status],
                borderColor: statusColors[status].replace('0.7', '1'),
                borderWidth: 1
            }));

            // Create charts
            const ctx1 = document.getElementById('topHazardsChart').getContext('2d');
            topHazardsChart = new Chart(ctx1, {
                type: 'line',
                data: {
                    labels: topHazardLabels,
                    datasets: [{
                        label: 'Number of Reports',
                        data: topHazardData,
                        backgroundColor: 'rgba(22, 163, 74, 0.2)',
                        borderColor: 'rgba(22, 163, 74, 1)',
                        borderWidth: 2,
                        tension: 0.1,
                        fill: true
                    }]
                },
                options: {
                    responsive: true,
                    plugins: {
                        title: {
                            display: true,
                            text: 'Top 3 Most Reported Hazards'
                        },
                        legend: {
                            display: false
                        }
                    },
                    scales: {
                        y: {
                            beginAtZero: true,
                            ticks: {
                                stepSize: 1
                            }
                        }
                    }
                }
            });

            const ctx2 = document.getElementById('areaStatusChart').getContext('2d');
            areaStatusChart = new Chart(ctx2, {
                type: 'bar',
                data: {
                    labels: areaStatusLabels,
                    datasets: statusDatasets
                },
                options: {
                    responsive: true,
                    plugins: {
                        title: {
                            display: true,
                            text: 'Hazard Status by Area'
                        },
                        tooltip: {
                            mode: 'index',
                            intersect: false
                        }
                    },
                    scales: {
                        x: {
                            stacked: true,
                        },
                        y: {
                            stacked: true,
                            beginAtZero: true,
                            ticks: {
                                stepSize: 1
                            }
                        }
                    }
                }
            });

            const ctx3 = document.getElementById('areaReportsChart').getContext('2d');
            areaReportsChart = new Chart(ctx3, {
                type: 'bar',
                data: {
                    labels: areaLabels,
                    datasets: [{
                        label: 'Reports',
                        data: areaReportData,
                        backgroundColor: 'rgba(59, 130, 246, 0.7)',
                        borderColor: 'rgba(59, 130, 246, 1)',
                        borderWidth: 1
                    }]
                },
                options: {
                    responsive: true,
                    plugins: {
                        title: {
                            display: true,
                            text: 'Areas with Most Reports'
                        },
                        legend: {
                            display: false
                        }
                    },
                    scales: {
                        y: {
                            beginAtZero: true,
                            ticks: {
                                stepSize: 1
                            }
                        }
                    }
                }
            });

            const ctx4 = document.getElementById('areaCorrectedChart').getContext('2d');
            areaCorrectedChart = new Chart(ctx4, {
                type: 'bar',
                data: {
                    labels: areaLabels,
                    datasets: [{
                        label: 'Corrected Hazards',
                        data: areaCorrectedData,
                        backgroundColor: 'rgba(16, 185, 129, 0.7)',
                        borderColor: 'rgba(16, 185, 129, 1)',
                        borderWidth: 1
                    }]
                },
                options: {
                    responsive: true,
                    plugins: {
                        title: {
                            display: true,
                            text: 'Areas with Most Corrected Hazards'
                        },
                        legend: {
                            display: false
                        }
                    },
                    scales: {
                        y: {
                            beginAtZero: true,
                            ticks: {
                                stepSize: 1
                            }
                        }
                    }
                }
            });
        }

        // Function to export to Excel
        function exportToExcel() {
            // Prepare data
            const data = [
                ['Date', 'Hazard Type', 'Details', 'Area', 'Status', 'Aging Days']
            ];

            filteredReports.forEach(report => {
                const reportDate = new Date(report.date);
                const today = new Date();
                const diffTime = Math.abs(today - reportDate);
                const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

                data.push([
                    report.date,
                    report.hazardType,
                    report.details,
                    report.area,
                    report.status,
                    diffDays
                ]);
            });

            // Create workbook
            const wb = XLSX.utils.book_new();
            const ws = XLSX.utils.aoa_to_sheet(data);
            XLSX.utils.book_append_sheet(wb, ws, 'Hazard Reports');

            // Export
            XLSX.writeFile(wb, 'MDC_Warehouse_Hazard_Reports.xlsx');
        }

        // Function to export to PDF
        function exportToPDF() {
            const { jsPDF } = window.jspdf;
            const doc = new jsPDF({
                orientation: 'landscape'
            });

            // Add title
            doc.setFontSize(18);
            doc.setTextColor(22, 101, 52); // Dark green
            doc.text('MDC Warehouse Hazard Report', 15, 15);

            // Add date
            doc.setFontSize(10);
            doc.setTextColor(0, 0, 0);
            doc.text(`Generated on: ${new Date().toLocaleDateString()}`, 250, 15);

            // Add summary
            doc.setFontSize(12);
            doc.text('Summary:', 15, 30);
            doc.text(`Total Reports: ${hazardReports.length}`, 15, 40);
            doc.text(`Open: ${hazardReports.filter(r => r.status === 'Open').length}`, 15, 50);
            doc.text(`Closed: ${hazardReports.filter(r => r.status === 'Closed').length}`, 15, 60);
            doc.text(`Pending/Ongoing: ${hazardReports.filter(r => r.status === 'Pending' || r.status === 'Ongoing').length}`, 15, 70);

            // Add charts (as images - simplified for this example)
            // In a real implementation, you might need to use canvas.toDataURL() and addImage()

            // Add table
            doc.setFontSize(10);
            const headers = ['Date', 'Hazard Type', 'Details', 'Area', 'Status', 'Aging Days'];
            const rows = [];

            filteredReports.forEach(report => {
                const reportDate = new Date(report.date);
                const today = new Date();
                const diffTime = Math.abs(today - reportDate);
                const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

                rows.push([
                    report.date,
                    report.hazardType,
                    report.details.substring(0, 50) + (report.details.length > 50 ? '...' : ''),
                    report.area,
                    report.status,
                    diffDays.toString()
                ]);
            });

            // AutoTable plugin would be better here, but using basic text for this example
            doc.text('Hazard Reports:', 15, 85);
            let y = 95;
            
            // Add headers
            doc.setFontStyle('bold');
            let x = 15;
            headers.forEach((header, i) => {
                doc.text(header, x, y);
                x += i === 2 ? 60 : 40; // Wider column for details
            });
            
            // Add rows
            doc.setFontStyle('normal');
            rows.forEach(row => {
                y += 7;
                x = 15;
                row.forEach((cell, i) => {
                    doc.text(cell, x, y);
                    x += i === 2 ? 60 : 40;
                });
                
                // Add new page if needed
                if (y > 270) {
                    doc.addPage('landscape');
                    y = 20;
                }
            });

            // Save the PDF
            doc.save('MDC_Warehouse_Hazard_Report.pdf');
        }
    </script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=froilan1262/version-2-tracker" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>