nhance-enrollment/app/Views/rack_rate_auto_si.php

352 lines
13 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div id="rack_rate_auto_si_modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="fullWidthModalLabel"
aria-hidden="true">
<div class="modal-dialog modal-full-width">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="fullWidthModalLabel">Rack Rate Auto SI</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<form role="form" class="parsley-examples" method="post" id="auto_si_form"
enctype="multipart/form-data">
<input type="hidden" id="auto_si_pk" name="id">
<input type="hidden" id="client_policy_pk" name="client_policy_id">
<div class="form-row">
<div class="form-group col-md-12">
<label class="switch" style="position: relative;">
<input id="auto_si_enable" type="checkbox" name="is_auto_si">
<span class="slider round" style="height: 27px;"></span>
</label>
<label for="auto_si_enable" style="position: relative;bottom: 5px;left: 10px;">Enable Auto
SI</label>
</div>
</div>
<hr>
<div id="dynamicForm">
<div class="formGroupContainer"></div>
</div> <br><br>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="submitFormButton">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div><!-- /.modal -->
<script>
let rowIndex = 0;
var SIAMOUNT = []; //policy_premium_2 table data, rack rate si amount
$(document).ready(function() {
// Attach change event listener to check for duplicates
$('#dynamicForm').on('change', '.form-row input, .form-row select', function() {
checkForDuplicate($(this).closest('.form-row'), $(this));
});
});
// Function to add a new row
function addRow(button = null, data = null) {
rowIndex++;
// Hide the "Add More" button for the current row (if button is provided)
if (button) {
$(button).hide();
}
// Create a new row
const formGroupContainer = document.querySelector('#dynamicForm .formGroupContainer');
const newRow = document.createElement('div');
newRow.className = 'row form-row';
newRow.innerHTML = `
<div class="col-md-1" style="margin-top: 45px;">
<div class="form-check form-check-inline">
<input class="form-check-input family-checkbox" type="checkbox" id="selfCheckbox${rowIndex}" name="self" ${data && data.self == 1 ? 'checked' : ''}>
<label class="form-check-label" for="selfCheckbox${rowIndex}">Self</label>
</div>
</div>
<div class="col-md-2" style="margin-top: 45px;">
<div class="form-check form-check-inline">
<input class="form-check-input family-checkbox" type="checkbox" id="spouseCheckbox${rowIndex}" name="spouse" ${data && data.spouse == 1 ? 'checked' : ''}>
<label class="form-check-label" for="spouseCheckbox${rowIndex}">Spouse</label>
</div>
</div>
<div class="col-md-1">
<label class="form-label" for="childrenInput${rowIndex}">Children</label>
<input type="number" class="form-control sm-input family-input" id="childrenInput${rowIndex}" placeholder="0" value="${data && data.children ? data.children : 0}" min="0">
</div>
<div class="col-md-1">
<label class="form-label" for="eldersInput${rowIndex}">Elders</label>
<input type="number" class="form-control sm-input family-input" id="eldersInput${rowIndex}" placeholder="0" value="${data && data.elders ? data.elders : 0}" min="0">
</div>
<div class="col-md-4">
<label class="form-label" for="siAmountSelect${rowIndex}">Choose SI Amount</label>
<select class="form-select form-control si-select" id="siAmountSelect${rowIndex}">
<option value="">Select SI</option>
</select>
</div>
<div class="col-md-2" style="margin-top: 45px;">
<button type="button" class="btn btn-success addMoreBtn" onclick="addRow(this)">+</button>
<button type="button" class="btn btn-danger" onclick="removeRow(this)">-</button>
</div>
`;
formGroupContainer.appendChild(newRow);
let selectedValue = data && data.si ? data.si : null;
appendSIAmount(SIAMOUNT, selectedValue, rowIndex)
}
// Function to remove a row
function removeRow(button) {
const row = button.closest('.form-row');
const formGroupContainer = document.querySelector('.formGroupContainer');
// Prevent deletion of the first row
if (formGroupContainer.children.length === 1) {
alert("The first row cannot be deleted.");
return;
}
// If the removed row had the "Add More" button visible, show it for the previous row
if ($(button).siblings('.addMoreBtn').is(':visible')) {
const previousRow = row.previousElementSibling;
if (previousRow) {
$(previousRow).find('.addMoreBtn').show();
}
}
// Remove the row
row.remove();
}
// Function to check for duplicates
function checkForDuplicate(currentRow, changedElement) {
const currentValues = {
self: currentRow.find('input[name="self"]').is(':checked'),
spouse: currentRow.find('input[name="spouse"]').is(':checked'),
children: currentRow.find('input[id^="childrenInput"]').val(),
elders: currentRow.find('input[id^="eldersInput"]').val(),
// siAmount: currentRow.find('select[id^="siAmountSelect"]').val(),
};
let isDuplicate = false;
$('.form-row').not(currentRow).each(function() {
const otherRow = $(this);
const otherValues = {
self: otherRow.find('input[name="self"]').is(':checked'),
spouse: otherRow.find('input[name="spouse"]').is(':checked'),
children: otherRow.find('input[id^="childrenInput"]').val(),
elders: otherRow.find('input[id^="eldersInput"]').val(),
// siAmount: otherRow.find('select[id^="siAmountSelect"]').val(),
};
if (
currentValues.self === otherValues.self &&
currentValues.spouse === otherValues.spouse &&
currentValues.children === otherValues.children &&
currentValues.elders === otherValues.elders
// && currentValues.siAmount === otherValues.siAmount
) {
isDuplicate = true;
return false; // Exit the loop if duplicate is found
}
});
if (isDuplicate) {
alert("Duplicate values are not allowed!");
// currentRow.find('input, select').val('').prop('checked', false);
if (changedElement.is('input[type="checkbox"]')) {
changedElement.prop('checked', false);
} else {
changedElement.val('');
}
}
}
//Function to form submit
$(document).ready(function() {
$('#submitFormButton').on('click', function(e) {
e.preventDefault();
const formData = new FormData($('#auto_si_form')[0])
const familyComposition = getFormDataWithJSON();
console.log(formData);
console.log('familyComposition', familyComposition);
formData.append('family_composition', JSON.stringify(familyComposition));
let url = '<?= base_url('util/createAutoSI') ?>';
$.ajax({
url: url,
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response) {
console.log(response);
if (response.status) {
if (response.status == true) {
toastr.success(response.message, 'Success');
} else {
toastr.warning(response.message, 'Warning');
}
} else {
toastr.error(response.message || 'Unable to get responce', 'Error');
}
},
error: function(xhr, status, error) {
console.error('Error fetching data:', error);
console.error(xhr.responseText);
toastr.error('An error occurred while Auto SI Form Submit.', 'Error');
},
});
});
});
// Function to gather form data with JSON structure
function getFormDataWithJSON() {
const familyComposition = [];
$('#dynamicForm .form-row').each(function() {
const row = $(this);
const self = row.find('input[name="self"]').is(':checked') ? 1 : 0;
const spouse = row.find('input[name="spouse"]').is(':checked') ? 1 : 0;
const children = parseInt(row.find('input[id^="childrenInput"]').val()) || 0;
const elders = parseInt(row.find('input[id^="eldersInput"]').val()) || 0;
const si = row.find('select[id^="siAmountSelect"]').val() || null;
// if (si) {
familyComposition.push({
self: self,
spouse: spouse,
children: children,
elders: elders,
si: si,
});
// }
});
console.log('familyComposition', JSON.stringify(familyComposition))
return familyComposition;
}
function getRackRateSIAmountAndAutoSiDataForAutoSI(client_policy_id) {
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
$('.formGroupContainer').empty();
console.log('client_policy_id', client_policy_id);
$('#client_policy_pk').val(client_policy_id);
let url = '<?= base_url('util/getRackRateSIAmountAndAutoSiDataForAutoSI') ?>';
// Data to send in the AJAX request
let requestData = {
client_policy_id: client_policy_id,
};
// Send AJAX request
sendAjaxRequestForGlobal(url, 'GET', requestData, function(response) {
console.log('Data fetched successfully:', response);
if (response.status == true) {
SIAMOUNT = response.si_amount;
if (response.auto_si_amount !== null) {
console.log(response.auto_si_amount.family_composition)
let familyComp = JSON.parse(response.auto_si_amount.family_composition)
console.log('familyComp', familyComp)
familyComp.forEach((item) => {
addRow(null, item);
});
$('#auto_si_pk').val(response.auto_si_amount.id);
if (response.auto_si_amount.is_auto_si == 1) {
$('#auto_si_enable').prop('checked', true);
} else {
$('#auto_si_enable').prop('checked', false);
}
} else {
addRow();
}
var myModal = new bootstrap.Modal(document.getElementById('rack_rate_auto_si_modal'));
myModal.show();
} else {
if (response.status == false && response.code == 200) {
Swal.fire({
title: response.message,
icon: 'warning',
})
} else {
toastr.error(response.message || 'Unable to fetch data', 'Error');
addRow();
}
}
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
}, function(xhr, status, error) {
console.error('Error fetching data:', error);
console.error(xhr.responseText);
toastr.error('An error occurred while checking the SI amount.', 'Error');
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
// addRow();
});
}
function appendSIAmount(data, selectedValue = null, rowIndex = 0) {
console.log('data', data)
console.log('selectedValue', selectedValue)
console.log('rowIndex', rowIndex)
// Dynamically target the SI Amount Select element based on the rowIndex
const selectElement = $(`#siAmountSelect${rowIndex}`);
// Empty the current options
selectElement.empty();
// Add the default "Select SI" option
selectElement.append($('<option>', {
value: '',
text: 'Select SI'
}));
// Loop through the data to create the option elements
$.each(data, function(index, item) {
var option = $('<option>', {
value: item.si,
text: item.si,
});
// If selectedValue matches the item.id, set this option as selected
if (selectedValue == item.si) {
option.attr('selected', true);
}
// Append the option to the select element
selectElement.append(option);
});
}
</script>