-
+
+
+
+
@@ -2099,6 +2104,134 @@ $allergyStatusNoKnown = in_array($allergyStatusNormalized, ['no known drug aller
initDischargeComplaintsTable();
}
+ // Surgery/Procedure autocomplete with SNOMED/ICD code display
+ function initSurgeryProcedureAutocomplete() {
+ initTermAutocomplete('surgery', 'new_surgery_name', 'discharge_surgery_dropdown', 'new_surgery_master_id');
+ initTermAutocomplete('procedure', 'new_procedure_name', 'discharge_procedure_dropdown', 'new_procedure_master_id');
+ }
+
+ function initTermAutocomplete(type, inputId, dropdownId, hiddenId) {
+ var input = document.getElementById(inputId);
+ var dropdown = document.getElementById(dropdownId);
+ var hidden = document.getElementById(hiddenId);
+ if (!input || !dropdown || !hidden) return;
+
+ var searchTimer = null;
+ var highlightedIndex = -1;
+ var currentResults = [];
+
+ input.addEventListener('input', function() {
+ var q = input.value.trim();
+ highlightedIndex = -1;
+
+ if (q.length < 2) {
+ dropdown.style.display = 'none';
+ return;
+ }
+
+ clearTimeout(searchTimer);
+ searchTimer = setTimeout(function() {
+ if (!window.jQuery) return;
+
+ $.get('= base_url('Ipd_discharge/surgery_master_lookup') ?>?type=' + type + '&q=' + encodeURIComponent(q), function(data) {
+ currentResults = (data && data.rows) ? data.rows : [];
+ if (!currentResults.length) {
+ dropdown.style.display = 'none';
+ return;
+ }
+
+ var html = '';
+ currentResults.forEach(function(row) {
+ var name = row.term_name || '';
+ var code = row.term_code || '';
+ var icd = row.icd_code || '';
+ var codeDisplay = '';
+
+ if (code && icd) {
+ codeDisplay = '
' + code + '' + icd + '';
+ } else if (code) {
+ codeDisplay = '
' + code + '';
+ } else if (icd) {
+ codeDisplay = '
' + icd + '';
+ }
+
+ html += '
';
+ html += '
' + name + '
';
+ if (codeDisplay) {
+ html += '
' + codeDisplay + '
';
+ }
+ html += '
';
+ });
+
+ dropdown.innerHTML = html;
+ dropdown.style.display = 'block';
+
+ dropdown.querySelectorAll('.dropdown-item').forEach(function(item, idx) {
+ item.addEventListener('mouseenter', function() {
+ dropdown.querySelectorAll('.dropdown-item').forEach(function(el) {
+ el.style.backgroundColor = '';
+ });
+ this.style.backgroundColor = '#f8f9fa';
+ highlightedIndex = idx;
+ });
+ item.addEventListener('mouseleave', function() {
+ this.style.backgroundColor = '';
+ });
+ item.addEventListener('click', function() {
+ var id = this.getAttribute('data-id');
+ var name = this.getAttribute('data-name');
+ input.value = name;
+ hidden.value = id;
+ dropdown.style.display = 'none';
+ });
+ });
+ }, 'json');
+ }, 300);
+ });
+
+ input.addEventListener('keydown', function(e) {
+ var items = dropdown.querySelectorAll('.dropdown-item');
+ var isVisible = dropdown.style.display === 'block' && items.length > 0;
+
+ if (e.key === 'ArrowDown' && isVisible) {
+ e.preventDefault();
+ highlightedIndex = Math.min(highlightedIndex + 1, items.length - 1);
+ updateHighlight(items);
+ } else if (e.key === 'ArrowUp' && isVisible) {
+ e.preventDefault();
+ highlightedIndex = Math.max(highlightedIndex - 1, 0);
+ updateHighlight(items);
+ } else if (e.key === 'Enter' && isVisible) {
+ e.preventDefault();
+ if (highlightedIndex >= 0 && items[highlightedIndex]) {
+ items[highlightedIndex].click();
+ }
+ } else if (e.key === 'Escape') {
+ dropdown.style.display = 'none';
+ highlightedIndex = -1;
+ }
+ });
+
+ input.addEventListener('blur', function() {
+ setTimeout(function() {
+ dropdown.style.display = 'none';
+ }, 200);
+ });
+
+ function updateHighlight(items) {
+ items.forEach(function(item, idx) {
+ if (idx === highlightedIndex) {
+ item.style.backgroundColor = '#007bff';
+ item.style.color = '#fff';
+ item.scrollIntoView({ block: 'nearest' });
+ } else {
+ item.style.backgroundColor = '';
+ item.style.color = '';
+ }
+ });
+ }
+ }
+
function setSectionStatus(id, text, level) {
var el = document.getElementById(id);
if (!el) {
@@ -3503,6 +3636,7 @@ $allergyStatusNoKnown = in_array($allergyStatusNormalized, ['no known drug aller
initComplaintEditor();
initComplaintTools();
+ initSurgeryProcedureAutocomplete();
initSurgeryTools(getDischargeForm());
initDiagnosisTools();
initCourseTools();