diff --git a/resources/views/form/rapport-team3/1793772514-e8e67206-8dba-471e-b08f-deece5d36d11.blade.php b/resources/views/form/rapport-team3/1793772514-e8e67206-8dba-471e-b08f-deece5d36d11.blade.php index 43937f46..0413fd90 100644 --- a/resources/views/form/rapport-team3/1793772514-e8e67206-8dba-471e-b08f-deece5d36d11.blade.php +++ b/resources/views/form/rapport-team3/1793772514-e8e67206-8dba-471e-b08f-deece5d36d11.blade.php @@ -366,9 +366,8 @@ $('#field-msl_code,#field-pcode,#field-speciality').attr('readonly',true); - /* $('#field-brand_name').on('input', function () { - this.value = this.value.replace(/[^a-zA-Z\s]/g, ''); - }); */ + var isEditMode = @json((bool)$id); + $(document).on('input', 'input[id^="field-brand_name"]', function () { @@ -392,6 +391,7 @@ }); } + initializeSelect2(); preventDuplicateDoctors(); @@ -466,51 +466,62 @@ } */ function preventDuplicateDoctors() { - let selectedValues = []; + let selectedDoctorIds = []; - // Collect all selected doctor IDs (ignoring empty selections) - $('select[id^="field-contact_id"]').each(function () { - let val = $(this).val(); - if (val) { - selectedValues.push(val); + // Step 1: Collect all currently selected doctor values + $('select[id^="field-contact_id"]').each(function () { + let val = $(this).val(); + if (val && val !== '') { + selectedDoctorIds.push(String(val)); + } + }); + + // Step 2: Traverse each doctor select element and update option states + $('select[id^="field-contact_id"]').each(function () { + let $currentSelect = $(this); + let currentVal = String($currentSelect.val() || ''); + + $currentSelect.find('option').each(function () { + let $option = $(this); + let optionVal = String($option.val() || ''); + + // Skip the default placeholder option + if (!optionVal) return; + + // Clean any previously appended '(Already Added)' tags + let rawText = $option.text().replace(/\s*\(Already Added\)/g, '').trim(); + + // If selected in another dropdown, disable and tag it + if (selectedDoctorIds.includes(optionVal) && optionVal !== currentVal) { + $option.prop('disabled', true); + $option.text(rawText + ' (Already Added)'); + } else { + $option.prop('disabled', false); + $option.text(rawText); + } + }); + + // Step 3: Refresh Select2 cleanly to render updated disabled states + if ($currentSelect.hasClass("select2-hidden-accessible")) { + $currentSelect.select2('destroy'); + } + $currentSelect.select2({ + width: '100%', + placeholder: '-- Select Doctor --' + }); + + // Preserve the selected value + $currentSelect.val(currentVal); + }); } - }); - - // Update options for each select - $('select[id^="field-contact_id"]').each(function () { - let currentSelect = $(this); - let currentValue = currentSelect.val(); - - currentSelect.find('option').each(function () { - let option = $(this); - let optionValue = option.val(); - - if (!optionValue) return; - - // Clean previously appended text - let originalText = option.text().replace(' (Already Added)', '').trim(); - - // If selected in another dropdown, disable it and add indicator - if (selectedValues.includes(optionValue) && optionValue !== currentValue) { - option.prop('disabled', true); - option.text(originalText + ' (Already Added)'); - } else { - option.prop('disabled', false); - option.text(originalText); - } - }); - - // Trigger change to update Select2 without destroying entire DOM binding - currentSelect.trigger('change.select2'); - }); -} $(document).on('change', 'select[id^="field-contact_id"]', function () { let selectedOption = $(this).find(':selected'); - let id = $(this).attr('id').split('-').pop(); // get doctor_count dynamically + let id = $(this).attr('id').split('-'); // get doctor_count dynamically + let suffix = idParts.length > 2 ? '-' + idParts[2] : ''; $('#field-msl_code-' + id).val(selectedOption.attr('msl_code')); $('#field-pcode-' + id).val(selectedOption.attr('pcode')); @@ -702,7 +713,7 @@ console.log(hierarchy) }) - var isEditMode = @json((bool)$id); + $('#report-form').on('submit', function(event) { event.preventDefault(); /* if (numberOfSubmission !=9) {