').text(term));
+ if (isSnomed && hierarchy) {
+ $item.append($('
').text(hierarchy));
+ }
+ $item.data('term', term);
+
+ $item.on('mouseenter', function() { $(this).css('background', '#f0f4ff'); })
+ .on('mouseleave', function() { $(this).css('background', ''); })
+ .on('mousedown', function(e) { e.preventDefault(); })
+ .on('click', function() {
+ var selectedTerm = $(this).data('term') || term;
+ $inp.val(selectedTerm);
+ if (idx >= 0 && idx < selectedDischargeComplaints.length) {
+ selectedDischargeComplaints[idx].term = selectedTerm;
+ syncDischargeComplaintJsonField();
+ }
+ $dd.hide().empty();
+ _dischargeRowDdIdx = -1;
+ $inp.closest('tr').find('.complaint-freq-input').trigger('focus');
+ });
+
+ $dd.append($item);
+ });
+
+ _positionDischargeDd($dd, $inp);
+ $dd.show();
+ }
+
+ $(document).on('input focus', '.complaint-name-input', function(e) {
+ var $inp = $(this);
+ var q = ($inp.val() || '').trim();
+ var idx = parseInt($inp.attr('data-idx'), 10);
+
+ if (idx >= 0 && idx < selectedDischargeComplaints.length) {
+ selectedDischargeComplaints[idx].term = $inp.val();
+ syncDischargeComplaintJsonField();
+ }
+
+ if (q.length < 1) {
+ $('#discharge_row_complaint_dd').hide().empty();
+ _dischargeRowDdIdx = -1;
+ return;
+ }
+
+ var cacheKey = q.toUpperCase();
+ if (_dischargeComplaintSearchCache[cacheKey]) {
+ renderRowComplaintDropdown(_dischargeComplaintSearchCache[cacheKey], $inp, idx);
+ return;
+ }
+
+ if (_dischargeComplaintSearchTimer) clearTimeout(_dischargeComplaintSearchTimer);
+ if (_dischargeComplaintXhr) {
+ try { _dischargeComplaintXhr.abort(); } catch(e){}
+ _dischargeComplaintXhr = null;
+ }
+
+ _dischargeComplaintSearchTimer = setTimeout(function() {
+ if (document.activeElement !== $inp[0]) return;
+ _dischargeComplaintXhr = $.ajax({
+ url: '= base_url('Opd_prescription/complaints_search') ?>',
+ data: { q: q },
+ dataType: 'json',
+ success: function(data) {
+ _dischargeComplaintXhr = null;
+ if (document.activeElement !== $inp[0]) return;
+ var rows = (data && data.rows) ? data.rows : [];
+ _dischargeComplaintSearchCache[cacheKey] = rows;
+ renderRowComplaintDropdown(rows, $inp, idx);
+ },
+ error: function() {
+ _dischargeComplaintXhr = null;
+ }
+ });
+ }, 200);
+ });
+
+ $(document).on('keydown', '.complaint-name-input', function(e) {
+ var $dd = $('#discharge_row_complaint_dd');
+ var $items = $dd.find('.discharge-row-complaint-item');
+ if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
+ if (!$dd.is(':visible') || !$items.length) return;
+ e.preventDefault();
+ _dischargeRowDdIdx = e.key === 'ArrowDown'
+ ? Math.min(_dischargeRowDdIdx + 1, $items.length - 1)
+ : Math.max(_dischargeRowDdIdx - 1, 0);
+ $items.css('background', '').eq(_dischargeRowDdIdx).css('background', '#f0f4ff');
+ } else if (e.key === 'Enter') {
+ if ($dd.is(':visible') && _dischargeRowDdIdx >= 0 && _dischargeRowDdIdx < $items.length) {
+ e.preventDefault();
+ $items.eq(_dischargeRowDdIdx).trigger('click');
+ } else {
+ $(this).closest('tr').find('.complaint-freq-input').trigger('focus');
+ }
+ $dd.hide();
+ _dischargeRowDdIdx = -1;
+ } else if (e.key === 'Escape') {
+ $dd.hide();
+ _dischargeRowDdIdx = -1;
+ }
+ });
+
+ $(document).on('blur', '.complaint-name-input', function() {
+ setTimeout(function() {
+ $('#discharge_row_complaint_dd').hide();
+ _dischargeRowDdIdx = -1;
+ }, 200);
+ });
+
function getDischargeDurationSuggestions(input) {
input = (input || '').toString().trim();
var numMatch = input.match(/^(\d+\.?\d*)\s*(.*)/);
@@ -2542,6 +2806,9 @@ $historyFields = [
if (!$(e.target).closest('#discharge_complaint_lookup, #discharge_complaint_dropdown').length) {
closeDischargeComplaintDropdown();
}
+ if (!$(e.target).closest('.complaint-name-input, #discharge_row_complaint_dd').length) {
+ $('#discharge_row_complaint_dd').hide();
+ }
if (!$(e.target).closest('.complaint-freq-input, #discharge_freq_dd').length) {
$('#discharge_freq_dd').hide();
}
@@ -7750,7 +8017,7 @@ $historyFields = [
var actionValue = submitter && submitter.name === 'action' ? String(submitter.value || '') : '';
var saveMode = submitter && submitter.dataset ? String(submitter.dataset.saveMode || '').toLowerCase() : '';
var statusTargetId = submitter && submitter.dataset ? String(submitter.dataset.statusId || '').trim() : '';
- var isComplaintAction = actionValue === 'add_complaint' || actionValue === 'remove_complaint';
+ var isComplaintAction = actionValue === 'add_complaint' || actionValue === 'remove_complaint' || actionValue === 'update_complaint';
var isMedicineAction = actionValue === 'add_drug' || actionValue === 'remove_drug' || actionValue === 'apply_rx_group';
var targetSectionId = '';
@@ -7776,6 +8043,9 @@ $historyFields = [
if (isMedicineAction) {
targetSectionId = 'section-medicine';
}
+ if (isComplaintAction) {
+ targetSectionId = 'section-complaints';
+ }
if (actionValue === 'add_procedure') {
var procedureDateInput = form.querySelector('[name="new_procedure_date"]');
@@ -7797,7 +8067,7 @@ $historyFields = [
}).done(function(result) {
// The server always returns JSON for AJAX requests (isAJAX() === true in CI4).
// Because dataType is 'html', jQuery does NOT auto-parse it — result is a raw
- // JSON string. Detect this by attempting JSON.parse and handle medicine actions.
+ // JSON string. Detect this by attempting JSON.parse and handle medicine/complaint actions.
var parsedJson = null;
if (typeof result === 'string' && result.charAt(0) === '{') {
try { parsedJson = JSON.parse(result); } catch (e) { parsedJson = null; }
@@ -7819,11 +8089,21 @@ $historyFields = [
noticeLevel = noticeLevel === 'success' ? 'error' : noticeLevel;
}
if (noticeText !== '') {
- setSectionStatus('discharge_medicine_status', noticeText, noticeLevel === 'success' ? 'success' : 'error');
+ var statusIdToUse = statusTargetId !== '' ? statusTargetId : ((targetSectionId === 'section-complaints' || isComplaintAction) ? 'discharge_complaint_status' : 'discharge_medicine_status');
+ setSectionStatus(statusIdToUse, noticeText, noticeLevel === 'success' ? 'success' : 'error');
if (typeof window.notify === 'function') {
window.notify(noticeLevel === 'success' ? 'success' : 'error', 'Discharge Update', noticeText);
}
}
+ // Re-render complaint table rows from returned complaintRows array
+ if ((isComplaintAction || targetSectionId === 'section-complaints') && Array.isArray(parsedJson.complaintRows)) {
+ if (typeof selectedDischargeComplaints !== 'undefined') {
+ selectedDischargeComplaints = parsedJson.complaintRows;
+ if (typeof renderDischargeComplaintTable === 'function') {
+ renderDischargeComplaintTable();
+ }
+ }
+ }
// Re-render medicine table rows from the returned drugRows array
if (isMedicineAction && Array.isArray(parsedJson.drugRows)) {
var tbody = document.getElementById('discharge_medicine_tbody');