Rapport-Laravel/resources/views/form/rapport-team3/1756354787-47cecdc2-8df4-4b...

712 lines
37 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>
<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
@if (isset($form->user_info))
<div class="col-12">
<h6>Submitted By: {{ $form->user_info->name }}</h6>
</div>
@endif
@php
$history = Formhelper::getMultiformData(
$form->partition,
'1756354787-47cecdc2-8df4-4b7c-adbe-e579dcb42b13',
$form->user_id,
);
$submissions = isset($history->data->submissions) && is_array($history->data->submissions)
? $history->data->submissions
: [];
$subcount = count($submissions);
$previousDoctors = [];
foreach ($submissions as $sub) {
if (!empty($sub->formdata->cont_id) && strtolower($sub->formdata->cont_id) !== 'other') {
$previousDoctors[] = $sub->formdata->cont_id;
}
}
$uniquesdocpreviousselected = implode(',', array_unique($previousDoctors));
// $subcount = count($history->data->submissions);
// echo '<pre>';
// print_r($history->data->submissions);
// echo count($history->data->submissions)
// exit;
@endphp
<input type="hidden" name="submissioncount" id="field-submissioncount" value="{{$subcount}}">
<input type="hidden" name="submissiondoctors" id="field-submissiondoctors" value="{{ $uniquesdocpreviousselected }}">
<form method="POST" action="/form/submit" id="report-form" enctype="multipart/form-data">
<input type="hidden" name="form_id" value="{{ $form_id }}">
@csrf
@if ($id)
<input type="hidden" name="id[0]" 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)
@if (!in_array($question->code, ['record_date', 'select_month']))
@continue;
@endif
<div class="col-md-12 mb-2">
<label for="field-{{ $question->code }}"
class="form-label">{{ $question->question }}
{!! $question->required == 'yes' ? '<span class="text-danger">*</span>' : '' !!}
</label>
@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')
<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' : '' }}
{{ $question->code == 'contact_id' ? 'msl_code= ' . $option->contact->msl_code . ' ' : '' }}
{{ $question->code == 'doctor_name' ? 'speciality= ' . (isset($option->contact->speciality) ? $option->contact->speciality : '') . ' ' : '' }}>
{{ $option->display }}
</option>
@endforeach
</select>
@break
@endswitch
</div>
@endforeach
<div class="doctor-wrapper">
@foreach ($questions as $question)
@if (in_array($question->code, ['record_date', 'select_month']))
@continue;
@endif
<div class="col-md-12 mb-2">
<label for="field-{{ $question->code }}"
class="form-label">{{ $question->question }} {!! $question->required == 'yes' ? '<span class="text-danger">*</span>' : '' !!}
</label>
@switch($question->type)
@case('hidden')
<input type="{{ $question->type }}" id="field-{{ $question->code }}"
name="{{ $question->code }}[0]"
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 }}[0]" 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 }}[0]" 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 }}[0]" class="form-control"
{{ $question->required == 'yes' ? 'required' : '' }}>{{ isset($question->value) ? $question->value : '' }}</textarea>
@break
@case('dropdown')
<select class="form-control" id="field-{{ $question->code }}"
name="{{ $question->code }}[0]"
{{ $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' : '' }}
{{ $question->code == 'contact_id' ? 'msl_code= ' . $option->contact->msl_code . ' ' : '' }}
{{ $question->code == 'contact_id' ? 'speciality= ' . (isset($option->contact->speciality) ? $option->contact->speciality : '') . ' ' : '' }}>
{{ $option->display }}
</option>
@endforeach
@if($question->code == 'contact_id')
<option value="Other" {{ isset($question->value) && $question->value == $option->value ? 'selected' : '' }}>
Other
</option>
@endif
</select>
@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 }}[0][]" 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')
@if (!isset($form->is_approval) || (isset($form->is_approval) && $form->is_approval != 'yes'))
<input type="file" id="field-{{ $question->code }}"
name="{{ $question->code }}" class="form-control" value=""
{{ $question->required == 'yes' ? 'required' : '' }}>
@elseif(isset($question->value))
<img src="{{ $question->value }}" class="img-fluid">
@endif
@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
</div>
<div id="doctor-group-wrapper">
</div>
<div id="error-Message">
</div>
@if (!isset($form->is_approval) || (isset($form->is_approval) && $form->is_approval != 'yes'))
<p class="text-center text-danger p-1" >
{{ $subcount >= 5 ? "You cannot add more doctors, maximum submissions already reached." : ''}}
</p>
<div class="text-center button-list">
@if (!$id)
<button class="btn btn-primary" type="button" id="adddoctor-btn" {{$subcount >= 5 ? "disabled" : ''}}> Add
Doctor </button>
@endif
@if (!$id)
<button class="btn btn-primary" type="submit" id="submit-btn" {{$subcount >= 5 ? "disabled" : ''}}> Submit
</button>
@else
<button class="btn btn-primary" type="submit" id="submit-btn"> Submit
</button>
@endif
</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%;
}
.doctor-wrapper {
border: 1px solid #ccc;
padding: 20px;
border-radius: 10px;
margin-bottom: 20px;
}
</style>
@include('layouts.shared/footer-script')
<script src="{{ asset('assets/libs/sweetalert2/sweetalert2.min.js') }}"></script>
<script type="text/javascript">
$(document).ready(function() {
// ===========================
// Other Doctor Name handling
// ===========================
function toggleOtherDoctor($select) {
let $wrapper = $select.closest('.doctor-wrapper');
let index = $select.attr('id')?.split('-').pop(); // extract index if exists
// Support wrapper 0 (id = field-contact_id)
let $otherInput;
if ($select.attr('id') === 'field-contact_id') {
$otherInput = $('#field-other_doctor_name'); // wrapper 0
} else {
$otherInput = $(`#field-other_doctor_name-${index}`);
}
let $otherDiv = $otherInput.closest('.col-md-12.mb-2');
let selectedText = $select.find("option:selected").text().trim().toLowerCase();
if (selectedText === "other") {
$otherDiv.show();
$otherInput.prop('required', true);
} else {
$otherDiv.hide();
$otherInput.prop('required', false).val('');
}
}
function initOtherDoctor($wrapper) {
let $otherInput = $wrapper.find('input[id^="field-other_doctor_name"]');
let $otherDiv = $otherInput.closest('.col-md-12.mb-2');
$otherDiv.hide();
$otherInput.prop('required', false);
let $select = $wrapper.find('select[name^="contact_id"]');
if ($select.length) toggleOtherDoctor($select);
}
// Init all existing wrappers on page load
$('.doctor-wrapper').each(function() {
initOtherDoctor($(this));
});
// Attach change listener (covers wrapper 0 + dynamic ones)
$(document).on('change', 'select[name^="contact_id"]', function() {
toggleOtherDoctor($(this));
});
// ===========================
// Record date max = today
// ===========================
let today = new Date().toISOString().split('T')[0];
$('#field-record_date').attr('max', today);
// ===========================
// Initial msl_code update for wrapper 0
// ===========================
$('#field-contact_id').on('change', function() {
$('#field-msl_code').val($(this).find(":selected").attr("msl_code"));
});
// ===========================
// Hide already selected doctors except "Other"
// ===========================
function refreshContactDropdowns() {
// 1. Collect currently selected doctors in the form
let selected = [];
$('select[name^="contact_id"]').each(function() {
if ($(this).val()) selected.push($(this).val());
});
// 2. Add previously submitted doctors from hidden input
let prev = $('#field-submissiondoctors').val(); // e.g., "Vaishnav,Doc 101"
if (prev) {
prev.split(',').forEach(d => {
if (d.trim() && !selected.includes(d.trim())) {
selected.push(d.trim());
}
});
}
// 3. Hide options already selected (except 'Other') in each dropdown
$('select[name^="contact_id"]').each(function() {
let currentVal = $(this).val();
$(this).find("option").each(function() {
let optVal = $(this).val();
if (optVal.toLowerCase() === "other") {
$(this).show(); // always show "Other"
} else if (optVal === currentVal) {
$(this).show(); // keep the one currently selected visible
} else if (selected.includes(optVal)) {
$(this).hide(); // hide already selected / previous doctors
} else {
$(this).show(); // show remaining
}
});
});
}
// 🔑 CALL IT ON PAGE LOAD
$(document).ready(function () {
refreshContactDropdowns();
});
// ===========================
// Get next available index
// ===========================
function getNextIndex() {
let indices = [];
$('#doctor-group-wrapper .doctor-wrapper').each(function() {
let select = $(this).find('select[name^="contact_id"]');
if (!select.length) return;
let id = select.attr('id');
let idxMatch = id.match(/\d+$/);
if (idxMatch) {
let idx = parseInt(idxMatch[0]);
if (idx > 0) indices.push(idx); // only dynamic wrappers
}
});
for (let i = 1; i <= 4; i++) {
if (!indices.includes(i)) return i;
}
return null;
}
// ===========================
// Add doctor wrapper
// ===========================
// ===========================
// Add doctor wrapper (with subcount check)
// ===========================
function updateAddDoctorButton() {
let subcount = parseInt($('#field-submissioncount').val() || 0);
let staticWrapperCount = $('.doctor-wrapper').first().length ? 1 : 0;
let dynamicWrappers = $('#doctor-group-wrapper .doctor-wrapper').length;
let totalUsed = subcount + staticWrapperCount + dynamicWrappers;
let maxAllowed = 5;
console.log("Submissions in DB:", subcount);
console.log("Static wrapper count:", staticWrapperCount);
console.log("Dynamic wrappers count:", dynamicWrappers);
console.log("Total used:", totalUsed, "/", maxAllowed);
if (totalUsed >= maxAllowed) {
$('#error-Message').html(
'<p class="text-center text-danger">You cannot add more doctors, maximum submissions already reached.</p>'
);
$('#adddoctor-btn').prop('disabled', true);
} else {
$('#error-Message').html('');
$('#adddoctor-btn').prop('disabled', false);
}
}
// Call it on add
$('#adddoctor-btn').click(function () {
updateAddDoctorButton();
let subcount = parseInt($('#field-submissioncount').val() || 0);
let dynamicWrappers = $('#doctor-group-wrapper .doctor-wrapper').length;
let maxAllowed = 5;
if (subcount + 1 + dynamicWrappers >= maxAllowed) return; // stop if max reached
let doctorName = $('#field-contact_id').html();
let newIndex = getNextIndex();
if (newIndex === null) return;
let empty_doctor = `
<div class="doctor-wrapper">
<button type="button" class="btn btn-danger btn-remove float-end" style="margin-top:-25px; margin-right:-25px">
<i class="mdi mdi-close"></i>
</button>
@foreach ($questions as $question)
@if (in_array($question->code, ['record_date', 'select_month'])) @continue; @endif
<div class="col-md-12 mb-2">
<label for="field-{{ $question->code }}-${newIndex}" class="form-label">
{{ $question->question }} {!! $question->required == 'yes' ? '<span class="text-danger">*</span>' : '' !!}
</label>
@switch($question->type)
@case('hidden')
<input type="{{ $question->type }}" id="field-{{ $question->code }}-${newIndex}"
name="{{ $question->code }}[${newIndex}]" value="{{ $question->value ?? '' }}">
@break
@case('text') @case('file') @case('email') @case('tel') @case('date')
<input type="{{ $question->type }}" id="field-{{ $question->code }}-${newIndex}"
name="{{ $question->code }}[${newIndex}]" class="form-control"
value="{{ $question->value ?? '' }}" {{ $question->required == 'yes' ? 'required' : '' }}>
@break
@case('number')
<input type="number" min="0" max="9999999999" step="1"
id="field-{{ $question->code }}-${newIndex}" name="{{ $question->code }}[${newIndex}]"
class="form-control" value="{{ $question->value ?? 0 }}" {{ $question->required == 'yes' ? 'required' : '' }}>
@break
@case('dropdown')
<select class="form-control" id="field-{{ $question->code }}-${newIndex}"
name="{{ $question->code }}[${newIndex}]" {{ $question->required == 'yes' ? 'required' : '' }}>
@if ($question->code == 'contact_id') ${doctorName} @else
<option value="">-- Select --</option>
@foreach ($question->options as $option)
<option value="{{ $option->value }}"
{{ isset($question->value) && $question->value == $option->value ? 'selected' : '' }}
{{ $question->code == 'contact_id' ? 'msl_code=' . $option->contact->msl_code : '' }}>
{{ $option->display }}
</option>
@endforeach
@endif
</select>
@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 }}-${newIndex}-{{ $option->value }}"
name="{{ $question->code }}[${newIndex}][]" value="{{ $option->value }}"
{{ isset($question->value) && $question->value != '' && in_array($option->value, $question->value) ? 'checked' : '' }}>
<label class="form-check-label"
for="field-{{ $question->code }}-${newIndex}-{{ $option->value }}">
{{ $option->display }}
</label>
</div>
@endforeach
@break
@endswitch
</div>
@endforeach
<input type="hidden" id="field-msl_code-${newIndex}" name="msl_code[${newIndex}]" disabled>
</div>`;
$('#doctor-group-wrapper').append(empty_doctor);
let $newWrapper = $('#doctor-group-wrapper .doctor-wrapper').last();
initOtherDoctor($newWrapper);
refreshContactDropdowns();
updateAddDoctorButton(); // recalc after append
});
// Call it on remove
$(document).on('click', '.btn-remove', function() {
$(this).closest('.doctor-wrapper').remove();
// Reindex dynamic wrappers starting from 1 (skip wrapper 0)
let dynamicIdx = 1;
$('#doctor-group-wrapper .doctor-wrapper').each(function() {
let select = $(this).find('select[name^="contact_id"]');
if (!select.length) return; // skip wrapper 0
$(this).find('[id]').each(function() {
let oldId = $(this).attr('id');
if (oldId) $(this).attr('id', oldId.replace(/\d+$/, dynamicIdx));
});
$(this).find('[name]').each(function() {
let oldName = $(this).attr('name');
if (!oldName) return;
let newName = oldName.replace(/\[\d+\]/, `[${dynamicIdx}]`);
$(this).attr('name', newName);
});
dynamicIdx++;
});
refreshContactDropdowns();
updateAddDoctorButton(); // recalc after remove
});
// ===========================
// Update msl_code when contact is selected
// ===========================
$(document).on('change', 'select[name^="contact_id"]', function() {
let wrapper = $(this).closest('.doctor-wrapper');
let selectedOption = $(this).find(":selected");
// Only overwrite msl_code if it's a contact with msl_code attribute
let mslCode = selectedOption.attr("msl_code");
let $mslInput = wrapper.find('input[id^="field-msl_code"]');
if (selectedOption.text().trim().toLowerCase() !== "other") {
$mslInput.val(mslCode || '');
$mslInput.prop('readonly', true); // readonly for contact
} else {
$mslInput.prop('readonly', false); // editable for Other
}
refreshContactDropdowns();
});
// ===========================
// Fetch doctor list when user changes
// ===========================
$('#field-user_id').change(function(event) {
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('').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).attr("msl_code", itm.contact.msl_code)
);
}
},
});
});
// ===========================
// Form submission
// ===========================
$('#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'
});
$('#report-form').trigger("reset");
} else {
Swal.fire({
icon: 'error',
title: 'Data not submitted',
text: 'Something went wrong, please try again later!'
});
}
},
});
});
});
</script>
</body>
</html>