584 lines
26 KiB
PHP
584 lines
26 KiB
PHP
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
@include('layouts.shared/title-meta', ['title' => 'Form'])
|
|
@include('layouts.shared/head-css', ['mode' => $mode ?? '', 'demo' => $demo ?? ''])
|
|
<link href="{{ asset('assets/libs/sweetalert2/sweetalert2.min.css') }}" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<div id="pageloader">
|
|
<img src="http://cdnjs.cloudflare.com/ajax/libs/semantic-ui/0.16.1/images/loader-large.gif" alt="processing..." />
|
|
</div>
|
|
|
|
{{-- <div>
|
|
@php
|
|
echo "<pre>";
|
|
print_r($questions);
|
|
@endphp
|
|
</div> --}}
|
|
|
|
<body class="loading">
|
|
<div class="account-pages">
|
|
<div class="container">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8 col-lg-6 col-xl-5 p-0">
|
|
<div class="card bg-pattern">
|
|
<div class="card-body p-3 row">
|
|
|
|
@if (Session::has('error'))
|
|
<div class="alert alert-danger">{{ Session::get('error') }}</div>
|
|
<br>
|
|
@endif
|
|
@if (session('success'))
|
|
<div class=" alert alert-success">{{ session('success') }}
|
|
</div>
|
|
<br>
|
|
@endif
|
|
<div class="col-8">
|
|
<h4 class="page-title">{{ $form->title }}</h4>
|
|
</div>
|
|
@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>
|
|
@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 }}">
|
|
|
|
@csrf
|
|
@if ($id)
|
|
<input type="hidden" name="id" value="{{ $id }}">
|
|
@endif
|
|
@if (isset($form->manager_can_submit) && $form->manager_can_submit == true)
|
|
<div class="col-md-12 mb-2">
|
|
<label for="field-user_id" class="form-label"> Select User / Territory <span
|
|
class="text-danger">*</span> </label>
|
|
<select class="form-control" id="field-user_id" name="user_id" required>
|
|
<option value=""> -- Select -- </option>
|
|
@foreach ($form->users as $option)
|
|
<option value="{{ $option->value }}"
|
|
hierarchy="{{ $option->hierarchy_id }}"
|
|
{{ isset($question->value) && $question->value == $option->value ? 'selected' : '' }}>
|
|
{{ $option->display }}</option>
|
|
@endforeach
|
|
</select>
|
|
<input type="hidden" id="field-hierarchy_id" name="hierarchy_id"
|
|
value="">
|
|
</div>
|
|
@endif
|
|
|
|
@foreach ($questions as $question)
|
|
<div class="col-md-12 mb-2">
|
|
@if ($question->code == 'category')
|
|
@else
|
|
<label for="field-{{ $question->code }}"
|
|
class="form-label">{{ $question->question }} {!! $question->required == 'yes' ? '<span class="text-danger">*</span>' : '' !!}
|
|
</label>
|
|
@endif
|
|
@switch($question->type)
|
|
@case('hidden')
|
|
<input type="{{ $question->type }}" id="field-{{ $question->code }}"
|
|
name="{{ $question->code }}"
|
|
value="{{ isset($question->value) ? $question->value : '' }}">
|
|
@break
|
|
|
|
@case('text')
|
|
@case('email')
|
|
|
|
@case('tel')
|
|
@case('date')
|
|
<input type="{{ $question->type }}" id="field-{{ $question->code }}"
|
|
name="{{ $question->code }}" class="form-control"
|
|
{{ $question->required == 'yes' ? 'required' : '' }}
|
|
value="{{ isset($question->value) ? $question->value : '' }}">
|
|
@break
|
|
|
|
@case('number')
|
|
<input type="{{ $question->type }}" min="0" max="9999999999"
|
|
step="1" id="field-{{ $question->code }}"
|
|
name="{{ $question->code }}" class="form-control"
|
|
{{ $question->required == 'yes' ? 'required' : '' }}
|
|
value="{{ isset($question->value) ? $question->value : 0 }}">
|
|
@break
|
|
|
|
@case('textarea')
|
|
<textarea id="field-{{ $question->code }}" name="{{ $question->code }}" class="form-control"
|
|
{{ $question->required == 'yes' ? 'required' : '' }}>{{ isset($question->value) ? $question->value : '' }}</textarea>
|
|
@break
|
|
|
|
@case('dropdown')
|
|
@if ($question->code == 'category')
|
|
<div class="category"
|
|
style="border: 1.5px solid #007bff; border-radius: 8px; padding: 10px 14px;">
|
|
<label for="field-{{ $question->code }}"
|
|
class="form-label">{{ $question->question }}
|
|
{!! $question->required == 'yes' ? '<span class="text-danger">*</span>' : '' !!} </label>
|
|
<select class="form-control" id="field-{{ $question->code }}"
|
|
name="{{ $question->code }}"
|
|
{{ $question->required == 'yes' ? 'required' : '' }}>
|
|
<option value=""> -- Select -- </option>
|
|
@foreach ($question->options as $option)
|
|
<option value="{{ $option->value }}"
|
|
{{ isset($question->value) && $question->value == $option->value ? 'selected' : '' }}>
|
|
{{ $option->display }}</option>
|
|
@endforeach
|
|
</select>
|
|
</div>
|
|
@elseif(preg_match('/^doctor_name_(?:[1-9]|1[0-9]|20)$/', $question->code))
|
|
<select class="form-control" id="field-{{ $question->code }}"
|
|
name="{{ $question->code }}"
|
|
{{ $question->required == 'yes' ? 'required' : '' }}>
|
|
<option value=""> -- Select -- </option>
|
|
@foreach ($question->options as $option)
|
|
@if(isset($option->contact->type) && $option->contact->type != 'chemist' )
|
|
<option
|
|
value="{{ $option->value }}"
|
|
{{-- po5_synchrobreathe_non_prescriber="{{ isset($option->contact->po5_synchrobreathe_non_prescriber) ? $option->contact->po5_synchrobreathe_non_prescriber : '' }}"
|
|
po5_ciphaler_non_prescriber="{{ isset($option->contact->po5_ciphaler_non_prescriber) ? $option->contact->po5_ciphaler_non_prescriber : '' }}" --}}
|
|
{{ isset($question->value) && $question->value == $option->value ? 'selected' : '' }}>
|
|
{{ $option->display }}
|
|
</option>
|
|
@endif
|
|
@endforeach
|
|
</select>
|
|
@else
|
|
<select class="form-control" id="field-{{$question->code}}" name="{{$question->code}}" {{$question->required == 'yes'?'required':'' }}>
|
|
<option value=""> -- Select -- </option>
|
|
@foreach($question->options as $option)
|
|
<option value="{{$option->value}}" {{isset($question->value) && $question->value == $option->value ? 'selected':''}}>{{$option->display}}</option>
|
|
@endforeach
|
|
</select>
|
|
@endif
|
|
@break
|
|
|
|
@case('checkbox')
|
|
@foreach ($question->options as $option)
|
|
<div class="form-check mb-2 form-check-primary">
|
|
<input class="form-check-input" type="checkbox"
|
|
id="field-{{ $question->code }}-{{ $option->value }}"
|
|
name="{{ $question->code }}[]" value="{{ $option->value }}"
|
|
{{ isset($question->value) && $question->value != '' && in_array($option->value, $question->value) ? 'checked' : '' }}>
|
|
<label class="form-check-label"
|
|
for="field-{{ $question->code }}-{{ $option->value }}">{{ $option->display }}</label>
|
|
</div>
|
|
@endforeach
|
|
@break
|
|
|
|
@case('file')
|
|
<input type="file" id="field-{{ $question->code }}"
|
|
name="{{ $question->code }}" class="form-control" value=""
|
|
accept="image/jpg, image/jpeg, image/png"
|
|
{{ $question->required == 'yes' ? 'required' : '' }}>
|
|
@break
|
|
|
|
@case('multitextbox')
|
|
<div class="row">
|
|
@foreach ($question->options as $option)
|
|
<div class="col-6">{{ $option->display }}</div>
|
|
<div class="col-6 mb-1">
|
|
<input type="number" id="field-{{ $option->value }}"
|
|
name="{{ $option->value }}" class="form-control" required
|
|
value="{{ isset($option->val) ? $option->val : 0 }}">
|
|
</div>
|
|
@endforeach
|
|
</div>
|
|
@break
|
|
@endswitch
|
|
</div>
|
|
@endforeach
|
|
@if (!isset($form->is_approval) || (isset($form->is_approval) && $form->is_approval != 'yes'))
|
|
<div class="text-center d-grid mt-3">
|
|
<button class="btn btn-primary" type="submit" id="submit-btn"> Submit
|
|
</button>
|
|
</div>
|
|
@endif
|
|
</form>
|
|
|
|
</div> <!-- end card-body -->
|
|
</div>
|
|
<!-- end card -->
|
|
|
|
<!-- end row -->
|
|
|
|
</div> <!-- end col -->
|
|
</div>
|
|
<!-- end row -->
|
|
</div>
|
|
<!-- end container -->
|
|
</div>
|
|
<!-- end page -->
|
|
<style type="text/css">
|
|
#pageloader {
|
|
background: rgba(255, 255, 255, 0.8);
|
|
display: none;
|
|
height: 100%;
|
|
position: fixed;
|
|
width: 100%;
|
|
z-index: 9999;
|
|
}
|
|
|
|
#pageloader img {
|
|
left: 50%;
|
|
margin-left: -32px;
|
|
margin-top: -32px;
|
|
position: absolute;
|
|
top: 50%;
|
|
}
|
|
</style>
|
|
@include('layouts.shared/footer-script')
|
|
<script src="{{ asset('assets/libs/sweetalert2/sweetalert2.min.js') }}"></script>
|
|
<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)
|
|
event.preventDefault();
|
|
$.ajax({
|
|
type: "GET",
|
|
url: '/form/getdoctorlist?partition={{ $form->partition }}&hierarchy=' +
|
|
hierarchy,
|
|
processData: false,
|
|
contentType: false,
|
|
success: function(data) {
|
|
$("#pageloader").fadeOut();
|
|
$('#field-contact_id').html('')
|
|
$('#field-contact_id').append($("<option></option>").attr("value", '')
|
|
.text('-- Select --'));
|
|
for (let itm of data) {
|
|
$('#field-contact_id').append($("<option></option>").attr("value",
|
|
itm.value).text(itm.display));
|
|
}
|
|
},
|
|
});
|
|
|
|
console.log(hierarchy)
|
|
})
|
|
|
|
$('#report-form').on('submit', function(event) {
|
|
event.preventDefault();
|
|
$("#pageloader").fadeIn();
|
|
$.ajax({
|
|
type: "POST",
|
|
url: '/form/ajaxsubmitmultiple',
|
|
data: new FormData($('#report-form')[0]),
|
|
processData: false,
|
|
contentType: false,
|
|
success: function(data) {
|
|
$("#pageloader").fadeOut();
|
|
if (data.status == 'success') {
|
|
Swal.fire({
|
|
title: 'Great!',
|
|
text: 'Your data is successfully submitted!',
|
|
icon: 'success'
|
|
}).then(() => {
|
|
location.reload(); // 🔥 page refresh here
|
|
});
|
|
$('#report-form').trigger("reset");
|
|
} else {
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Data not submitted',
|
|
text: 'Something went wrong, please try again after some time!'
|
|
})
|
|
}
|
|
},
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<script>
|
|
function updateDoctorOptions() {
|
|
let selected = [];
|
|
|
|
$('[id^="field-doctor_name_"]').each(function () {
|
|
let val = $(this).val();
|
|
if (val) selected.push(val);
|
|
});
|
|
|
|
$('[id^="field-doctor_name_"]').each(function () {
|
|
let currentVal = $(this).val();
|
|
|
|
$(this).find('option').each(function () {
|
|
let optionVal = $(this).val();
|
|
|
|
if (optionVal && selected.includes(optionVal) && optionVal !== currentVal) {
|
|
$(this).prop('disabled', true);
|
|
} else {
|
|
$(this).prop('disabled', false);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
// trigger on change
|
|
$(document).on('change', '[id^="field-doctor_name_"]', function () {
|
|
updateDoctorOptions();
|
|
});
|
|
|
|
// trigger on page load
|
|
$(document).ready(function () {
|
|
updateDoctorOptions();
|
|
});
|
|
</script>
|
|
|
|
@if(!empty($submissions))
|
|
<script>
|
|
$(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();
|
|
});
|
|
|
|
});
|
|
</script>
|
|
@endif
|
|
</body>
|
|
|
|
</html>
|