Team 2 (Form Changes)
This commit is contained in:
parent
463d464acb
commit
8192185faa
|
|
@ -40,25 +40,14 @@
|
|||
@if (!isset($form->is_approval) || (isset($form->is_approval) && $form->is_approval != 'yes'))
|
||||
<div class="col-4">
|
||||
@if ($form->link_type == 'form')
|
||||
<a href="/form/history" class="float-end pt-1">View History</a>
|
||||
<!-- <a href="/form/history" class="float-end pt-1">View History</a> -->
|
||||
@else
|
||||
<a href="{{ url()->previous() }}" class="float-end pt-1">Back</a>
|
||||
@endif
|
||||
</div>
|
||||
@endif
|
||||
|
||||
@php
|
||||
$history = Formhelper::getMultiformData(
|
||||
$form->partition,
|
||||
$form->form_id,
|
||||
$form->user_id,
|
||||
);
|
||||
$submissions = $history->data->submissions ?? [];
|
||||
// echo "<pre>";
|
||||
// print_r($submissions);
|
||||
|
||||
|
||||
@endphp
|
||||
|
||||
<form method="POST" action="/form/submit" id="report-form" enctype="multipart/form-data">
|
||||
<input type="hidden" name="form_id" value="{{ $form_id }}">
|
||||
<input type="hidden" name="user_id" value="{{ $form->user_id }}">
|
||||
|
|
@ -142,7 +131,7 @@
|
|||
@endforeach
|
||||
</select>
|
||||
</div>
|
||||
@elseif(preg_match('/^doctor_name_(?:[1-9]|1[0-9]|20)$/', $question->code))
|
||||
@elseif(preg_match('/^doctor_name_(?:[1-9]|[1-3][0-9]|4[0-5])$/', $question->code))
|
||||
<select class="form-control" id="field-{{ $question->code }}"
|
||||
name="{{ $question->code }}"
|
||||
{{ $question->required == 'yes' ? 'required' : '' }}>
|
||||
|
|
@ -253,9 +242,6 @@
|
|||
<script type="text/javascript">
|
||||
jQuery(document).ready(function($) {
|
||||
|
||||
|
||||
|
||||
|
||||
$('#field-user_id').change(function() {
|
||||
let hierarchy = $('option:selected', this).attr('hierarchy');
|
||||
$('#field-hierarchy_id').val(hierarchy)
|
||||
|
|
@ -286,7 +272,7 @@
|
|||
$("#pageloader").fadeIn();
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: '/form/ajaxsubmitmultiple',
|
||||
url: '/form/ajaxsubmit',
|
||||
data: new FormData($('#report-form')[0]),
|
||||
processData: false,
|
||||
contentType: false,
|
||||
|
|
@ -298,7 +284,7 @@
|
|||
text: 'Your data is successfully submitted!',
|
||||
icon: 'success'
|
||||
})
|
||||
$('#report-form').trigger("reset");
|
||||
//$('#report-form').trigger("reset");
|
||||
} else {
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
|
|
@ -347,242 +333,66 @@
|
|||
});
|
||||
</script>
|
||||
|
||||
@if(!empty($submissions))
|
||||
@php
|
||||
// Find the first question of type 'contacts'
|
||||
$contactsQuestion = null;
|
||||
foreach ($questions as $q) {
|
||||
if (strpos($q->code, 'doctor_name_') === 0) {
|
||||
$contactsQuestion = $q;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$contactArr = [];
|
||||
if ($contactsQuestion && isset($contactsQuestion->options)) {
|
||||
foreach ($contactsQuestion->options as $option) {
|
||||
if (
|
||||
isset($option->contact) &&
|
||||
isset($option->contact->type) &&
|
||||
strtolower(trim($option->contact->type)) === 'doctor' &&
|
||||
(isset($option->contact->po5_ace_meeting) && strtolower(trim($option->contact->po5_ace_meeting)) === 'yes')
|
||||
) {
|
||||
$contactArr[] = $option->contact;
|
||||
}
|
||||
}
|
||||
}
|
||||
@endphp
|
||||
|
||||
<script>
|
||||
// Extract contacts of type 'contacts' and filter doctors with po5_ace_meeting == 'Yes'
|
||||
// Convert PHP doctor contacts array to JS
|
||||
var aceDoctors = {!! json_encode($contactArr) !!};
|
||||
// Example: log to console all doctors with po5_ace_meeting = Yes
|
||||
console.log("Doctors with po5_ace_meeting == Yes:", aceDoctors);
|
||||
|
||||
// Ensure the hidden fields are appended inside the form element
|
||||
$(document).ready(function() {
|
||||
|
||||
console.log("🚀 Script Loaded (FINAL HYBRID FIX)");
|
||||
|
||||
let logs = [];
|
||||
|
||||
function log(msg, data = null) {
|
||||
if (data) {
|
||||
logs.push(msg + ' ' + JSON.stringify(data).substring(0, 100));
|
||||
} else {
|
||||
logs.push(msg);
|
||||
}
|
||||
}
|
||||
|
||||
function showLogs() {
|
||||
console.log('🧾 FINAL LOGS:\n' + logs.join('\n'));
|
||||
}
|
||||
|
||||
function short(val) {
|
||||
if (!val) return val;
|
||||
return val.toString().substring(0, 8) + '...';
|
||||
}
|
||||
|
||||
const doctorSelects = $('[id^="field-doctor_name_"]');
|
||||
const submissionData = @json($submissions);
|
||||
|
||||
// ==============================
|
||||
// 🌍 Get ALL used doctor IDs
|
||||
// ==============================
|
||||
function getGloballyUsedDoctorIds() {
|
||||
|
||||
let usedIds = [];
|
||||
|
||||
Object.values(submissionData).forEach(item => {
|
||||
if (item.contacts && item.contacts.length) {
|
||||
item.contacts.forEach(contact => {
|
||||
if (contact.type === 'doctor' && contact.id) {
|
||||
usedIds.push(contact.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return [...new Set(usedIds)];
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// ⚡ Get LIVE selected IDs
|
||||
// ==============================
|
||||
function getCurrentlySelectedDoctorIds() {
|
||||
|
||||
let ids = [];
|
||||
|
||||
doctorSelects.each(function() {
|
||||
let val = $(this).val();
|
||||
if (val) ids.push(val);
|
||||
});
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// 🔁 Prevent duplicates
|
||||
// ==============================
|
||||
function refreshDoctorOptions() {
|
||||
|
||||
const selected = getCurrentlySelectedDoctorIds();
|
||||
const dbUsed = getGloballyUsedDoctorIds();
|
||||
|
||||
const finalUsed = [...new Set([...dbUsed, ...selected])];
|
||||
|
||||
doctorSelects.each(function() {
|
||||
|
||||
const $select = $(this);
|
||||
const current = $select.val();
|
||||
|
||||
// Reset
|
||||
$select.find('option').prop('disabled', false).css({
|
||||
'color': '',
|
||||
'background-color': ''
|
||||
});
|
||||
|
||||
// Clean labels
|
||||
$select.find('option').each(function() {
|
||||
let txt = $(this).text()
|
||||
.replace(' (Selected)', '')
|
||||
.replace(' (In Use)', '');
|
||||
$(this).text(txt);
|
||||
});
|
||||
|
||||
// Same form duplicates
|
||||
selected.forEach(id => {
|
||||
if (id && id !== current) {
|
||||
const $opt = $select.find(`option[value="${id}"]`);
|
||||
$opt.prop('disabled', true).css({
|
||||
'color': '#999',
|
||||
'background-color': '#f5f5f5'
|
||||
});
|
||||
if (!$opt.text().includes('(Selected)')) {
|
||||
$opt.text($opt.text() + ' (Selected)');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Global restriction
|
||||
finalUsed.forEach(id => {
|
||||
if (id && id !== current) {
|
||||
const $opt = $select.find(`option[value="${id}"]`);
|
||||
$opt.prop('disabled', true).css({
|
||||
'color': '#bbb',
|
||||
'background-color': '#f2f2f2'
|
||||
});
|
||||
if (!$opt.text().includes('(In Use)')) {
|
||||
$opt.text($opt.text() + ' (In Use)');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// 🔥 Populate using HYBRID mapping
|
||||
// ==============================
|
||||
function populateDoctors() {
|
||||
|
||||
const match = Object.values(submissionData)
|
||||
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))[0];
|
||||
|
||||
log('Latest submission found', !!match);
|
||||
|
||||
doctorSelects.val('');
|
||||
|
||||
if (!match || !match.formdata || !match.contacts) {
|
||||
log('Missing formdata or contacts');
|
||||
return;
|
||||
}
|
||||
|
||||
const formdata = match.formdata;
|
||||
const contacts = match.contacts;
|
||||
|
||||
// 🔥 Create NAME → ID map (only doctors)
|
||||
let doctorMap = {};
|
||||
|
||||
contacts.forEach(contact => {
|
||||
if (contact.type === 'doctor' && contact.name && contact.id) {
|
||||
doctorMap[contact.name.trim()] = contact.id;
|
||||
}
|
||||
});
|
||||
|
||||
log('Doctor Map Size', Object.keys(doctorMap).length);
|
||||
|
||||
// 🔁 Loop fixed positions
|
||||
for (let i = 1; i <= 20; i++) {
|
||||
|
||||
const doctorName = formdata[`doctor_name_${i}`];
|
||||
if (!doctorName) continue;
|
||||
|
||||
const doctorId = doctorMap[doctorName.trim()];
|
||||
|
||||
const $select = $(`#field-doctor_name_${i}`);
|
||||
if (!$select.length) continue;
|
||||
|
||||
if (!doctorId) {
|
||||
log(`❌ ID not found`, doctorName);
|
||||
continue;
|
||||
}
|
||||
|
||||
log(`Mapping doctor_${i}`, {
|
||||
name: doctorName,
|
||||
id: short(doctorId)
|
||||
});
|
||||
|
||||
function trySet() {
|
||||
|
||||
const $opt = $select.find(`option[value="${doctorId}"]`);
|
||||
|
||||
if ($opt.length) {
|
||||
|
||||
$opt.prop('disabled', false); // important
|
||||
|
||||
$select.val(doctorId).trigger('change');
|
||||
|
||||
log(`✅ Set doctor_${i}`, short(doctorId));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try immediately
|
||||
if (trySet()) continue;
|
||||
|
||||
// 🔥 Observe async loading
|
||||
const observer = new MutationObserver(() => {
|
||||
|
||||
if (trySet()) {
|
||||
observer.disconnect();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
observer.observe($select[0], {
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
observer.disconnect();
|
||||
log(`⏰ Timeout doctor_${i}`, doctorName);
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
refreshDoctorOptions();
|
||||
showLogs();
|
||||
}, 1500);
|
||||
}
|
||||
|
||||
// ==============================
|
||||
// 🚀 Init
|
||||
// ==============================
|
||||
setTimeout(() => {
|
||||
populateDoctors();
|
||||
}, 300);
|
||||
|
||||
doctorSelects.on('change', function() {
|
||||
refreshDoctorOptions();
|
||||
var $form = $('#report-form');
|
||||
// Remove any old existing_doctors[] hidden fields to avoid duplication
|
||||
$form.find('input[name="existing_doctors[]"]').remove();
|
||||
aceDoctors.forEach(function(doctor) {
|
||||
var $input = $('<input>')
|
||||
.attr('type', 'hidden')
|
||||
.attr('name', 'existing_doctors[]')
|
||||
.val(doctor.id);
|
||||
$form.append($input);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Assign each aceDoctor to its corresponding dropdown in order - first doctor to first dropdown, second to second, etc.
|
||||
$('[id^="field-doctor_name_"]').each(function(index, dropdown) {
|
||||
if (aceDoctors[index]) {
|
||||
// Set the dropdown value to the doctor's id at the same index
|
||||
$(dropdown).val(aceDoctors[index].id);
|
||||
// Optionally, trigger change for listeners
|
||||
$(dropdown).trigger('change');
|
||||
// console.log("Selected doctor for dropdown", dropdown.id, aceDoctors[index]);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@endif
|
||||
|
||||
|
||||
|
||||
</body>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue