934 lines
47 KiB
PHP
934 lines
47 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" />
|
|
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
|
|
<style>
|
|
|
|
/* =========================
|
|
SELECT2 CUSTOM HIGHLIGHT
|
|
========================= */
|
|
|
|
.select2-container .select2-selection--single{
|
|
height: 38px !important;
|
|
border: 1px solid #ced4da !important;
|
|
border-radius: 6px !important;
|
|
padding-top: 4px !important;
|
|
}
|
|
|
|
.select2-container--default .select2-selection--single .select2-selection__rendered{
|
|
line-height: 28px !important;
|
|
}
|
|
|
|
.select2-container--default .select2-selection--single .select2-selection__arrow{
|
|
height: 36px !important;
|
|
}
|
|
|
|
/* Dropdown option hover */
|
|
.select2-results__option--highlighted{
|
|
background-color: #0d6efd !important;
|
|
color: white !important;
|
|
}
|
|
|
|
/* Selected option */
|
|
.select2-results__option[aria-selected=true]{
|
|
background-color: #dbe9ff !important;
|
|
color: black !important;
|
|
font-weight: 600;
|
|
}
|
|
|
|
/* Search box */
|
|
.select2-search__field{
|
|
border-radius: 5px !important;
|
|
padding: 6px !important;
|
|
}
|
|
|
|
</style>
|
|
</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
|
|
$submissions = Formhelper::getMultiformData( $form->partition, $form_id, $form->user_id);
|
|
$submissions = $submissions->data->submissions;
|
|
|
|
|
|
@endphp
|
|
@if(count($submissions)===0 || $id)
|
|
<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_rxber_type"] ) )
|
|
@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->legacy_code . ' ' : '' }} {{ $question->code == 'contact_id' ? 'pcode= ' . $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_rxber_type"] ) )
|
|
@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')
|
|
@if ($question->code == 'contact_id')
|
|
<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' : '' }}
|
|
@if($question->code == 'contact_id')
|
|
msl_code="{{ (isset($option->contact->legacy_code) ? $option->contact->legacy_code : '') }}"
|
|
pcode="{{ (isset($option->contact->msl_code) ? $option->contact->msl_code : '') }}"
|
|
speciality="{{ (isset($option->contact->speciality) ? $option->contact->speciality : '') }}"
|
|
@endif
|
|
>
|
|
{{ $option->display }}
|
|
</option>
|
|
|
|
@endforeach
|
|
</select>
|
|
@else
|
|
<select class="form-control" id="execution-{{ $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' : '' }}>
|
|
{{ $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}}[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>
|
|
@if(!isset($form->is_approval) || (isset($form->is_approval) && $form->is_approval !=
|
|
'yes' ) )
|
|
<div class="text-center button-list">
|
|
@if(!$id)
|
|
<button class="btn btn-primary" type="button" id="adddoctor-btn"> Add Doctor </button>
|
|
@endif
|
|
<button class="btn btn-primary" type="submit" id="submit-btn"> Submit </button>
|
|
</div>
|
|
@endif
|
|
</form>
|
|
@elseif(!$id)
|
|
<h4>You have submitted 10 doctors</h4>
|
|
@endif
|
|
</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="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
|
|
<script src="{{asset('assets/libs/sweetalert2/sweetalert2.min.js')}}"></script>
|
|
|
|
|
|
{{-- <script>
|
|
jQuery(document).ready(function($) {
|
|
|
|
// Check whether the form is loaded in Edit Mode
|
|
var isEditMode = @json((bool)$id);
|
|
|
|
// Auto-fill metadata fields (MSL Code, Pcode, Speciality) dynamically
|
|
$(document).on('change', 'select[id^="field-contact_id"]', function() {
|
|
let selectedOption = $(this).find(':selected');
|
|
let idParts = $(this).attr('id').split('-');
|
|
let suffix = idParts.length > 2 ? '-' + idParts[2] : '';
|
|
|
|
$('#field-msl_code' + suffix).val(selectedOption.attr('msl_code') || '');
|
|
$('#field-pcode' + suffix).val(selectedOption.attr('pcode') || '');
|
|
$('#field-speciality' + suffix).val(selectedOption.attr('speciality') || '');
|
|
|
|
// Refresh disabled options across all doctor dropdowns
|
|
preventDuplicateDoctors();
|
|
});
|
|
|
|
// Restrict brand name inputs to letters and spaces only
|
|
$(document).on('input', 'input[id^="field-brand_name"]', function () {
|
|
this.value = this.value.replace(/[^a-zA-Z\s]/g, '');
|
|
});
|
|
|
|
// Initialize Select2 elements helper
|
|
function initSelect2(selector, placeholderText) {
|
|
$(selector).each(function() {
|
|
if ($(this).hasClass("select2-hidden-accessible")) {
|
|
$(this).select2('destroy');
|
|
}
|
|
$(this).select2({
|
|
width: '100%',
|
|
placeholder: placeholderText || '-- Select --'
|
|
});
|
|
});
|
|
}
|
|
|
|
// Core logic to disable already selected doctors in other dropdowns
|
|
function preventDuplicateDoctors() {
|
|
let selectedDoctorIds = [];
|
|
|
|
// Step 1: Collect all currently selected doctor values
|
|
$('select[id^="field-contact_id"]').each(function () {
|
|
let val = $(this).val();
|
|
if (val && val !== '') {
|
|
selectedDoctorIds.push(String(val));
|
|
}
|
|
});
|
|
|
|
// Step 2: Traverse each doctor select element and update option states
|
|
$('select[id^="field-contact_id"]').each(function () {
|
|
let $currentSelect = $(this);
|
|
let currentVal = String($currentSelect.val() || '');
|
|
|
|
$currentSelect.find('option').each(function () {
|
|
let $option = $(this);
|
|
let optionVal = String($option.val() || '');
|
|
|
|
// Skip the default placeholder option
|
|
if (!optionVal) return;
|
|
|
|
// Clean any previously appended '(Already Added)' tags
|
|
let rawText = $option.text().replace(/\s*\(Already Added\)/g, '').trim();
|
|
|
|
// If selected in another dropdown, disable and tag it
|
|
if (selectedDoctorIds.includes(optionVal) && optionVal !== currentVal) {
|
|
$option.prop('disabled', true);
|
|
$option.text(rawText + ' (Already Added)');
|
|
} else {
|
|
$option.prop('disabled', false);
|
|
$option.text(rawText);
|
|
}
|
|
});
|
|
|
|
// Step 3: Refresh Select2 cleanly to render updated disabled states
|
|
if ($currentSelect.hasClass("select2-hidden-accessible")) {
|
|
$currentSelect.select2('destroy');
|
|
}
|
|
$currentSelect.select2({
|
|
width: '100%',
|
|
placeholder: '-- Select Doctor --'
|
|
});
|
|
|
|
// Preserve the selected value
|
|
$currentSelect.val(currentVal);
|
|
});
|
|
}
|
|
|
|
// Initial setup on page load
|
|
initSelect2('select[id^="field-contact_id"]', '-- Select Doctor --');
|
|
initSelect2('#field-user_id', '-- Select User / Territory --');
|
|
initSelect2('select[id^="execution-"]');
|
|
$('#field-msl_code,#field-pcode,#field-speciality').attr('readonly', true);
|
|
preventDuplicateDoctors();
|
|
|
|
// Calculate doctor count based on existing elements
|
|
var doctor_count = $('.doctor-wrapper').length ? ($('.doctor-wrapper').length - 1) : 0;
|
|
|
|
// Add doctor row dynamically
|
|
$('#adddoctor-btn').click(function() {
|
|
if ($('.doctor-wrapper').length >= 10) {
|
|
alert('You can only add up to 10 doctors.');
|
|
return;
|
|
}
|
|
|
|
doctor_count++;
|
|
|
|
// Clean clone of doctor options from the primary select element
|
|
let $temp = $('#field-contact_id').clone();
|
|
$temp.find('option').each(function() {
|
|
$(this).prop('disabled', false);
|
|
$(this).text($(this).text().replace(/\s*\(Already Added\)/g, '').trim());
|
|
});
|
|
let doctorName = $temp.html();
|
|
|
|
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_rxber_type"] ) )
|
|
@continue;
|
|
@endif
|
|
|
|
<div class="col-md-12 mb-2">
|
|
<label for="field-{{$question->code}}-${doctor_count}" 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}}-${doctor_count}" name="{{$question->code}}[${doctor_count}]" value="{{isset($question->value)?$question->value:''}}">
|
|
@break
|
|
@case('text')
|
|
@case('file')
|
|
@case('email')
|
|
@case('tel')
|
|
@case('date')
|
|
<input type="{{$question->type}}" id="field-{{$question->code}}-${doctor_count}" name="{{$question->code}}[${doctor_count}]" 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}}-${doctor_count}" name="{{$question->code}}[${doctor_count}]" class="form-control" {{$question->required == 'yes'?'required':'' }} value="{{isset($question->value)?$question->value:0}}">
|
|
@break
|
|
@case('dropdown')
|
|
<select class="form-control" id="field-{{$question->code}}-${doctor_count}" name="{{$question->code}}[${doctor_count}]" {{$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' : '' }}>
|
|
{{ $option->display }}
|
|
</option>
|
|
@endforeach
|
|
@endif
|
|
</select>
|
|
@break
|
|
@case('checkbox')
|
|
<div class="checkbox-group">
|
|
@if(isset($question->options) && count($question->options) > 0)
|
|
@foreach($question->options as $option)
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="field-{{$question->code}}-${doctor_count}-{{ $loop->index }}" name="{{$question->code}}[${doctor_count}][]" value="{{ $option->value }}">
|
|
<label class="form-check-label" for="field-{{$question->code}}-${doctor_count}-{{ $loop->index }}">
|
|
{{ $option->display }}
|
|
</label>
|
|
</div>
|
|
@endforeach
|
|
@endif
|
|
</div>
|
|
@break
|
|
@endswitch
|
|
</div>
|
|
@endforeach
|
|
</div>`;
|
|
|
|
let $newRow = $(empty_doctor);
|
|
$('#doctor-group-wrapper').append($newRow);
|
|
|
|
// Initialize Select2 on the new elements
|
|
initSelect2('#field-contact_id-' + doctor_count, '-- Select Doctor --');
|
|
initSelect2($newRow.find('select[id^="execution-"]'));
|
|
|
|
// Set readonly attributes for metadata inputs in the new card
|
|
$('#field-msl_code-' + doctor_count).attr('readonly', true);
|
|
$('#field-pcode-' + doctor_count).attr('readonly', true);
|
|
$('#field-speciality-' + doctor_count).attr('readonly', true);
|
|
|
|
// Update duplicate validation state
|
|
preventDuplicateDoctors();
|
|
|
|
// Toggle Add Button visibility
|
|
if ($('.doctor-wrapper').length >= 10) {
|
|
$('#adddoctor-btn').hide();
|
|
}
|
|
});
|
|
|
|
// Remove a doctor row
|
|
$(document).on('click', '.btn-remove', function() {
|
|
let $row = $(this).closest('.doctor-wrapper');
|
|
$row.find('select').each(function() {
|
|
if ($(this).hasClass("select2-hidden-accessible")) {
|
|
$(this).select2('destroy');
|
|
}
|
|
});
|
|
$row.remove();
|
|
|
|
// Re-enable options freed by removing this doctor
|
|
preventDuplicateDoctors();
|
|
|
|
if ($('.doctor-wrapper').length < 10) {
|
|
$('#adddoctor-btn').show();
|
|
}
|
|
});
|
|
|
|
// Territory / User dropdown dynamic change
|
|
$('#field-user_id').change(function(event){
|
|
let hierarchy = $('option:selected', this).attr('hierarchy');
|
|
$('#field-hierarchy_id').val(hierarchy);
|
|
$('#pageloader').fadeIn();
|
|
|
|
$.ajax({
|
|
type: "GET",
|
|
url: '/form/getdoctorlist?partition={{$form->partition}}&hierarchy=' + hierarchy,
|
|
processData: false,
|
|
contentType: false,
|
|
success: function(data) {
|
|
$("#pageloader").fadeOut();
|
|
let $contactSelect = $('#field-contact_id');
|
|
$contactSelect.empty().append($("<option></option>").attr("value", '').text('-- Select --'));
|
|
|
|
for (let itm of data) {
|
|
$contactSelect.append(
|
|
$("<option></option>")
|
|
.attr("value", itm.value)
|
|
.text(itm.display)
|
|
.attr("msl_code", itm.contact ? itm.contact.msl_code : '')
|
|
.attr("pcode", itm.contact ? itm.contact.pcode : '')
|
|
.attr("speciality", itm.contact ? itm.contact.speciality : '')
|
|
);
|
|
}
|
|
|
|
$contactSelect.val('').trigger('change.select2');
|
|
preventDuplicateDoctors();
|
|
}
|
|
});
|
|
});
|
|
|
|
// Form submit handler
|
|
$('#report-form').on('submit', function(event) {
|
|
event.preventDefault();
|
|
|
|
let currentTotal = $('.doctor-wrapper').length;
|
|
|
|
// Only enforce 10 submissions in Create Mode
|
|
if (!isEditMode && currentTotal !== 10) {
|
|
alert(`You must submit 10 Doctors for Levoswitch, Current Doctors: ${currentTotal}`);
|
|
return;
|
|
}
|
|
|
|
$("#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();
|
|
});
|
|
$('#report-form').trigger("reset");
|
|
} else {
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Data not submitted',
|
|
text: data.message || 'Something went wrong, please try again after some time!'
|
|
});
|
|
}
|
|
},
|
|
error: function() {
|
|
$("#pageloader").fadeOut();
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Server Error',
|
|
text: 'Something went wrong, please try again after some time!'
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
});
|
|
</script> --}}
|
|
|
|
<script>
|
|
jQuery(document).ready(function($) {
|
|
|
|
var isEditMode = @json((bool)$id);
|
|
|
|
// Auto-fill metadata fields
|
|
$(document).on('change', 'select[id^="field-contact_id"]', function() {
|
|
let selectedOption = $(this).find(':selected');
|
|
let idParts = $(this).attr('id').split('-');
|
|
let suffix = idParts.length > 2 ? '-' + idParts[2] : '';
|
|
|
|
$('#field-msl_code' + suffix).val(selectedOption.attr('msl_code') || '');
|
|
$('#field-pcode' + suffix).val(selectedOption.attr('pcode') || '');
|
|
$('#field-speciality' + suffix).val(selectedOption.attr('speciality') || '');
|
|
|
|
preventDuplicateDoctors();
|
|
});
|
|
|
|
// Restrict brand name
|
|
$(document).on('input', 'input[id^="field-brand_name"]', function () {
|
|
this.value = this.value.replace(/[^a-zA-Z\s]/g, '');
|
|
});
|
|
|
|
// Init Select2
|
|
function initSelect2(selector, placeholderText) {
|
|
$(selector).each(function() {
|
|
if ($(this).hasClass("select2-hidden-accessible")) {
|
|
$(this).select2('destroy');
|
|
}
|
|
$(this).select2({
|
|
width: '100%',
|
|
placeholder: placeholderText || '-- Select --'
|
|
});
|
|
});
|
|
}
|
|
|
|
// Duplicate doctor restriction
|
|
function preventDuplicateDoctors() {
|
|
let selectedDoctorIds = [];
|
|
|
|
$('select[id^="field-contact_id"]').each(function () {
|
|
let val = $(this).val();
|
|
if (val && val !== '') {
|
|
selectedDoctorIds.push(String(val));
|
|
}
|
|
});
|
|
|
|
$('select[id^="field-contact_id"]').each(function () {
|
|
let $currentSelect = $(this);
|
|
let currentVal = String($currentSelect.val() || '');
|
|
|
|
$currentSelect.find('option').each(function () {
|
|
let $option = $(this);
|
|
let optionVal = String($option.val() || '');
|
|
|
|
if (!optionVal) return;
|
|
|
|
let rawText = $option.text().replace(/\s*\(Already Added\)/g, '').trim();
|
|
|
|
if (selectedDoctorIds.includes(optionVal) && optionVal !== currentVal) {
|
|
$option.prop('disabled', true);
|
|
$option.text(rawText + ' (Already Added)');
|
|
} else {
|
|
$option.prop('disabled', false);
|
|
$option.text(rawText);
|
|
}
|
|
});
|
|
|
|
if ($currentSelect.hasClass("select2-hidden-accessible")) {
|
|
$currentSelect.select2('destroy');
|
|
}
|
|
$currentSelect.select2({
|
|
width: '100%',
|
|
placeholder: '-- Select Doctor --'
|
|
});
|
|
|
|
$currentSelect.val(currentVal);
|
|
});
|
|
}
|
|
|
|
// Initialize elements
|
|
initSelect2('select[id^="field-contact_id"]', '-- Select Doctor --');
|
|
initSelect2('#field-user_id', '-- Select User / Territory --');
|
|
initSelect2('select[id^="execution-"]');
|
|
$('#field-msl_code,#field-pcode,#field-speciality').attr('readonly', true);
|
|
|
|
// Populate metadata on initial edit load
|
|
$('select[id^="field-contact_id"]').trigger('change');
|
|
|
|
var doctor_count = $('.doctor-wrapper').length || 0;
|
|
|
|
// Add doctor row dynamically
|
|
$('#adddoctor-btn').click(function() {
|
|
if ($('.doctor-wrapper').length >= 10) {
|
|
alert('You can only add up to 10 doctors.');
|
|
return;
|
|
}
|
|
|
|
doctor_count++;
|
|
|
|
let $temp = $('#field-contact_id').clone();
|
|
$temp.find('option').each(function() {
|
|
$(this).prop('disabled', false);
|
|
$(this).text($(this).text().replace(/\s*\(Already Added\)/g, '').trim());
|
|
});
|
|
let doctorName = $temp.html();
|
|
|
|
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_rxber_type"] ) )
|
|
@continue;
|
|
@endif
|
|
|
|
<div class="col-md-12 mb-2">
|
|
<label for="field-{{$question->code}}-${doctor_count}" 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}}-${doctor_count}" name="{{$question->code}}[${doctor_count}]" value="{{isset($question->value)?$question->value:''}}">
|
|
@break
|
|
@case('text')
|
|
@case('file')
|
|
@case('email')
|
|
@case('tel')
|
|
@case('date')
|
|
<input type="{{$question->type}}" id="field-{{$question->code}}-${doctor_count}" name="{{$question->code}}[${doctor_count}]" 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}}-${doctor_count}" name="{{$question->code}}[${doctor_count}]" class="form-control" {{$question->required == 'yes'?'required':'' }} value="{{isset($question->value)?$question->value:0}}">
|
|
@break
|
|
@case('dropdown')
|
|
<select class="form-control" id="field-{{$question->code}}-${doctor_count}" name="{{$question->code}}[${doctor_count}]" {{$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' : '' }}>
|
|
{{ $option->display }}
|
|
</option>
|
|
@endforeach
|
|
@endif
|
|
</select>
|
|
@break
|
|
@case('checkbox')
|
|
<div class="checkbox-group">
|
|
@if(isset($question->options) && count($question->options) > 0)
|
|
@foreach($question->options as $option)
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" id="field-{{$question->code}}-${doctor_count}-{{ $loop->index }}" name="{{$question->code}}[${doctor_count}][]" value="{{ $option->value }}">
|
|
<label class="form-check-label" for="field-{{$question->code}}-${doctor_count}-{{ $loop->index }}">
|
|
{{ $option->display }}
|
|
</label>
|
|
</div>
|
|
@endforeach
|
|
@endif
|
|
</div>
|
|
@break
|
|
@endswitch
|
|
</div>
|
|
@endforeach
|
|
</div>`;
|
|
|
|
let $newRow = $(empty_doctor);
|
|
$('#doctor-group-wrapper').append($newRow);
|
|
|
|
initSelect2('#field-contact_id-' + doctor_count, '-- Select Doctor --');
|
|
initSelect2($newRow.find('select[id^="execution-"]'));
|
|
|
|
$('#field-msl_code-' + doctor_count).attr('readonly', true);
|
|
$('#field-pcode-' + doctor_count).attr('readonly', true);
|
|
$('#field-speciality-' + doctor_count).attr('readonly', true);
|
|
|
|
preventDuplicateDoctors();
|
|
|
|
if ($('.doctor-wrapper').length >= 10) {
|
|
$('#adddoctor-btn').hide();
|
|
}
|
|
});
|
|
|
|
// Remove row
|
|
$(document).on('click', '.btn-remove', function() {
|
|
let $row = $(this).closest('.doctor-wrapper');
|
|
$row.find('select').each(function() {
|
|
if ($(this).hasClass("select2-hidden-accessible")) {
|
|
$(this).select2('destroy');
|
|
}
|
|
});
|
|
$row.remove();
|
|
preventDuplicateDoctors();
|
|
|
|
if ($('.doctor-wrapper').length < 10) {
|
|
$('#adddoctor-btn').show();
|
|
}
|
|
});
|
|
|
|
// User/Territory Change
|
|
$('#field-user_id').change(function(event){
|
|
let hierarchy = $('option:selected', this).attr('hierarchy');
|
|
$('#field-hierarchy_id').val(hierarchy);
|
|
$('#pageloader').fadeIn();
|
|
|
|
$.ajax({
|
|
type: "GET",
|
|
url: '/form/getdoctorlist?partition={{$form->partition}}&hierarchy=' + hierarchy,
|
|
processData: false,
|
|
contentType: false,
|
|
success: function(data) {
|
|
$("#pageloader").fadeOut();
|
|
let $contactSelect = $('#field-contact_id');
|
|
$contactSelect.empty().append($("<option></option>").attr("value", '').text('-- Select --'));
|
|
|
|
for (let itm of data) {
|
|
$contactSelect.append(
|
|
$("<option></option>")
|
|
.attr("value", itm.value)
|
|
.text(itm.display)
|
|
.attr("msl_code", itm.contact ? itm.contact.msl_code : '')
|
|
.attr("pcode", itm.contact ? itm.contact.pcode : '')
|
|
.attr("speciality", itm.contact ? itm.contact.speciality : '')
|
|
);
|
|
}
|
|
|
|
$contactSelect.val('').trigger('change.select2');
|
|
preventDuplicateDoctors();
|
|
}
|
|
});
|
|
});
|
|
|
|
// Form Submit
|
|
$('#report-form').on('submit', function(event) {
|
|
event.preventDefault();
|
|
|
|
let currentTotal = $('.doctor-wrapper').length;
|
|
|
|
if (!isEditMode && currentTotal !== 10) {
|
|
alert(`You must submit 10 Doctors for Levoswitch, Current Doctors: ${currentTotal}`);
|
|
return;
|
|
}
|
|
|
|
// CRITICAL FIX: Re-enable disabled options so values are not skipped in FormData
|
|
$('select option:disabled').prop('disabled', false);
|
|
|
|
$("#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();
|
|
});
|
|
$('#report-form').trigger("reset");
|
|
} else {
|
|
preventDuplicateDoctors(); // Re-apply restrictions on failure
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Data not submitted',
|
|
text: data.message || 'Something went wrong, please try again after some time!'
|
|
});
|
|
}
|
|
},
|
|
error: function(xhr) {
|
|
$("#pageloader").fadeOut();
|
|
preventDuplicateDoctors(); // Re-apply restrictions on failure
|
|
|
|
let errorMsg = 'Something went wrong, please try again after some time!';
|
|
if (xhr.responseJSON && xhr.responseJSON.message) {
|
|
errorMsg = xhr.responseJSON.message;
|
|
}
|
|
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Server Error',
|
|
text: errorMsg
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
});
|
|
</script>
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|