-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
889 lines (782 loc) · 41.7 KB
/
index.html
File metadata and controls
889 lines (782 loc) · 41.7 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
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Aushadham</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Inter', sans-serif;
transition: background-color 0.3s ease, color 0.3s ease;
}
body.dark-mode {
background-color: #1a202c;
color: #e2e8f0;
}
.no-scrollbar::-webkit-scrollbar {
display: none;
}
.no-scrollbar {
-ms-overflow-style: none;
scrollbar-width: none;
}
.section-fade {
transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out;
}
.section-hidden {
opacity: 0;
transform: translateY(20px);
pointer-events: none;
position: absolute;
width: 100%;
}
.section-visible {
opacity: 1;
transform: translateY(0);
position: relative;
}
.card-hover {
transition: all 0.2s ease;
}
.card-hover:hover {
transform: translateY(-2px);
}
/* Dark mode color overrides - uses class-based theming with localStorage persistence
Note: !important is required to override TailwindCSS utility classes */
.dark-mode .bg-gray-50 {
background-color: #1a202c !important;
}
.dark-mode .bg-white {
background-color: #2d3748 !important;
}
.dark-mode .text-gray-900 {
color: #e2e8f0 !important;
}
.dark-mode .text-gray-700 {
color: #cbd5e0 !important;
}
.dark-mode .text-gray-600 {
color: #a0aec0 !important;
}
.dark-mode .text-gray-500 {
color: #718096 !important;
}
.dark-mode .border-gray-200 {
border-color: #4a5568 !important;
}
.dark-mode .bg-gray-100 {
background-color: #4a5568 !important;
}
.dark-mode input,
.dark-mode textarea {
background-color: #4a5568 !important;
color: #e2e8f0 !important;
}
.dark-mode .shadow-md,
.dark-mode .shadow-lg,
.dark-mode .shadow-2xl {
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -1px rgba(0, 0, 0, 0.2) !important;
}
.dark-mode .theme-toggle {
background-color: #4a5568 !important;
}
</style>
</head>
<body class="bg-gray-50 text-gray-900 min-h-screen p-4">
<div class="max-w-2xl mx-auto">
<!-- Header -->
<div class="flex items-center justify-between mb-6">
<div class="flex gap-2">
<button id="back-btn" class="w-12 h-12 rounded-xl bg-white shadow-md flex items-center justify-center hover:shadow-lg transition hidden">
<svg class="w-6 h-6 text-cyan-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M15 19l-7-7 7-7"/>
</svg>
</button>
<button id="home-btn" class="w-12 h-12 rounded-xl bg-white shadow-md flex items-center justify-center hover:shadow-lg transition hidden">
<svg class="w-6 h-6 text-green-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/>
</svg>
</button>
<button id="history-btn" class="w-12 h-12 rounded-xl bg-white shadow-md flex items-center justify-center hover:shadow-lg transition hidden">
<svg class="w-6 h-6 text-purple-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
</button>
</div>
<div class="flex gap-2">
<button id="theme-toggle-btn" class="w-12 h-12 rounded-xl bg-white shadow-md flex items-center justify-center hover:shadow-lg transition theme-toggle">
<svg id="theme-icon-sun" class="w-6 h-6 text-yellow-500 hidden" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"/>
</svg>
<svg id="theme-icon-moon" class="w-6 h-6 text-indigo-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"/>
</svg>
</button>
<div id="user-display" class="hidden px-4 py-2 bg-white rounded-xl shadow-md text-sm font-semibold text-gray-700"></div>
<button id="login-btn" class="px-4 py-2 bg-gradient-to-r from-cyan-500 to-blue-500 hover:from-cyan-600 hover:to-blue-600 text-white font-semibold rounded-xl shadow-md transition">
Login
</button>
<button id="logout-btn" class="hidden px-4 py-2 bg-gray-600 hover:bg-gray-700 text-white font-semibold rounded-xl shadow-md transition">
Logout
</button>
</div>
</div>
<div id="container-content" class="relative min-h-[600px]">
<!-- Start Section -->
<div id="start-section" class="section-fade section-visible">
<div class="text-center mb-8">
<h1 class="text-4xl font-bold mb-3 text-gray-900">Aushadham</h1>
<p class="text-gray-600 text-lg">AI-Powered Health Assessment</p>
</div>
<div class="bg-white rounded-2xl p-6 mb-6 shadow-md">
<label class="block text-sm font-semibold mb-2 text-gray-700">Main Symptom</label>
<input type="text" id="symptom-input" placeholder="e.g., Headache, Fever, Stomach pain"
class="w-full p-4 bg-gray-50 text-gray-900 rounded-xl border-2 border-gray-200 focus:border-cyan-500 focus:outline-none transition text-lg" />
</div>
<div class="bg-white rounded-2xl p-6 mb-6 shadow-md">
<label class="block text-sm font-semibold mb-2 text-gray-700">Description (Optional)</label>
<textarea id="description-input" placeholder="Briefly describe how you feel..." rows="4"
class="w-full p-4 bg-gray-50 text-gray-900 rounded-xl border-2 border-gray-200 focus:border-cyan-500 focus:outline-none transition no-scrollbar resize-none"></textarea>
</div>
<button id="start-btn" class="w-full bg-gradient-to-r from-green-500 to-green-600 hover:from-green-600 hover:to-green-700 text-white font-bold py-5 px-6 rounded-2xl transition duration-300 ease-in-out transform hover:scale-[1.02] text-lg shadow-lg shadow-green-500/20 disabled:opacity-50 disabled:cursor-not-allowed disabled:scale-100 flex items-center justify-center">
<span class="btn-text">Start Assessment</span>
<svg class="loader-spinner animate-spin ml-3 h-6 w-6 text-white hidden" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
</button>
</div>
<!-- Question Section -->
<div id="question-section" class="section-fade section-hidden">
<div class="mb-6">
<div class="flex justify-between items-center mb-2">
<span class="text-sm text-green-400 font-semibold" id="progress-text">0/10 questions answered</span>
<button id="evaluate-btn" class="px-6 py-2 bg-cyan-500 hover:bg-cyan-600 text-black font-bold rounded-xl transition">
Evaluate
</button>
</div>
<div class="w-full bg-gray-200 rounded-full h-3">
<div id="progress-bar-fill" class="bg-gradient-to-r from-green-400 to-cyan-400 h-3 rounded-full transition-all duration-500 ease-out" style="width: 0%"></div>
</div>
</div>
<div id="question-card" class="bg-white rounded-2xl p-8 border-2 border-yellow-400 shadow-lg">
<div class="mb-4">
<span class="text-sm text-gray-700 font-semibold" id="category-label">Category</span>
<span class="text-xs text-gray-500 ml-2" id="skip-label"></span>
</div>
<div class="mb-6 text-gray-600 text-sm" id="helper-text"></div>
<h2 id="question-text" class="text-2xl font-bold mb-8 text-center min-h-[60px] text-gray-900"></h2>
<div id="options-container" class="space-y-4"></div>
<div class="flex gap-3 mt-6">
<button id="back-question-btn" class="flex-1 bg-white hover:bg-gray-50 text-gray-900 font-semibold py-4 px-6 rounded-xl transition flex items-center justify-center border-2 border-red-400 shadow-md">
<svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 17l-5-5m0 0l5-5m-5 5h12"/>
</svg>
Back
</button>
<button id="skip-question-btn" class="flex-1 bg-white hover:bg-gray-50 text-gray-900 font-semibold py-4 px-6 rounded-xl transition flex items-center justify-center border-2 border-green-400 shadow-md">
Skip
<svg class="w-5 h-5 ml-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7l5 5m0 0l-5 5m5-5H6"/>
</svg>
</button>
</div>
</div>
</div>
<!-- Report Section -->
<div id="report-section" class="section-fade section-hidden">
<div class="text-center mb-8">
<div class="w-20 h-20 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4">
<svg class="w-12 h-12 text-green-600" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
</div>
<h2 class="text-3xl font-bold mb-2 text-gray-900">Assessment Complete</h2>
<p class="text-gray-600">Here's your health report</p>
</div>
<div id="report-content" class="space-y-4"></div>
<div id="save-section" class="mt-6 bg-blue-50 rounded-2xl p-6 border-2 border-blue-400 hidden">
<p class="text-blue-800 mb-4 font-semibold">💾 Save this assessment to your account for future reference</p>
<button id="save-test-btn" class="w-full bg-gradient-to-r from-blue-500 to-indigo-500 hover:from-blue-600 hover:to-indigo-600 text-white font-bold py-4 px-6 rounded-xl transition shadow-lg">
Save Test Results
</button>
</div>
<div id="login-prompt" class="mt-6 bg-yellow-50 rounded-2xl p-6 border-2 border-yellow-400 hidden">
<p class="text-yellow-800 mb-4 font-semibold">📋 Login to save your test results and track your health history</p>
<button id="login-to-save-btn" class="w-full bg-gradient-to-r from-yellow-500 to-orange-500 hover:from-yellow-600 hover:to-orange-600 text-white font-bold py-4 px-6 rounded-xl transition shadow-lg">
Login to Save
</button>
</div>
<button id="restart-btn" class="mt-4 w-full bg-gradient-to-r from-cyan-500 to-blue-500 hover:from-cyan-600 hover:to-blue-600 text-white font-bold py-5 px-6 rounded-2xl transition duration-300 text-lg shadow-lg shadow-cyan-500/20">
Start New Assessment
</button>
</div>
<!-- History Section -->
<div id="history-section" class="section-fade section-hidden">
<div class="text-center mb-8">
<h2 class="text-3xl font-bold mb-2 text-gray-900">Assessment History</h2>
<p class="text-gray-600">Your saved health assessments</p>
</div>
<div id="history-content" class="space-y-4"></div>
<div id="no-history" class="text-center py-12 hidden">
<div class="w-20 h-20 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
<svg class="w-12 h-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
</svg>
</div>
<p class="text-gray-500">No saved assessments yet</p>
</div>
</div>
<!-- Login/Register Modal -->
<div id="auth-modal" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50" style="margin: 0;">
<div class="bg-white rounded-2xl p-8 max-w-md w-full mx-4 shadow-2xl">
<div class="flex justify-between items-center mb-6">
<h2 id="auth-modal-title" class="text-2xl font-bold text-gray-900">Login</h2>
<button id="close-auth-modal" class="text-gray-500 hover:text-gray-700">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
</svg>
</button>
</div>
<!-- Modal Error Banner -->
<div id="modal-error-banner" class="hidden bg-red-50 border-2 border-red-400 text-red-800 px-4 py-3 rounded-xl mb-4" role="alert">
<strong class="font-bold">Error: </strong>
<span class="block sm:inline" id="modal-error-message">Something went wrong.</span>
</div>
<div id="login-form">
<div class="mb-4">
<label class="block text-sm font-semibold mb-2 text-gray-700">Username or Email</label>
<input type="text" id="login-username" class="w-full p-3 bg-gray-50 rounded-xl border-2 border-gray-200 focus:border-cyan-500 focus:outline-none" />
</div>
<div class="mb-6">
<label class="block text-sm font-semibold mb-2 text-gray-700">Password</label>
<input type="password" id="login-password" class="w-full p-3 bg-gray-50 rounded-xl border-2 border-gray-200 focus:border-cyan-500 focus:outline-none" />
</div>
<button id="login-submit-btn" class="w-full bg-gradient-to-r from-cyan-500 to-blue-500 hover:from-cyan-600 hover:to-blue-600 text-white font-bold py-3 rounded-xl transition mb-3">
Login
</button>
<p class="text-center text-gray-600">
Don't have an account?
<button id="switch-to-register" class="text-cyan-500 font-semibold hover:text-cyan-600">Register</button>
</p>
</div>
<div id="register-form" class="hidden">
<div class="mb-4">
<label class="block text-sm font-semibold mb-2 text-gray-700">Username</label>
<input type="text" id="register-username" class="w-full p-3 bg-gray-50 rounded-xl border-2 border-gray-200 focus:border-cyan-500 focus:outline-none" />
</div>
<div class="mb-4">
<label class="block text-sm font-semibold mb-2 text-gray-700">Email</label>
<input type="email" id="register-email" class="w-full p-3 bg-gray-50 rounded-xl border-2 border-gray-200 focus:border-cyan-500 focus:outline-none" />
</div>
<div class="mb-4">
<label class="block text-sm font-semibold mb-2 text-gray-700">Full Name (Optional)</label>
<input type="text" id="register-fullname" class="w-full p-3 bg-gray-50 rounded-xl border-2 border-gray-200 focus:border-cyan-500 focus:outline-none" />
</div>
<div class="mb-6">
<label class="block text-sm font-semibold mb-2 text-gray-700">Password</label>
<input type="password" id="register-password" class="w-full p-3 bg-gray-50 rounded-xl border-2 border-gray-200 focus:border-cyan-500 focus:outline-none" />
</div>
<button id="register-submit-btn" class="w-full bg-gradient-to-r from-green-500 to-emerald-500 hover:from-green-600 hover:to-emerald-600 text-white font-bold py-3 rounded-xl transition mb-3">
Register
</button>
<p class="text-center text-gray-600">
Already have an account?
<button id="switch-to-login" class="text-cyan-500 font-semibold hover:text-cyan-600">Login</button>
</p>
</div>
</div>
</div>
<!-- Error Banner -->
<div id="error-banner" class="hidden bg-red-50 border-2 border-red-400 text-red-800 px-6 py-4 rounded-xl mb-4" role="alert">
<strong class="font-bold">Error: </strong>
<span class="block sm:inline" id="error-message">Something went wrong.</span>
</div>
</div>
</div>
<script>
// --- DOM Elements ---
const startBtn = document.getElementById('start-btn');
const restartBtn = document.getElementById('restart-btn');
const evaluateBtn = document.getElementById('evaluate-btn');
const backBtn = document.getElementById('back-btn');
const homeBtn = document.getElementById('home-btn');
const historyBtn = document.getElementById('history-btn');
const loginBtn = document.getElementById('login-btn');
const logoutBtn = document.getElementById('logout-btn');
const userDisplay = document.getElementById('user-display');
const backQuestionBtn = document.getElementById('back-question-btn');
const skipQuestionBtn = document.getElementById('skip-question-btn');
const symptomInput = document.getElementById('symptom-input');
const descriptionInput = document.getElementById('description-input');
const startSection = document.getElementById('start-section');
const questionSection = document.getElementById('question-section');
const reportSection = document.getElementById('report-section');
const historySection = document.getElementById('history-section');
const questionText = document.getElementById('question-text');
const categoryLabel = document.getElementById('category-label');
const skipLabel = document.getElementById('skip-label');
const helperText = document.getElementById('helper-text');
const optionsContainer = document.getElementById('options-container');
const progressBarFill = document.getElementById('progress-bar-fill');
const progressText = document.getElementById('progress-text');
const reportContent = document.getElementById('report-content');
const historyContent = document.getElementById('history-content');
const noHistory = document.getElementById('no-history');
const saveSection = document.getElementById('save-section');
const loginPrompt = document.getElementById('login-prompt');
const saveTestBtn = document.getElementById('save-test-btn');
const loginToSaveBtn = document.getElementById('login-to-save-btn');
const authModal = document.getElementById('auth-modal');
const authModalTitle = document.getElementById('auth-modal-title');
const closeAuthModal = document.getElementById('close-auth-modal');
const loginForm = document.getElementById('login-form');
const registerForm = document.getElementById('register-form');
const switchToRegister = document.getElementById('switch-to-register');
const switchToLogin = document.getElementById('switch-to-login');
const loginSubmitBtn = document.getElementById('login-submit-btn');
const registerSubmitBtn = document.getElementById('register-submit-btn');
const errorBanner = document.getElementById('error-banner');
const errorMessage = document.getElementById('error-message');
const modalErrorBanner = document.getElementById('modal-error-banner');
const modalErrorMessage = document.getElementById('modal-error-message');
// --- State ---
let sessionId = null;
let currentQuestion = null;
let currentUser = null;
let authToken = null;
const API_BASE_URL = window.location.hostname === 'localhost' ? 'http://127.0.0.1:5000' : 'https://aushadham.onrender.com';
// --- UI Control Functions ---
const showSection = (sectionToShow) => {
[startSection, questionSection, reportSection, historySection].forEach(section => {
if (section.id === sectionToShow) {
section.classList.remove('section-hidden');
section.classList.add('section-visible');
} else {
section.classList.add('section-hidden');
section.classList.remove('section-visible');
}
});
// Show/hide navigation buttons
const showNav = sectionToShow !== 'start-section';
backBtn.classList.toggle('hidden', !showNav);
homeBtn.classList.toggle('hidden', !showNav);
historyBtn.classList.toggle('hidden', !showNav || !authToken);
};
const setLoading = (button, isLoading) => {
const btnText = button.querySelector('.btn-text');
const spinner = button.querySelector('.loader-spinner');
button.disabled = isLoading;
if (isLoading) {
btnText.textContent = 'Processing...';
spinner.classList.remove('hidden');
} else {
btnText.textContent = 'Start Assessment';
spinner.classList.add('hidden');
}
};
const showError = (message) => {
errorMessage.textContent = message || 'An unexpected error occurred.';
errorBanner.classList.remove('hidden');
setTimeout(() => {
errorBanner.classList.add('hidden');
}, 5000);
};
const showModalError = (message) => {
modalErrorMessage.textContent = message || 'An unexpected error occurred.';
modalErrorBanner.classList.remove('hidden');
setTimeout(() => {
modalErrorBanner.classList.add('hidden');
}, 5000);
};
const showSuccess = (message) => {
const successBanner = document.createElement('div');
successBanner.className = 'fixed top-4 right-4 bg-green-50 border-2 border-green-400 text-green-800 px-6 py-4 rounded-xl shadow-lg z-50';
successBanner.innerHTML = `<strong class="font-bold">Success: </strong><span>${message}</span>`;
document.body.appendChild(successBanner);
setTimeout(() => {
successBanner.remove();
}, 3000);
};
// --- API Call Wrapper ---
const apiCall = async (endpoint, body, method = 'POST', requiresAuth = false, useModalError = false) => {
try {
const headers = { 'Content-Type': 'application/json' };
if (requiresAuth && authToken) {
headers['Authorization'] = `Bearer ${authToken}`;
}
const options = {
method: method,
headers: headers
};
if (body && method !== 'GET') {
options.body = JSON.stringify(body);
}
const response = await fetch(`${API_BASE_URL}/${endpoint}`, options);
const data = await response.json();
if (!data.success) {
throw new Error(data.error || 'API request failed');
}
return data;
} catch (error) {
console.error(`API Error on ${endpoint}:`, error);
if (useModalError) {
showModalError(error.message);
} else {
showError(error.message);
}
return null;
}
};
// --- Authentication Functions ---
const initAuth = () => {
const storedToken = localStorage.getItem('authToken');
const storedUser = localStorage.getItem('currentUser');
if (storedToken && storedUser) {
authToken = storedToken;
currentUser = JSON.parse(storedUser);
updateAuthUI();
}
};
const updateAuthUI = () => {
if (authToken && currentUser) {
loginBtn.classList.add('hidden');
logoutBtn.classList.remove('hidden');
userDisplay.classList.remove('hidden');
userDisplay.textContent = `👤 ${currentUser.username}`;
historyBtn.classList.remove('hidden');
} else {
loginBtn.classList.remove('hidden');
logoutBtn.classList.add('hidden');
userDisplay.classList.add('hidden');
historyBtn.classList.add('hidden');
}
};
const openAuthModal = (isRegister = false) => {
authModal.classList.remove('hidden');
// Clear any previous error messages
modalErrorBanner.classList.add('hidden');
if (isRegister) {
authModalTitle.textContent = 'Register';
loginForm.classList.add('hidden');
registerForm.classList.remove('hidden');
} else {
authModalTitle.textContent = 'Login';
loginForm.classList.remove('hidden');
registerForm.classList.add('hidden');
}
};
const closeAuthModalFn = () => {
authModal.classList.add('hidden');
// Clear form fields
document.getElementById('login-username').value = '';
document.getElementById('login-password').value = '';
document.getElementById('register-username').value = '';
document.getElementById('register-email').value = '';
document.getElementById('register-fullname').value = '';
document.getElementById('register-password').value = '';
};
const handleLogin = async () => {
const username = document.getElementById('login-username').value.trim();
const password = document.getElementById('login-password').value;
if (!username || !password) {
showModalError('Please enter username and password');
return;
}
loginSubmitBtn.disabled = true;
loginSubmitBtn.textContent = 'Logging in...';
const data = await apiCall('login', { username, password }, 'POST', false, true);
loginSubmitBtn.disabled = false;
loginSubmitBtn.textContent = 'Login';
if (data) {
authToken = data.access_token;
currentUser = data.user;
localStorage.setItem('authToken', authToken);
localStorage.setItem('currentUser', JSON.stringify(currentUser));
updateAuthUI();
closeAuthModalFn();
showSuccess('Login successful!');
}
};
const handleRegister = async () => {
const username = document.getElementById('register-username').value.trim();
const email = document.getElementById('register-email').value.trim();
const password = document.getElementById('register-password').value;
const full_name = document.getElementById('register-fullname').value.trim();
if (!username || !email || !password) {
showModalError('Please fill in all required fields');
return;
}
registerSubmitBtn.disabled = true;
registerSubmitBtn.textContent = 'Registering...';
const data = await apiCall('register', { username, email, password, full_name }, 'POST', false, true);
registerSubmitBtn.disabled = false;
registerSubmitBtn.textContent = 'Register';
if (data) {
authToken = data.access_token;
currentUser = data.user;
localStorage.setItem('authToken', authToken);
localStorage.setItem('currentUser', JSON.stringify(currentUser));
updateAuthUI();
closeAuthModalFn();
showSuccess('Registration successful!');
}
};
const handleLogout = () => {
authToken = null;
currentUser = null;
localStorage.removeItem('authToken');
localStorage.removeItem('currentUser');
updateAuthUI();
showSuccess('Logged out successfully');
if (historySection.classList.contains('section-visible')) {
showSection('start-section');
}
};
// --- Core Application Logic ---
const startQuestionnaire = async () => {
const symptom = symptomInput.value.trim();
const description = descriptionInput.value.trim();
if (!symptom) {
showError('Please enter your main symptom to begin.');
return;
}
setLoading(startBtn, true);
const data = await apiCall('start_questionnaire', { symptom, description });
setLoading(startBtn, false);
if (data) {
sessionId = data.session_id;
showSection('question-section');
displayQuestion(data.question);
}
};
const submitAnswer = async (answer, action = 'next') => {
if (!sessionId) return;
const buttons = optionsContainer.querySelectorAll('button');
buttons.forEach(btn => btn.disabled = true);
const data = await apiCall('submit_answer', { session_id: sessionId, answer, action });
if (data) {
if (data.completed) {
showReport();
} else {
displayQuestion(data.question);
}
} else {
buttons.forEach(btn => btn.disabled = false);
}
};
const displayQuestion = (question) => {
currentQuestion = question;
categoryLabel.textContent = question.category || 'Assessment';
skipLabel.textContent = question.skipped ? '(skipped)' : '';
skipLabel.className = question.skipped ? 'text-xs text-gray-500 ml-2' : 'text-xs text-gray-500 ml-2 hidden';
helperText.textContent = question.helper_text || '';
questionText.textContent = question.question;
optionsContainer.innerHTML = '';
question.options.forEach((opt, idx) => {
const colors = ['yellow', 'green', 'red', 'cyan'];
const color = colors[idx % colors.length];
const btn = document.createElement('button');
btn.textContent = opt;
btn.className = `w-full bg-white hover:bg-${color}-50 hover:border-${color}-500 text-gray-900 font-semibold py-5 px-6 rounded-xl transition duration-200 border-2 border-${color}-400 card-hover text-lg shadow-md`;
btn.onclick = () => submitAnswer(opt, 'next');
optionsContainer.appendChild(btn);
});
const totalQuestions = 10;
const answered = Math.round((question.progress / 100) * totalQuestions);
progressText.textContent = `${answered}/${totalQuestions} questions answered`;
progressBarFill.style.width = `${question.progress}%`;
};
const showReport = async () => {
const data = await apiCall('get_report', { session_id: sessionId });
if (data) {
showSection('report-section');
const { report } = data;
let html = `
<div class="bg-white rounded-2xl p-6 border-l-4 border-cyan-500 shadow-md">
<h3 class="font-bold text-lg mb-2 text-cyan-600">Symptom Summary</h3>
<p class="text-gray-700">${report.symptom}</p>
</div>
<div class="grid grid-cols-2 gap-4">
<div class="bg-white rounded-2xl p-6 text-center shadow-md">
<div class="text-3xl font-bold text-blue-600 mb-2">${report.severity}</div>
<h3 class="text-sm font-semibold text-gray-600">Severity Level</h3>
</div>
<div class="bg-white rounded-2xl p-6 text-center shadow-md">
<div class="text-3xl font-bold text-amber-600 mb-2">${report.urgency}</div>
<h3 class="text-sm font-semibold text-gray-600">Urgency</h3>
</div>
</div>
`;
const createList = (title, items, color = 'cyan') => {
if (!items || items.length === 0) return '';
return `
<div class="bg-white rounded-2xl p-6 shadow-md">
<h4 class="font-bold text-lg mb-4 text-${color}-600">${title}</h4>
<div class="space-y-3">
${items.map(item => `
<div class="bg-gray-50 rounded-lg p-4 border-l-4 border-${color}-400">
${typeof item === 'object'
? `<div class="font-semibold text-gray-900 mb-1">${item.name}</div>
<div class="text-sm text-gray-600">${item.purpose}</div>`
: `<div class="text-gray-700">${item}</div>`
}
</div>
`).join('')}
</div>
</div>
`;
};
html += createList('Recommendations', report.recommendations, 'green');
html += createList('Suggested Medications', report.suggested_medications, 'purple');
reportContent.innerHTML = html;
// Show save/login prompt
if (authToken) {
saveSection.classList.remove('hidden');
loginPrompt.classList.add('hidden');
} else {
saveSection.classList.add('hidden');
loginPrompt.classList.remove('hidden');
}
}
};
const saveTest = async () => {
if (!authToken) {
showError('Please login to save your test');
return;
}
saveTestBtn.disabled = true;
saveTestBtn.textContent = 'Saving...';
const data = await apiCall('save_questionnaire', { session_id: sessionId }, 'POST', true);
saveTestBtn.disabled = false;
saveTestBtn.textContent = 'Save Test Results';
if (data) {
showSuccess('Test results saved successfully!');
saveSection.classList.add('hidden');
saveTestBtn.textContent = '✓ Saved';
}
};
const loadHistory = async () => {
if (!authToken) {
showError('Please login to view history');
return;
}
const data = await apiCall('my_questionnaires', null, 'GET', true);
if (data) {
showSection('history-section');
if (data.questionnaires && data.questionnaires.length > 0) {
historyContent.classList.remove('hidden');
noHistory.classList.add('hidden');
let html = '';
data.questionnaires.forEach(q => {
const date = new Date(q.created_at).toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
const severityColor = {
'High': 'red',
'Moderate': 'yellow',
'Low': 'green'
}[q.severity] || 'gray';
html += `
<div class="bg-white rounded-2xl p-6 shadow-md border-l-4 border-${severityColor}-400 card-hover">
<div class="flex justify-between items-start mb-3">
<div>
<h3 class="font-bold text-xl text-gray-900 mb-1">${q.symptom}</h3>
<p class="text-sm text-gray-500">${date}</p>
</div>
<span class="px-3 py-1 rounded-full text-sm font-semibold bg-${severityColor}-100 text-${severityColor}-700">
${q.severity}
</span>
</div>
${q.initial_description ? `<p class="text-gray-600 mb-3">${q.initial_description}</p>` : ''}
<div class="text-sm text-gray-500">
Session: ${q.session_id.substring(0, 8)}...
</div>
</div>
`;
});
historyContent.innerHTML = html;
} else {
historyContent.classList.add('hidden');
noHistory.classList.remove('hidden');
}
}
};
// --- Event Listeners ---
startBtn.addEventListener('click', startQuestionnaire);
restartBtn.addEventListener('click', () => location.reload());
evaluateBtn.addEventListener('click', () => showReport());
backBtn.addEventListener('click', () => showSection('start-section'));
homeBtn.addEventListener('click', () => showSection('start-section'));
historyBtn.addEventListener('click', loadHistory);
backQuestionBtn.addEventListener('click', () => submitAnswer('', 'previous'));
skipQuestionBtn.addEventListener('click', () => submitAnswer('Skipped', 'skip'));
// Authentication event listeners
loginBtn.addEventListener('click', () => openAuthModal(false));
logoutBtn.addEventListener('click', handleLogout);
closeAuthModal.addEventListener('click', closeAuthModalFn);
switchToRegister.addEventListener('click', () => openAuthModal(true));
switchToLogin.addEventListener('click', () => openAuthModal(false));
loginSubmitBtn.addEventListener('click', handleLogin);
registerSubmitBtn.addEventListener('click', handleRegister);
// Save test listeners
saveTestBtn.addEventListener('click', saveTest);
loginToSaveBtn.addEventListener('click', () => openAuthModal(false));
// Close modal on outside click
authModal.addEventListener('click', (e) => {
if (e.target === authModal) {
closeAuthModalFn();
}
});
// Allow Enter key to submit forms
document.getElementById('login-password').addEventListener('keypress', (e) => {
if (e.key === 'Enter') handleLogin();
});
document.getElementById('register-password').addEventListener('keypress', (e) => {
if (e.key === 'Enter') handleRegister();
});
// Initialize authentication on page load
initAuth();
// --- Theme Management ---
const themeToggleBtn = document.getElementById('theme-toggle-btn');
const themeIconSun = document.getElementById('theme-icon-sun');
const themeIconMoon = document.getElementById('theme-icon-moon');
const initTheme = () => {
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark') {
document.body.classList.add('dark-mode');
themeIconSun.classList.remove('hidden');
themeIconMoon.classList.add('hidden');
} else {
document.body.classList.remove('dark-mode');
themeIconSun.classList.add('hidden');
themeIconMoon.classList.remove('hidden');
}
};
const toggleTheme = () => {
document.body.classList.toggle('dark-mode');
const isDark = document.body.classList.contains('dark-mode');
if (isDark) {
localStorage.setItem('theme', 'dark');
themeIconSun.classList.remove('hidden');
themeIconMoon.classList.add('hidden');
} else {
localStorage.setItem('theme', 'light');
themeIconSun.classList.add('hidden');
themeIconMoon.classList.remove('hidden');
}
};
themeToggleBtn.addEventListener('click', toggleTheme);
// Initialize theme on page load
initTheme();
</script>
</body>
</html>