From 8f1381d6aa0224142f1bc39ba29b6d263ad0d91d Mon Sep 17 00:00:00 2001 From: zeeshan Date: Mon, 16 Nov 2026 21:05:55 +0530 Subject: [PATCH] Team 2 (Lung Manager Programme) Duplicate Doctor --- ...ffaa-1b76-40c8-800b-6b2d3c801016.blade.php | 130 +++++++++++++++++- 1 file changed, 128 insertions(+), 2 deletions(-) diff --git a/resources/views/form/rapport-team2/1794811995-cd07ffaa-1b76-40c8-800b-6b2d3c801016.blade.php b/resources/views/form/rapport-team2/1794811995-cd07ffaa-1b76-40c8-800b-6b2d3c801016.blade.php index 3a6b7818..3a3272cd 100644 --- a/resources/views/form/rapport-team2/1794811995-cd07ffaa-1b76-40c8-800b-6b2d3c801016.blade.php +++ b/resources/views/form/rapport-team2/1794811995-cd07ffaa-1b76-40c8-800b-6b2d3c801016.blade.php @@ -356,7 +356,7 @@ $('#field-speciality' + suffix).val(selectedOption.attr('speciality') || ''); } - function preventDuplicateDoctors() { + /* function preventDuplicateDoctors() { const selectedValues = []; $('select[id^="field-contact_id"]').each(function () { @@ -388,8 +388,80 @@ $select.select2({ width: '100%', placeholder: '-- Select Doctor --' }); $select.val(oldValue).trigger('change.select2'); }); + } */ + + function preventDuplicateDoctors() { + + const selectedValues = []; + + // Get all selected doctor IDs + $('select[id^="field-contact_id"]').each(function () { + + const value = $(this).val(); + + if (value) { + selectedValues.push(String(value)); + } + + }); + + // Disable already selected doctors in other dropdowns + $('select[id^="field-contact_id"]').each(function () { + + const $select = $(this); + + const currentValue = $select.val() + ? String($select.val()) + : ''; + + $select.find('option').each(function () { + + const $option = $(this); + const optionValue = String($option.val() || ''); + + if (!optionValue) return; + + const cleanText = $option.text() + .replace(' (Already Added)', ''); + + if ( + selectedValues.includes(optionValue) && + optionValue !== currentValue + ) { + + $option.prop('disabled', true); + + $option.text(cleanText + ' (Already Added)'); + + } else { + + $option.prop('disabled', false); + + $option.text(cleanText); + + } + + }); + + // Refresh Select2 + const oldValue = $select.val(); + + if ($select.hasClass('select2-hidden-accessible')) { + $select.select2('destroy'); + } + + $select.select2({ + width: '100%', + placeholder: '-- Select Doctor --' + }); + + $select.val(oldValue).trigger('change.select2'); + + }); + } + // All doctor dropdowns use the same change handler, including added wrappers. $(document).on('change', 'select[id^="field-contact_id"]', function () { updateDoctorMeta($(this)); @@ -425,7 +497,7 @@ Promise.all(requests).then(function (responses) { const doctorMap = new Map(); - responses.forEach(function (data) { + /* responses.forEach(function (data) { if (!Array.isArray(data)) return; data.forEach(function (doctor) { @@ -433,8 +505,40 @@ const key = String(doctor.value); if (!doctorMap.has(key)) doctorMap.set(key, doctor); }); + }); */ + + responses.forEach(function (data) { + if (!Array.isArray(data)) return; + + data.forEach(function (doctor) { + + if (!doctor || !doctor.contact) return; + + // Only Doctor type contacts + const contactType = String(doctor.contact.type || '').toLowerCase(); + + if (contactType !== 'doctor') { + return; + } + + if ( + doctor.value === undefined || + doctor.value === null || + doctor.value === '' + ) { + return; + } + + const key = String(doctor.value); + + // Remove duplicate doctor records + if (!doctorMap.has(key)) { + doctorMap.set(key, doctor); + } + }); }); + const allDoctors = Array.from(doctorMap.values()).sort(function (a, b) { return String(a.display || '').localeCompare(String(b.display || ''), 'en', { sensitivity: 'base' }); }); @@ -559,6 +663,28 @@ return !!$(this).val(); }).length; + + const selectedDoctorIds = []; + + $('select[id^="field-contact_id"]').each(function () { + + const doctorId = $(this).val(); + + if (doctorId) { + selectedDoctorIds.push(String(doctorId)); + } + + }); + + const uniqueDoctorIds = [...new Set(selectedDoctorIds)]; + + if (selectedDoctorIds.length !== uniqueDoctorIds.length) { + + alert('Duplicate doctors are not allowed.'); + + return; + } + if (!isEditMode && selectedDoctorCount !== 10) { alert('You must select exactly 10 doctors. Currently selected: ' + selectedDoctorCount); return;