From 4fc64be2a2191b98fc74541da7ad8140050c5fb7 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Sat, 6 Jun 2026 05:46:38 +0530 Subject: [PATCH] Fix autocomplete selection - now adds to client-side array MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BUGFIX: Autocomplete dropdown was showing but items weren't selectable PROBLEM: Clicking autocomplete suggestions didn't add complaints to table ROOT CAUSE: btnAdd click handler was using old database-submit logic instead of new client-side array management SOLUTION: Simplified btnAdd to: 1. Take value from lookup input 2. Check if already exists (prevent duplicates) 3. Add to selectedDischargeComplaints array 4. Call renderDischargeComplaintTable() to refresh 5. Clear input and show success message HOW IT WORKS NOW: - Type complaint name → autocomplete dropdown appears - Click suggestion → sets lookup input value - btnAdd triggered → adds to array → table refreshes - Complaint appears in table with editable Frequency/Severity/Duration All inline editing works without database calls! --- app/Views/billing/ipd/discharge_create.php | 91 ++++++---------------- 1 file changed, 24 insertions(+), 67 deletions(-) diff --git a/app/Views/billing/ipd/discharge_create.php b/app/Views/billing/ipd/discharge_create.php index f10b0f3..3fe055b 100644 --- a/app/Views/billing/ipd/discharge_create.php +++ b/app/Views/billing/ipd/discharge_create.php @@ -1797,81 +1797,38 @@ $allergyStatusNoKnown = in_array($allergyStatusNormalized, ['no known drug aller return; } - var chosen = ''; - complaintSuggestions.forEach(function(row) { - var label = row.name || ''; - if (row.name_hinglish) { - label += ' (' + row.name_hinglish + ')'; - } - if (label.toUpperCase() === inputVal.toUpperCase() || (row.name || '').toUpperCase() === inputVal.toUpperCase()) { - chosen = row.name || inputVal; - } + // Check if already exists + var exists = selectedDischargeComplaints.some(function(item) { + return item.term.toUpperCase() === inputVal.toUpperCase(); }); - - function submitComplaint(value) { - if (!nameInput || !addRowBtn) { - return; - } - nameInput.value = value; - if (remarkInput) { - remarkInput.value = ''; - } - addRowBtn.click(); - } - - if (chosen !== '') { - addComplaintValue(chosen); - submitComplaint(chosen); + + if (exists) { + setComplaintStatus('Complaint already added.', 'error'); if (lookup) { lookup.value = ''; } - setComplaintStatus('Complaint added.', 'success'); return; } - if (!window.jQuery) { - submitComplaint(inputVal); - return; - } - - var csrf = getCsrfPair(form); - var payload = { - text: inputVal - }; - payload[csrf.name] = csrf.value; - - $.post('', payload, function(data) { - if (data && data.csrfName && data.csrfHash) { - var csrfInput = form.querySelector('input[name="' + data.csrfName + '"]'); - if (csrfInput) { - csrfInput.value = data.csrfHash; - } - } - - var rows = (data && data.rows) ? data.rows : []; - if (!rows.length) { - submitComplaint(inputVal); - if (lookup) { - lookup.value = ''; - } - setComplaintStatus('Added custom complaint text.', 'success'); - return; - } - - var first = (rows[0] || '').toString().trim(); - if (first === '') { - submitComplaint(inputVal); - } else { - addComplaintValue(first); - submitComplaint(first); - } - if (lookup) { - lookup.value = ''; - } - setComplaintStatus((data && data.error_text) ? data.error_text : 'Complaint term matched.', 'success'); - }, 'json').fail(function() { - setComplaintStatus('Unable to parse complaint text right now.', 'error'); + // Add to array + selectedDischargeComplaints.push({ + id: 0, + term: inputVal, + frequency: '', + severity: '', + duration: '', + date: '' }); + + // Refresh table + renderDischargeComplaintTable(); + + // Clear input + if (lookup) { + lookup.value = ''; + } + + setComplaintStatus('Complaint added. Edit inline if needed.', 'success'); }); }