Compare commits
2 Commits
cf768ce3df
...
dd1015dd8a
| Author | SHA1 | Date |
|---|---|---|
|
|
dd1015dd8a | |
|
|
8192185faa |
|
|
@ -40,25 +40,14 @@
|
||||||
@if (!isset($form->is_approval) || (isset($form->is_approval) && $form->is_approval != 'yes'))
|
@if (!isset($form->is_approval) || (isset($form->is_approval) && $form->is_approval != 'yes'))
|
||||||
<div class="col-4">
|
<div class="col-4">
|
||||||
@if ($form->link_type == 'form')
|
@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
|
@else
|
||||||
<a href="{{ url()->previous() }}" class="float-end pt-1">Back</a>
|
<a href="{{ url()->previous() }}" class="float-end pt-1">Back</a>
|
||||||
@endif
|
@endif
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@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">
|
<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="form_id" value="{{ $form_id }}">
|
||||||
<input type="hidden" name="user_id" value="{{ $form->user_id }}">
|
<input type="hidden" name="user_id" value="{{ $form->user_id }}">
|
||||||
|
|
@ -142,7 +131,7 @@
|
||||||
@endforeach
|
@endforeach
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</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 }}"
|
<select class="form-control" id="field-{{ $question->code }}"
|
||||||
name="{{ $question->code }}"
|
name="{{ $question->code }}"
|
||||||
{{ $question->required == 'yes' ? 'required' : '' }}>
|
{{ $question->required == 'yes' ? 'required' : '' }}>
|
||||||
|
|
@ -253,9 +242,6 @@
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
jQuery(document).ready(function($) {
|
jQuery(document).ready(function($) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$('#field-user_id').change(function() {
|
$('#field-user_id').change(function() {
|
||||||
let hierarchy = $('option:selected', this).attr('hierarchy');
|
let hierarchy = $('option:selected', this).attr('hierarchy');
|
||||||
$('#field-hierarchy_id').val(hierarchy)
|
$('#field-hierarchy_id').val(hierarchy)
|
||||||
|
|
@ -286,7 +272,7 @@
|
||||||
$("#pageloader").fadeIn();
|
$("#pageloader").fadeIn();
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: "POST",
|
type: "POST",
|
||||||
url: '/form/ajaxsubmitmultiple',
|
url: '/form/ajaxsubmit',
|
||||||
data: new FormData($('#report-form')[0]),
|
data: new FormData($('#report-form')[0]),
|
||||||
processData: false,
|
processData: false,
|
||||||
contentType: false,
|
contentType: false,
|
||||||
|
|
@ -298,7 +284,7 @@
|
||||||
text: 'Your data is successfully submitted!',
|
text: 'Your data is successfully submitted!',
|
||||||
icon: 'success'
|
icon: 'success'
|
||||||
})
|
})
|
||||||
$('#report-form').trigger("reset");
|
//$('#report-form').trigger("reset");
|
||||||
} else {
|
} else {
|
||||||
Swal.fire({
|
Swal.fire({
|
||||||
icon: 'error',
|
icon: 'error',
|
||||||
|
|
@ -347,242 +333,66 @@
|
||||||
});
|
});
|
||||||
</script>
|
</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>
|
<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() {
|
$(document).ready(function() {
|
||||||
|
var $form = $('#report-form');
|
||||||
console.log("🚀 Script Loaded (FINAL HYBRID FIX)");
|
// Remove any old existing_doctors[] hidden fields to avoid duplication
|
||||||
|
$form.find('input[name="existing_doctors[]"]').remove();
|
||||||
let logs = [];
|
aceDoctors.forEach(function(doctor) {
|
||||||
|
var $input = $('<input>')
|
||||||
function log(msg, data = null) {
|
.attr('type', 'hidden')
|
||||||
if (data) {
|
.attr('name', 'existing_doctors[]')
|
||||||
logs.push(msg + ' ' + JSON.stringify(data).substring(0, 100));
|
.val(doctor.id);
|
||||||
} else {
|
$form.append($input);
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// 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>
|
</script>
|
||||||
@endif
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue