';
+ if (data && parseInt(data.update || '0', 10) === 1) {
+ // Add row to table
+ var tbody = section.querySelector('#discharge_medicine_tbody');
+ if (!tbody) {
+ setMedicineStatus('Medicine table not found.', 'error');
+ return;
+ }
+
+ // Remove "No medicine added" row if present
+ var emptyRow = tbody.querySelector('tr td[colspan="9"]');
+ if (emptyRow) {
+ emptyRow.closest('tr').remove();
+ }
+
+ var tr = document.createElement('tr');
+ tr.innerHTML = '
' + $('
').text(medType).html() + '
'
+ + '
' + $('
').text(medName).html() + '
'
+ + '
' + $('
').text(doseLabel).html() + '
'
+ + '
' + $('
').text(whenLabel).html() + '
'
+ + '
' + $('
').text(freqLabel).html() + '
'
+ + '
' + $('
').text(formattedDuration).html() + '
'
+ + '
' + $('
').text(qty).html() + '
'
+ + '
' + $('
').text(remark).html() + '
'
+ + '
';
+ tbody.appendChild(tr);
+
+ // Clear form
+ $('#discharge_med_name, #discharge_med_type, #discharge_no_of_days, #discharge_qty, #discharge_remark').val('');
+ $('#discharge_dosage, #discharge_dosage_when, #discharge_dosage_freq, #discharge_dose_where').val('');
+ $('#discharge_med_name').focus();
+
+ setMedicineStatus('Medicine saved successfully.', 'success');
+
+ // Bind remove button (Note: Remove is not yet AJAX - medicines load from DB on page reload)
+ tr.querySelector('.btn-remove-discharge-med').addEventListener('click', function() {
+ if (confirm('Remove this medicine? This will only remove from the current view. Reload the page to see saved medicines.')) {
+ tr.remove();
+ if (tbody.children.length === 0) {
+ tbody.innerHTML = '
No medicine added
';
+ }
+ setMedicineStatus('Medicine removed from view. Reload to see saved data.', 'info');
+ }
+ });
+ } else {
+ var errorMsg = (data && data.error_text) ? data.error_text : 'Unable to save medicine.';
+ setMedicineStatus(errorMsg, 'error');
}
- setMedicineStatus('Medicine removed from list.', 'info');
+ }).fail(function(xhr, status, error) {
+ setMedicineStatus('Network error: ' + (error || status), 'error');
});
});
}