nhance/app/Views/leads_form_handler.php

1173 lines
44 KiB
PHP

<style>
.table th,
.table td {
padding: 8px;
}
table.dataTable tbody td {
padding: 4px 4px !important;
}
.col-12 {
max-width: 98% !important;
}
.dataTables_filter {
position: absolute;
}
.highlight {
border: 2px solid red;
background-color: #ffe6e6;
}
.column-header {
margin-right: 10px;
}
.form-section {
border: 1px solid #ccc;
padding: 15px;
margin-bottom: 20px;
}
.row-box {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
}
.custom-dropdown-menu {
display: none;
position: absolute;
background-color: #ffffff !important;
border: 1px solid rgba(0, 0, 0, 0.15);
border-radius: 0.25rem;
padding: 0.5rem 0;
min-width: 10rem;
z-index: 9999;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.custom-dropdown-menu .dropdown-item {
display: block !important;
width: 100% !important;
padding: 0.5rem 1rem !important;
color: #212529 !important;
text-decoration: none !important;
background-color: transparent !important;
}
.custom-dropdown-menu .dropdown-item:hover {
background-color: #f8f9fa !important;
color: #16181b !important;
cursor: pointer !important;
}
.readonly-select {
pointer-events: none;
/* background-color: #f0f0f0; */
background-color: #e0e0e0;
color: #666;
}
.switch {
position: relative;
display: inline-block;
width: 54px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 19px;
width: 19px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
</style>
<?php
if (isset($selected_lead_type)) {
if ($selected_lead_type == 1) {
include('leads_form.php');
} else {
include('leads_non_eb.php');
}
}
?>
<script>
var client_list = ''; // local variable for storing the client branch list
var branch_list = ''; // local variable for storing the client branch list
var policy_list = ''; // local variable for storing the client policy list
var temp_client_id = 0;
var temp_branch_id = 0;
var selected_lead_form_type = <?= isset($selected_lead_type) ? $selected_lead_type : 0 ?>;
var fileIndex = 1; // Initialize index
// select2 document ready
$(document).ready(function() {
getClientAndBranchAndPolicy()
$('#client_id').select2();
$('#client_branch_id').select2();
$('#salse_person_id').select2({
placeholder: "Select Sales Person",
});
$('#policy_type_id').select2();
$("textarea.select2-search__field").attr('rows', '1');
$("textarea.select2-search__field").css('resize', 'none');
})
//--------------------------------------------------------------------------------------------------------
function hide_list_show_add() {
$('#leads_list').hide()
$('#lead_filter_div').hide()
$('#leads_form').show()
}
function show_list_hide_add() {
$('#leads_list').show()
$('#lead_filter_div').show()
$('#leads_form').hide()
}
function getClientAndBranchAndPolicy() {
console.log('getClientAndBranchAndPolicy function called')
$.ajax({
url: '<?= base_url("/util/getClientAndBranchAndPolicy") ?>',
type: "GET",
dataType: 'json',
success: function(res) {
console.log('getClientAndBranchAndPolicy', res);
if (res.status == true) {
client_list = res.client_data;
branch_list = res.branch_data;
policy_list = res.policy_data;
appendClients(res.client_data);
} else {
console.log('No data found');
}
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
}
});
}
function appendClients(data) {
// console.log("lead_type",lead_type.value);
$('#client_id').empty();
if (lead_type.value == 3) {
if ($('#client_id option[value="add_client"]').length === 0) {
$('#client_id').prepend($('<option>', {
value: 'add_client',
text: '+ Add Client'
}));
}
}
$('#client_id').append($('<option>', {
value: '',
text: 'Select Client'
}));
$.each(data, function(index, item) {
var option = $('<option>', {
value: item.id,
text: item.client_name,
'data-cn': item.client_name,
'data-csn': item.short_name,
'data-ct': item.client_type,
'data-cet': item.entity_type_id,
class: (item.client_type == 1) ? 'group' : (item.client_type == 2) ? 'individual' : ''
});
$('#client_id').append(option);
});
}
function appendBranch(data) {
$('#client_branch_id').empty();
$('#client_branch_id').append($('<option>', {
value: '',
text: 'Select Branch'
}));
$.each(data, function(index, item) {
var option = $('<option>', {
value: item.id,
text: item.branch_name
});
$('#client_branch_id').append(option);
});
}
function appendRenewalPolicies(data) {
console.log('appendRenewalPolicies', data);
console.log('selected_lead_form_type', selected_lead_form_type);
$('#source_policy_id').empty();
$('#source_policy_id').append($('<option>', {
value: '',
text: 'Select Policy',
}));
$.each(data, function(index, item) {
let shouldAppend = false;
if (selected_lead_form_type == 1) {
shouldAppend = (item.allocg !== "Non-EB" && item.allocg !== "Marine");
} else if (selected_lead_form_type == 2) {
shouldAppend = (item.allocg === "Non-EB" || item.allocg === "Marine");
} else {
shouldAppend = true;
}
if (shouldAppend) {
const option = $('<option>', {
value: item.id,
text: item.policy_type + ' - ' + item.policy_no,
});
$('#source_policy_id').append(option);
}
});
}
//--------------------------------------------------------------------------------------------------------
$(document).ready(function() {
$('#client_id').change(function() {
let client_id = $(this).val();
const $selected = $(this).find('option:selected');
const clientId = $selected.val();
const clientName = $selected.data('cn');
const shortName = $selected.data('csn');
const clientType = $selected.data('ct');
const entityTypeId = $selected.data('cet');
console.log('Client ID:', clientId);
console.log('Client Name:', clientName);
console.log('Short Name:', shortName);
console.log('Client Type:', clientType);
console.log('Entity Type ID:', entityTypeId);
$("#client_type").val(clientType);
$("#entity_type_id").val(entityTypeId);
$("#client_name").val(clientName);
$("#client_short_name").val(shortName);
if (client_id == 'add_client') {
// alert("Hello");
var myModal = new bootstrap.Modal(document.getElementById('upload_enrollment_model'));
myModal.show();
// $('#upload_enrollment_model').modal('toggle');
return;
}
console.log("client_id", client_id)
let client_type = $('#client_id option:selected').data('ct');
if (branch_list[client_id] != '' && branch_list[client_id] != null && client_id != '') {
console.log('branch_list', branch_list[client_id]);
let data = branch_list[client_id];
appendBranch(data);
} else {
if (temp_client_id == 0) {
console.log("Step 1: temp_client_id is 0 - No Branch Found for the selected Client");
console.log('second client_id', client_id);
if (client_id == null || client_id == 0) {
console.log("Step 2: client_id is null or 0 - Setting temp_client_id to 0");
temp_client_id = 0;
} else {
console.log("Step 3: client_id exists - Assigning temp_client_id = client_id");
temp_client_id = client_id;
toastr.warning("No Branch Found for the selected Client", 'Warning');
}
} else {
console.log("Step 4: temp_client_id is not 0 - Showing warning");
toastr.warning("No Branch Found for the selected Client", 'Warning');
}
}
})
$("#contact_person_summary").change(function () {
let selectedKey = $(this).val();
if (allContacts[selectedKey]) {
$('#contact_person_name').val(allContacts[selectedKey].name || '');
$('#contact_person_mobile').val(allContacts[selectedKey].mobile || '');
$('#contact_person_email').val(allContacts[selectedKey].email || '');
}
});
$('#client_branch_id').change(function() {
let client_branch_id = $(this).val();
console.log('client_branch_id', client_branch_id);
if (policy_list[client_branch_id] != '' && policy_list[client_branch_id] != null &&
client_branch_id != '' && client_branch_id != null) {
console.log('policy_list', policy_list[client_branch_id]);
let data = policy_list[client_branch_id];
appendRenewalPolicies(data);
} else {
$('#source_policy_id').empty();
// let lead_type = data.lead_type || null;
if (temp_branch_id == 0) {
console.log("Step 1: temp_branch_id is 0 - No Policies Found for the selected branch");
console.log('second client_branch_id', client_branch_id);
if (client_branch_id == null || client_branch_id == 0) {
console.log("Step 2: client_branch_id is null or 0 - Setting temp_branch_id to 0");
temp_branch_id = 0;
} else {
console.log("Step 3: v exists - Assigning temp_branch_id = client_branch_id");
temp_branch_id = client_id;
if (lead_type != 3) {
toastr.warning("No Policies Found for the selected Branch", 'Warning');
}
}
} else {
console.log("Step 4: temp_branch_id is not 0 - Showing warning");
if (lead_type != 3) {
toastr.warning("No Policies Found for the selected Branch", 'Warning');
}
}
}
})
});
//--------------------------------------------------------------------------------------------------------
// Salse team user list data
function selecSalsePerson(salse_person_ids) {
salse_person_ids = JSON.parse(salse_person_ids);
// Loop through each insurer-branch combination
$.each(salse_person_ids, function(index, value) {
// Use value in the format 'branch_id-insurer_id'
$('#salse_person_id option').each(function() {
if ($(this).val() === value) {
$(this).prop('selected', true); // Select the matching option
}
});
});
// Trigger change for plugins (like Select2)
$('#salse_person_id').trigger('change');
}
function validateInput(input, table, field){
let client_type = $('#client_type').val();
if(client_type == 2){
return;
}
if(client_type == ""){
toastr.warning('Please select the client type', 'WARNING');
$('#client_short_name').val('')
return;
}
let value = $(input).val();
let label = $(input).closest('.form-group').find('label').text().replace('*', '').trim();
let message = "Value is duplicate!";
if(label){
message = label + " already exists!";
}
checkDuplicateTableFieldValue(table, field, value, function(isDuplicate) {
if (isDuplicate) {
toastr.warning(message, 'WARNING');
// $(input).val('')
$('#lead_form_submit_btn').prop('disabled', true);
} else{
$('#lead_form_submit_btn').prop('disabled', false);
}
});
}
// --------------------------------------------------------------------------------------------------------
function addFileField(increment) {
console.log('addFileField function called');
const container = document.getElementById(`multiFileAppendArea_${increment}`);
if (!container) return;
const div = document.createElement("div");
div.className = "form-row d-flex align-items-end";
div.setAttribute("id", `fileField_${fileIndex}`);
let isFirstField = container.childElementCount === 0; // Check if it's the first field
let placeholder = isFirstField ? 'First file must be Demography.' : '';
let accept = isFirstField ? '.xls,.xlsx' : '';
if(selected_lead_form_type != 1){
placeholder = '';
accept = '';
}
let sample_dwn_link = "";
if(selected_lead_form_type == 1 && isFirstField == 1){
sample_dwn_link = '&nbsp;[ <a href="<?= base_url("util/download-excel/member_data"); ?>" id="excel_download" data-toggle="tooltip" data-placement="top" title="Download Sample Excel">Sample Excel</a> ]';
}
div.innerHTML = `
<div class="form-group col-md-5">
<label>Document Name ${sample_dwn_link}<span class="text-danger"></span></label>
<input type="text" class="form-control" name="docs_name_${increment}[]" placeholder="${placeholder}">
</div>
<div class="form-group col-md-5">
<label>File Upload<span class="text-danger"></span></label>
<input type="file" class="form-control" id="file_name_${fileIndex}" name="file_name_${increment}[]" accept="${accept}">
</div>
<div class="col-md-2" style="position: relative; bottom: 16px;">
<button type="button" class="btn btn-danger" onclick="removeFileField(${fileIndex})">x</button>
<button type="button" class="btn btn-primary" onclick="addFileField(${increment})">+</button>
</div>
`;
container.appendChild(div);
fileIndex++;
}
function removeFileField(index, lead_file_id = null) {
if(index == 1){
toastr.warning("You can't remove the first file field", 'WARNING');
return false;
}
if(lead_file_id != null) {
Swal.fire({
title: "Do you want to remove this file?",
// text: "Do you want Save this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, Procced!"
}).then((result) => {
if (result.isConfirmed) {
let url = '<?= base_url('util/removeMultiFile') ?>';
let requestData = {
lead_file_id: lead_file_id
};
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
// Send AJAX request
sendAjaxRequestForGlobal(url, 'GET', requestData, function(response) {
console.log('Data fetched successfully:', response);
if (response.status == true) {
toastr.success(response.message, 'SUCCESS');
//remove the file field
const field = document.getElementById(`fileField_${index}`);
if(index != 1){
if (field) field.remove();
}
} else {
toastr.error(response.message, 'WARNING');
}
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
}, function(xhr, status, error) {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
console.error('Error fetching data:', error);
console.error(xhr.responseText);
toastr.error('An error occurred while checking the CD amount.', 'ERROR');
});
}else{
return false;
}
});
}else{
const field = document.getElementById(`fileField_${index}`);
if(index != 1){
if (field) field.remove();
}
}
}
function showFileName(input, index) {
if (input.files.length > 0) {
document.getElementById(`file_name_display_${index}`).textContent = input.files[0].name;
} else {
document.getElementById(`file_name_display_${index}`).textContent = "No file chosen";
}
}
</script>
<?php if (isset($lead_edit_data)) { ?>
<script>
var lead_type = "";
$(document).ready(async function () {
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
setTimeout(() => {
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
handleEbAndNonEbEdit(
<?= json_encode($lead_edit_data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP) ?>
);
}, 1000);
});
function handleEbAndNonEbEdit(data){
lead_type = data.lead_type || null;
if(data.lead_form_type == 1){
dynamicLeadsDataForEdit(data)
}else{
dynamicNonEbLeadsDataForEdit(data)
}
}
function dynamicLeadsDataForEdit(data) {
try {
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
console.log('########### THIS IS EB LEAD ###############')
console.log('Received data:', data);
let dataIncrement = 1;
fileIndex = data.lead_file_count + 1;
if (!data || typeof data !== 'object') {
console.error('Invalid data received for editing.');
return;
}
$('.btnDiv').hide();
$('#appendArea_' + dataIncrement).empty();
if (data.html) {
$('#appendArea_' + dataIncrement).append(data.html);
} else {
console.warn('HTML content missing in data.');
}
if (data.multi_file_html && data.multi_file_html != '') {
$('#multiFileAppendArea_' + dataIncrement).empty();
setTimeout(function(){
$('#multiFileAppendArea_' + dataIncrement).append(data.multi_file_html);
}, 2000)
} else {
console.warn('MULTI FILE HTML content missing in data.');
}
let policy_type_id = data.policy_type_id || null;
let lead_type = data.lead_type || null;
if ([1, 6, 7].includes(policy_type_id)) {
let newId = 'appendAreaForClaim_' + dataIncrement;
$('#appendAreaForClaim').attr('id', newId);
}
leadTypeBsedHideAndShow(lead_type);
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
if (lead_type == 1) {
$('.claim-row').hide();
if(data.is_client_created == null || data.is_client_created == 0 || data.is_client_created == ""){
$('#exixting_client').prop('checked', false);
$('#exixting_client').prop('disabled', true);
$('#client_id').closest('.form-group').hide();
$('#client_branch_id').closest('.form-group').hide();
$('#client_type').removeClass('readonly-select');
$('#entity_type_id').removeClass('readonly-select');
$("#gst").prop('readonly', false);
$("#pan").prop('readonly', false);
$("#branch_name").prop('readonly', false);
$("#branch_code").prop('readonly', false);
$("#contact_person_name").prop('readonly', false);
$("#contact_person_mobile").prop('readonly', false);
$("#contact_person_email").prop('readonly', false);
$("#client_name").prop('readonly', false);
$("#client_short_name").prop('readonly', false);
$("#client_id").prop("required", false);
$("#client_branch_id").prop("required", false);
}else{
$('#exixting_client').prop('checked', true);
$('#exixting_client').prop('disabled', true);
$('#client_id').closest('.form-group').show();
$('#client_branch_id').closest('.form-group').show();
$('#client_type').addClass('readonly-select');
$('#entity_type_id').addClass('readonly-select');
$("#client_name").prop('readonly', true);
$("#client_short_name").prop('readonly', true);
$("#gst").prop('readonly', true);
$("#pan").prop('readonly', true);
$("#branch_name").prop('readonly', true);
$("#branch_code").prop('readonly', true);
$("#contact_person_name").prop('readonly', true);
$("#contact_person_mobile").prop('readonly', true);
$("#contact_person_email").prop('readonly', true);
$("#client_id").prop("required", true);
$("#client_branch_id").prop("required", true);
}
} else {
$('.claim-row').show();
if (policy_type_id == 1) {
$('.gpaClaimFileds').show();
$('.lifeClaimFields').hide();
} else if (policy_type_id === 6 || policy_type_id === 7) {
$('.gpaClaimFileds').hide();
$('.lifeClaimFields').show();
} else {
// updateRenewalFields(dataIncrement);
// let incurred_claim_date_id = 'incurred_claim_date_' + dataIncrement;
let incurred_claim_date_id = 'incurred_claim_date';
if ($('#' + incurred_claim_date_id).length) {
flatpickr("#" + incurred_claim_date_id, {
dateFormat: "d/m/Y",
allowInput: false,
onChange: function (selectedDates) {
try {
let increment = this.input.id.split('_').pop();
let policyStartDate = $("#source_policy_start_date");
if (policyStartDate.val()) {
calculatePolicyRunDays(increment);
}
} catch (error) {
console.error('Error in incurred_claim_datepicker:', error);
}
}
});
} else {
console.warn(`Incurred claim date field #${incurred_claim_date_id} not found.`);
}
// console.log($('#proposed_insurer_' + dataIncrement).length);
$('#proposed_insurer_' + dataIncrement).select2();
$('#proposed_tpa_' + dataIncrement).select2();
}
}
// if (lead_type == 3) {
// $('#client_id').prepend($('<option>', {
// value: 'add_client',
// text: '+ Add Client'
// }));
// console.log("trying to remove required ");
// $("#source_policy_id").prop("required",false);
// $("#source_policy_id").closest("div") .find("label .text-danger").remove();
// } else if (lead_type != 3) {
// $("#source_policy_id").prop("required",true);
// $("#source_policy_id").prop("required", true);
// // Check if the asterisk doesn't already exist, then add it
// let $label = $("#source_policy_id")
// .closest("div")
// .find("label");
// if ($label.find(".text-danger").length === 0) {
// $label.append(' <span class="text-danger">*</span>');
// }
// var addOption = $('#client_id option[value="add_client"]');
// if (addOption.length > 0) {
// addOption.remove();
// }
// console.log("rying to remove required and option removed from client")
// }
hide_list_show_add();
$('#page_title').text('Edit Lead');
$('#leads_primarykey').val(data.id || '');
$('#policy_start_date').val(data.policy_end_date || '');
$('#lead_type').val(data.lead_type || '');
$('#issuer').val(data.issuer || '');
$('#client_type').val(data.client_type || '');
$('#client_name').val(data.client_name || '');
$('#client_short_name').val(data.client_short_name || '');
$('#entity_type_id').val(data.entity_type_id || '');
$('#lead_status').val(data.status || '');
$('#notes').val(data.notes || '');
$('#next_reminder_date').val(data.next_reminder_date || '');
if (data.is_insurer_auto_mail == 1) {
$('#is_insurer_auto_mail').prop('checked', true);
} else {
$('#is_insurer_auto_mail').prop('checked', false);
}
// setTimeout(() => {
// $('#client_id').val(data.client_id || '').change();
// setTimeout(() => {
// $('#client_branch_id').val(data.client_branch_id || '').change();
// setTimeout(() => {
// $('#source_policy_id').val(data.source_policy_id || '');
// $('#source_policy_end_date').val(data.source_policy_end_date || '');
// $('#source_policy_start_date').val(data.source_policy_start_date || '');
// $('#pan').val(data.pan || '');
// $('#gst').val(data.gst || '');
// $('#branch_name').val(data.branch_name || '');
// $('#branch_code').val(data.branch_code || '');
// $('#contact_person_name').val(data.contact_person_name || '');
// $('#contact_person_mobile').val(data.contact_person_mobile || '');
// $('#contact_person_email').val(data.contact_person_email || '');
// $('#policy_type_id_1').val(data.policy_type_id || '').select2();
// $('.loader').fadeOut();
// $('.loader-mask').delay(350).fadeOut('slow');
// }, 1000);
// }, 1000);
// }, 5000);
// setTimeout(() => {
populateForm(data);
// },5000);
let insurer = (data.insurer_branch_id && data.insurer_id)
? `${data.insurer_branch_id}-${data.insurer_id}`
: '';
let tpa = (data.tpa_branch_id && data.tpa_id)
? `${data.tpa_branch_id}-${data.tpa_id}`
: '';
let proposed_insurer = (data.proposed_insurer_branch_id && data.proposed_insurer_id)
? `${data.proposed_insurer_branch_id}-${data.proposed_insurer_id}`
: '';
let proposed_tpa = (data.proposed_tpa_branch_id && data.proposed_tpa_id)
? `${data.proposed_tpa_branch_id}-${data.proposed_tpa_id}`
: '';
$('#insurer_1').val(insurer).select2();
$('#tpa_1').val(tpa).select2();
$('#policy_start_date_1').val(data.policy_start_date || '');
$('#policy_end_date_1').val(data.policy_end_date || '');
$('#file_name_display').text(`Upload File Name : ${data.file_name || 'N/A'}`);
if (data.salse_person_id) {
selecSalsePerson(data.salse_person_id);
}
} catch (error) {
console.error('Error in dynamicLeadsDataForEdit function:', error);
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
}
}
function dynamicNonEbLeadsDataForEdit(data) {
try {
console.log('########### THIS IS NON EB LEAD ###############')
console.log('Received data:', data);
let dataIncrement = 1;
fileIndex = data.lead_file_count + 1;
if (!data || typeof data !== 'object') {
console.error('Invalid data received for editing.');
return;
}
$('#appendArea').empty();
$('#multiFileAppendArea_' + dataIncrement).empty();
if (data.html) {
$('#appendArea').append(data.html);
} else {
console.warn('HTML content missing in data.');
}
if (data.multi_file_html) {
// console.log(data.multi_file_html);
setTimeout(function(){
$('#multiFileAppendArea_' + dataIncrement).append(data.multi_file_html);
}, 1000)
} else {
console.warn('MULTI FILE HTML content missing in data.');
}
let policy_type_id = data.policy_type_id || null;
let lead_type = data.lead_type || null;
leadTypeBsedHideAndShow(lead_type);
if(data.client_id == 0 || data.client_id == null){
temp_client_id = 0;
}else{
temp_client_id = data.client_id
}
if(lead_type == 1){
if(data.is_client_created == null || data.is_client_created == 0 || data.is_client_created == ""){
$('#exixting_client').prop('checked', false);
$('#exixting_client').prop('disabled', true);
$('#client_id').closest('.form-group').hide();
$('#client_branch_id').closest('.form-group').hide();
$('#client_type').removeClass('readonly-select');
$('#entity_type_id').removeClass('readonly-select');
$("#gst").prop('readonly', false);
$("#pan").prop('readonly', false);
$("#branch_name").prop('readonly', false);
$("#branch_code").prop('readonly', false);
$("#contact_person_name").prop('readonly', false);
$("#contact_person_mobile").prop('readonly', false);
$("#contact_person_email").prop('readonly', false);
$("#client_name").prop('readonly', false);
$("#client_short_name").prop('readonly', false);
$("#client_id").prop("required", false);
$("#client_branch_id").prop("required", false);
}else{
$('#exixting_client').prop('checked', true);
$('#exixting_client').prop('disabled', true);
$('#client_id').closest('.form-group').show();
$('#client_branch_id').closest('.form-group').show();
$('#client_type').addClass('readonly-select');
$('#entity_type_id').addClass('readonly-select');
$("#client_name").prop('readonly', true);
$("#client_short_name").prop('readonly', true);
$("#gst").prop('readonly', true);
$("#pan").prop('readonly', true);
$("#branch_name").prop('readonly', true);
$("#branch_code").prop('readonly', true);
$("#contact_person_name").prop('readonly', true);
$("#contact_person_mobile").prop('readonly', true);
$("#contact_person_email").prop('readonly', true);
$("#client_id").prop("required", true);
$("#client_branch_id").prop("required", true);
}
}
$('#page_title').text('Edit Lead');
$('#leads_primarykey').val(data.id || '');
$('#policy_start_date').val(data.policy_end_date || '');
$('#lead_type').val(data.lead_type || '');
$('#issuer').val(data.issuer || '');
$('#client_type').val(data.client_type || '');
$('#client_name').val(data.client_name || '');
$('#client_short_name').val(data.client_short_name || '');
$('#entity_type_id').val(data.entity_type_id || '');
$('#lead_status').val(data.status || '');
$('#notes').val(data.notes || '');
// setTimeout(() => {
// $('#client_id').val(data.client_id || '').change();
// setTimeout(() => {
// $('#client_branch_id').val(data.client_branch_id || '').change();
// setTimeout(() => {
// $('#source_policy_id').val(data.source_policy_id || '');
// $('#pan').val(data.pan || '');
// $('#gst').val(data.gst || '');
// $('#branch_name').val(data.branch_name || '');
// $('#branch_code').val(data.branch_code || '');
// $('#contact_person_name').val(data.contact_person_name || '');
// $('#contact_person_mobile').val(data.contact_person_mobile || '');
// $('#contact_person_email').val(data.contact_person_email || '');
// $('#policy_type_id').val(data.policy_type_id || '').select2();
// $('.loader').fadeOut();
// $('.loader-mask').delay(350).fadeOut('slow');
// $(".loss_date").each(function () {
// flatpickr(this, {
// dateFormat: "d-m-Y",
// });
// });
// }, 1000);
// }, 1000);
// }, 2000);
populateForm(data);
let insurer = (data.insurer_branch_id && data.insurer_id)
? `${data.insurer_branch_id}-${data.insurer_id}`
: '';
let tpa = (data.tpa_branch_id && data.tpa_id)
? `${data.tpa_branch_id}-${data.tpa_id}`
: '';
let proposed_insurer = (data.proposed_insurer_branch_id && data.proposed_insurer_id)
? `${data.proposed_insurer_branch_id}-${data.proposed_insurer_id}`
: '';
let proposed_tpa = (data.proposed_tpa_branch_id && data.proposed_tpa_id)
? `${data.proposed_tpa_branch_id}-${data.proposed_tpa_id}`
: '';
$('#insurer').val(insurer).select2();
$('#tpa').val(tpa).select2();
$('#policy_start_date').val(data.policy_start_date || '');
$('#policy_end_date').val(data.policy_end_date || '');
$('#file_name_display').text(`Upload File Name : ${data.file_name || 'N/A'}`);
if (data.salse_person_id) {
selecSalsePerson(data.salse_person_id);
}
let referenceDiv = document.getElementById('appendAreaForClaim');
referenceDiv.innerHTML = '';
referenceDiv.insertAdjacentHTML('beforeend', data.claims_details_html);
console.log("wdesfgefwregfefwregffeegf",data.lead_type);
var lead_type_value = data.lead_type;
} catch (error) {
console.error('Error in dynamicLeadsDataForEdit function:', error);
}
}
function waitForDropdownValue(selector, value, callback, retries = 20) {
const trySet = () => {
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
const $el = $(selector);
if ($el.find(`option[value="${value}"]`).length > 0) {
$el.val(value).trigger('change');
callback();
} else if (retries > 0) {
setTimeout(trySet, 300); // retry without re-passing arguments
retries--;
} else {
console.warn(`Value ${value} not found for ${selector}`);
callback(); // Proceed anyway to avoid hanging
}
};
trySet();
}
function populateForm(data) {
let lead_type = data.lead_type || null;
waitForDropdownValue('#client_id', data.client_id || '', () => {
if (lead_type == 3) {
// Add "+ Add Client" option if not already present
if ($('#client_id option[value="add_client"]').length === 0) {
$('#client_id').prepend($('<option>', {
value: 'add_client',
text: '+ Add Client'
}));
}
console.log("Trying to remove 'required' attribute and asterisk");
$("#source_policy_id").prop("required", false);
$("#source_policy_id").closest("div").find("label .text-danger").remove();
} else {
if(lead_type == 2){
$("#source_policy_id").prop("required", true);
}else{
$("#source_policy_id").prop("required", false);
}
// Add asterisk if not present
let $label = $("#source_policy_id").closest("div").find("label");
if ($label.find(".text-danger").length === 0) {
$label.append(' <span class="text-danger">*</span>');
}
// Remove "+ Add Client" option if exists
$('#client_id option[value="add_client"]').remove();
console.log("Set 'required' for source_policy_id and removed add_client option if present");
}
waitForDropdownValue('#client_branch_id', data.client_branch_id || '', () => {
// Fill other form fields
$('#source_policy_id').val(data.source_policy_id || '');
$('#source_policy_end_date').val(data.source_policy_end_date || '');
$('#source_policy_start_date').val(data.source_policy_start_date || '');
$('#pan').val(data.pan || '');
$('#gst').val(data.gst || '');
$('#branch_name').val(data.branch_name || '');
$('#branch_code').val(data.branch_code || '');
$('#contact_person_name').val(data.contact_person_name || '');
$('#contact_person_mobile').val(data.contact_person_mobile || '');
$('#contact_person_email').val(data.contact_person_email || '');
$('#policy_type_id').val(data.policy_type_id || '').select2();
$('#policy_type_id_1').val(data.policy_type_id || '').select2();
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
$(".loss_date").each(function () {
flatpickr(this, {
dateFormat: "d-m-Y"
});
});
setTimeout(function () {
let selectedOption = $('#contact_person_summary option').filter(function () {
return $(this).text().trim() === (data.contact_person_name || '').trim();
});
if (selectedOption.length) {
selectedOption.prop('selected', true);
console.log("Selected option:", selectedOption.text().trim(), "Value:", selectedOption.val());
} else {
console.log("No match found for:", data.contact_person_name);
}
$('#contact_person_summary').trigger('change');
}, 2000);
});
});
}
</script>
<?php } ?>