File size: 12,416 Bytes
cbc783a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Medical Symptom Checker</title>
  <style>
    body {
      font-family: 'Segoe UI', sans-serif;
      background: linear-gradient(135deg, #fdfbfb, #ebedee);
      margin: 0;
      padding: 40px 20px;
      color: #333;
    }

    h1 {
      text-align: center;
      font-size: 2.5rem;
      margin-bottom: 30px;
      background: linear-gradient(to right, #3b82f6, #9333ea);
      -webkit-background-clip: text;
      background-clip: text;
      -webkit-text-fill-color: transparent;
    }

    .search-container {
      text-align: center;
      margin-bottom: 30px;
    }

    input[type="text"] {
      padding: 12px 20px;
      width: 60%;
      max-width: 500px;
      border-radius: 30px;
      border: 1px solid #ccc;
      box-shadow: 0 4px 10px rgba(0,0,0,0.05);
      transition: all 0.3s ease;
    }

    input[type="text"]:focus {
      outline: none;
      border-color: #6366f1;
      box-shadow: 0 4px 15px rgba(99,102,241,0.2);
    }

    button {
      padding: 12px 25px;
      margin-left: 10px;
      border: none;
      border-radius: 30px;
      background: linear-gradient(to right, #3b82f6, #9333ea);
      color: white;
      cursor: pointer;
      font-weight: bold;
      box-shadow: 0 4px 10px rgba(0,0,0,0.1);
      transition: background 0.3s ease;
    }

    button:hover {
      background: linear-gradient(to right, #2563eb, #7e22ce);
    }

    .selected-symptoms {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      gap: 10px;
      margin-bottom: 30px;
    }

    .symptom-tag {
      background: #e0e7ff;
      color: #1e40af;
      padding: 8px 15px;
      border-radius: 20px;
      display: flex;
      align-items: center;
      animation: fadeIn 0.3s ease;
    }

    .remove-symptom {
      margin-left: 10px;
      cursor: pointer;
      color: #555;
      font-weight: bold;
    }

    .container {
      display: flex;
      flex-wrap: wrap;
      gap: 20px;
      justify-content: center;
    }

    .panel {
      flex: 1 1 300px;
      background: white;
      border-radius: 15px;
      padding: 20px;
      box-shadow: 0 10px 20px rgba(0,0,0,0.05);
      transition: transform 0.2s ease;
      animation: fadeIn 0.5s ease;
    }

    .panel:hover {
      transform: translateY(-5px);
    }

    .panel h2 {
      margin-top: 0;
      background: linear-gradient(to right, #2563eb, #9333ea);
      -webkit-background-clip: text;
      background-clip: text;
      -webkit-text-fill-color: transparent;
      font-size: 1.5rem;
    }

    ul {
      list-style: none;
      padding: 0;
    }

    li {
      padding: 10px 0;
      border-bottom: 1px solid #eee;
      transition: background 0.2s ease;
    }

    .clickable {
      color: #6366f1;
      cursor: pointer;
    }

    .clickable:hover {
      text-decoration: underline;
    }

    #error-notification {
      background-color: #fee2e2;
      color: #b91c1c;
      padding: 12px 20px;
      border-radius: 8px;
      margin: 0 auto 20px auto;
      max-width: 600px;
      text-align: center;
      display: none;
    }

    @keyframes fadeIn {
      from { opacity: 0; transform: translateY(10px); }
      to { opacity: 1; transform: translateY(0); }
    }

    @media screen and (max-width: 768px) {
      input[type="text"] {
        width: 80%;
      }
      .container {
        flex-direction: column;
        align-items: stretch;
      }
    }
  </style>
</head>
<body>
  <h1>Medical Symptom Checker</h1>

  <div class="search-container">
    <input type="text" id="symptom-search" placeholder="Enter a symptom..."/>
    <button id="search-button">Search</button>
  </div>

  <div id="error-notification"></div>

  <div class="selected-symptoms" id="selected-symptoms">
    <!-- Selected symptoms will appear here -->
  </div>

  <div class="container">
    <div class="panel">
      <h2>Matching Diseases</h2>
      <ul id="diseases-list"></ul>
    </div>
    <div class="panel">
      <h2>Similar Symptoms</h2>
      <ul id="similar-list"></ul>
    </div>
    <div class="panel">
      <h2>Related Symptoms</h2>
      <ul id="related-list"></ul>
    </div>
  </div>

  <!-- Keep your JavaScript at the bottom (same as before, or I can refactor it too if you want) -->
  <script>
        // Store selected symptoms
        let selectedSymptoms = [];
        
        // DOM elements
        const searchInput = document.getElementById('symptom-search');
        const searchButton = document.getElementById('search-button');
        const selectedSymptomsDiv = document.getElementById('selected-symptoms');
        const diseasesList = document.getElementById('diseases-list');
        const similarList = document.getElementById('similar-list');
        const relatedList = document.getElementById('related-list');
        const errorElement = document.getElementById('error-notification');
        
        // Function to show error message
        function showError(message) {
            errorElement.textContent = message;
            errorElement.style.display = 'block';
            setTimeout(() => {
                errorElement.style.display = 'none';
            }, 5000);
        }
        
        // New function to handle both initial search and updates after selections
        async function performSearch(inputSymptom) {
            // Clear any previous errors
            errorElement.style.display = 'none';
            
            try {
                // Create base URL with inputSymptom if provided, otherwise use first selected symptom
                let symptomParam = inputSymptom;
                if (!symptomParam && selectedSymptoms.length > 0) {
                    symptomParam = selectedSymptoms[0];
                }
                
                if (!symptomParam) return; // Exit if no symptom to search
                
                let url = `${window.location.origin}/search?symptom=${encodeURIComponent(symptomParam)}`;
                
                // Add remaining selected symptoms to query
                if (selectedSymptoms.length > 0) {
                    selectedSymptoms.forEach((s, index) => {
                        // Skip the first one if it's the same as symptomParam
                        if (index === 0 && s === symptomParam) return;
                        url += `&selected=${encodeURIComponent(s)}`;
                    });
                }
                
                const response = await fetch(url);
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const data = await response.json();
                
                displayResults(data);
                
                // Clear search input only if this was initiated from the search box
                if (inputSymptom) {
                    searchInput.value = '';
                }
            } catch (error) {
                showError(`Error searching for symptom: ${error.message}. Please try again.`);
                console.error('Error fetching data:', error);
            }
        }
        
        // Update search function to use the new performSearch function
        async function searchSymptoms() {
            const symptom = searchInput.value.trim();
            if (!symptom) return;
            performSearch(symptom);
        }
        
        // Display results
        function displayResults(data) {
            // Update diseases list
            diseasesList.innerHTML = '';
            if (!data.matching_diseases || data.matching_diseases.length === 0) {
                diseasesList.innerHTML = '<li>No matching diseases found</li>';
            } else {
                data.matching_diseases.forEach(disease => {
                    diseasesList.innerHTML += `<li>${escapeHTML(disease)}</li>`;
                });
            }
            
            // Update similar symptoms
            similarList.innerHTML = '';
            if (data.semantic_matches && data.semantic_matches.length > 0) {
                data.semantic_matches.forEach(symptom => {
                    if (!selectedSymptoms.includes(symptom)) {
                        similarList.innerHTML += `<li class="clickable" onclick="addSymptom('${escapeJS(symptom)}')">${escapeHTML(symptom)}</li>`;
                    }
                });
            } else {
                similarList.innerHTML = '<li>No similar symptoms found</li>';
            }
            
            // Update related symptoms - sort them alphabetically
            relatedList.innerHTML = '';
            if (!data.related_symptoms || data.related_symptoms.length === 0) {
                relatedList.innerHTML = '<li>No related symptoms found</li>';
            } else {
                // Sort the related symptoms alphabetically
                const sortedSymptoms = [...data.related_symptoms].sort((a, b) => a.localeCompare(b));
                
                sortedSymptoms.forEach(symptom => {
                    if (!selectedSymptoms.includes(symptom)) {
                        relatedList.innerHTML += `<li class="clickable" onclick="addSymptom('${escapeJS(symptom)}')">${escapeHTML(symptom)}</li>`;
                    }
                });
            }
            
            // Update selected symptoms
            updateSelectedSymptoms();
        }
        
        // Helper function to escape HTML
        function escapeHTML(str) {
            return str
                .replace(/&/g, '&amp;')
                .replace(/</g, '&lt;')
                .replace(/>/g, '&gt;')
                .replace(/"/g, '&quot;')
                .replace(/'/g, '&#39;');
        }
        
        // Helper function to escape strings for JavaScript
        function escapeJS(str) {
            return str
                .replace(/\\/g, '\\\\')
                .replace(/'/g, "\\'")
                .replace(/"/g, '\\"')
                .replace(/\n/g, '\\n')
                .replace(/\r/g, '\\r')
                .replace(/\t/g, '\\t');
        }
        
        // Add a symptom to the selected list
        function addSymptom(symptom) {
            if (!selectedSymptoms.includes(symptom)) {
                selectedSymptoms.push(symptom);
                updateSelectedSymptoms();
                
                // Instead of calling searchSymptoms which expects input from search box,
                // directly make a request with the current selected symptoms
                performSearch(null);
            }
        }
        
        // Remove a symptom from the selected list
        function removeSymptom(symptom) {
            selectedSymptoms = selectedSymptoms.filter(s => s !== symptom);
            updateSelectedSymptoms();
            if (selectedSymptoms.length > 0) {
                performSearch(null);
            } else {
                // Clear results if no symptoms are selected
                diseasesList.innerHTML = '<li>Enter a symptom to get started</li>';
                similarList.innerHTML = '';
                relatedList.innerHTML = '';
            }
        }
        
        // Update the selected symptoms display
        function updateSelectedSymptoms() {
            selectedSymptomsDiv.innerHTML = '';
            selectedSymptoms.forEach(symptom => {
                const tag = document.createElement('div');
                tag.className = 'symptom-tag';
                tag.innerHTML = `
                    ${escapeHTML(symptom)}
                    <span class="remove-symptom" onclick="removeSymptom('${escapeJS(symptom)}')">×</span>
                `;
                selectedSymptomsDiv.appendChild(tag);
            });
        }
        
        // Event listeners
        searchButton.addEventListener('click', searchSymptoms);
        searchInput.addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                searchSymptoms();
            }
        });
        
        // Initialize
        diseasesList.innerHTML = '<li>Enter a symptom to get started</li>';
    
    // Paste your exact same JS from earlier here — no changes needed.
    // Let me know if you'd like animation when adding/removing tags.
  </script>
</body>
</html>