nhance/app/Views/tpa_branch.php
2025-09-03 10:35:02 +05:30

754 lines
32 KiB
PHP
Executable File
Raw 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.

<style>
.modal-subtitle {
font-weight: 500;
font-style: normal;
font-size: 18px;
line-height: 100%;
letter-spacing: 0%;
text-align: center;
margin-top: 5px;
color: #6c757d;
}
</style>
<!-- Optional CSS -->
<style>
/* Make modal really XL and scrollable */
#branchModal .modal-dialog.modal-xl {
max-width: 90%;
height: 90vh;
}
#branchModal .modal-content {
height: 100%;
overflow: hidden;
}
#branchModal .modal-body {
overflow-y: auto;
max-height: 80vh;
}
</style>
<div class="tab-pane fade" id="branch-tab" style="padding-left: 22px;">
<div class="row float-right" style="padding-bottom: 10px;position: relative;">
<a href="#" id="btnBranchAdd" class="btn app-btn-primary app-btn-border-radius mr-2"><span class="mdi mdi-plus" aria-hidden="true" style="padding: 5px 10px;"></span>Add Branch</a>
</div>
<div class="table-responsive" id="branch_table" >
<table data-custom-table-css="table" class="table table-borderless table-nowrap mb-0" id="tpa-branch-datatable">
<thead class="">
<tr>
<th>Branch Name</th>
<th>Branch Code</th>
<th>Action</th>
</tr>
</thead>
<tbody id="tpa_branch_list">
</tbody>
</table>
</div>
<!-- <div class="row" id="add_branch">
<div class="col-12">
<div class="card-body">
<div class="row float-right" style="position: relative; bottom: 20px; right: 13px;">
<button type="button" id="btnBranchBack" class="btn btn-primary waves-effect waves-light btn-sm btnBack"><span class="mdi mdi-format-list-bulleted" aria-hidden="true" style="padding: 5px 10px;"></span>Back To List</button>
</div>
<hr>
<form role="form" class="parsley-examples" method="post" id="tpa_branch_form" enctype="multipart/form-data">
<input type="hidden" class="form-control" value="<?= csrf_hash() ?>" name="<?= csrf_token() ?>" />
<input type="hidden" name="PrimaryKey" id="tpa_Branch_PrimaryKey" />
<input type="hidden" name="tpa_id" id="tpa_id_branch" value="<?= isset($tpa['id']) ? $tpa['id'] : '' ?>"/>
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label for="branch_name">Branch Name<span
class="text-danger">*</span></label>
<input type="text" class="form-control" id="branch_name"
placeholder="Enter Branch Name" name="branch_name" required>
</div>
<div class="form-group col-md-6">
<label for="branch_code">Branch Code<span
class="text-danger">*</span></label>
<input type="text" class="form-control" id="branch_code"
placeholder="Enter Branch Code" name="branch_code" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="address1">Address Line 1<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="address1" name="address1" placeholder="Enter Address 1" required>
</div>
<div class="form-group col-md-6">
<label for="address2">Address Line 2<span class="text-danger"></span></label>
<input type="text" class="form-control" id="address2" placeholder="Enter Address 2" name="address2">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-3">
<label for="state">State<span
class="text-danger">*</span></label>
<select class="form-control" id="state" name="state" required>
<option value="">Select State</option>
<?php if ($state) {
foreach($state as $value) { ?>
<option value="<?= $value['id'] ?>"><?= $value['state'] ?></option>
<?php } }?>
</select>
</div>
<div class="form-group col-md-3">
<label for="district">District<span
class="text-danger">*</span></label>
<input type="text" class="form-control" id="district"
placeholder="Enter District" name="district" required>
</div>
<div class="form-group col-md-3">
<label for="city">City</label>
<input type="text" class="form-control" id="branch_city"
placeholder="Enter City" name="city" required>
</div>
<div class="form-group col-md-3">
<label for="gst">Pincode<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="pincode" placeholder="Enter Pincode" name="pincode" required>
</div>
</div>
<hr>
<h6 class="header-title">Contact 1</h6>
<br>
<div class="form-row">
<div class="form-group col-md-6">
<label for="first_name">Name<span class="text-danger">*</span></label>
<input value="" type="text" class="form-control" placeholder="Enter Contact Name" name="name[]" id="name" required>
</div>
<div class="form-group col-md-6">
<label for="designation">Designation<span class="text-danger">*</span></label>
<input value="" type="text" class="form-control" placeholder="Enter Designation Name" name="designation[]" id="designation" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="last_name">Email<span class="text-danger">*</span></label>
<input value="" data-parsley-type="email" type="email" data-parsley-type="email" class="form-control" placeholder="Enter Contact Email" name="email[]" id="email" required>
</div>
<div class="form-group col-md-6">
<label for="mobile">Mobile<span class="text-danger">*</span></label>
<input value="" data-parsley-type="number" data-parsley-length="[10,10]" type="tel" class="form-control" placeholder="Enter Contact Mobile" name="mobile[]" id="mobile" required>
</div>
</div>
<div class="form-group" style="display: flex;">
<button style="margin-right: 10px;" type="button" class="btn btn-primary btn-sm ac" onclick="appendContactHtml()" id="add">Add Contact</button>
<button type="button" class="btn btn-danger btn-sm" onclick="removeContact(this)" id="${uniqueId}_remove">Remove</button>
</div>
<div id="container"></div>
</div>
<div class="form-group text-right m-b-0">
<button type="submit" class="btn btn-primary waves-effect waves-light mr-1"
id="btnSubmit">Submit</button>
<button type="button" class="btn btn-secondary waves-effect btnBack"
id="btnBack">Cancel</button>
</div>
</form>
</div>
</div>
</div> !-- end col-- -->
</div>
<!-- end -->
<!-- Modal -->
<div class="modal fade" id="branchModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-xl" style="max-width: 1140px !important;">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header flex-column">
<div class="d-flex w-100 justify-content-between align-items-center">
<h4 class="modal-title mb-0" id="modalLabel">New Branch</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
</div>
<!-- Modal Body -->
<div class="modal-body" style="overflow-y:auto; max-height:80vh;">
<form id="insurer_branch_form" class="parsley-examples" method="post" enctype="multipart/form-data">
<div class="row">
<!-- Left Section: Branch Details -->
<div class="col-md-8 d-flex flex-column" style="max-height: calc(80vh - 50px);">
<!-- Branch Name & Code -->
<div class="form-row">
<div class="form-group col-md-6">
<label for="branch_name">Branch Name <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="branch_name" name="branch_name" required>
</div>
<div class="form-group col-md-6">
<label for="branch_code">Branch Code <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="branch_code" name="branch_code" required>
</div>
</div>
<!-- Address Lines -->
<div class="form-row">
<div class="form-group col-md-6">
<label for="address1">Address Line 1 <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="address1" name="address1" required>
</div>
<div class="form-group col-md-6">
<label for="address2">Address Line 2</label>
<input type="text" class="form-control" id="address2" name="address2">
</div>
</div>
<!-- State & District -->
<div class="form-row">
<div class="form-group col-md-6">
<label for="state">State <span class="text-danger">*</span></label>
<select class="form-control" id="state" name="state" required>
<option value="">Select State</option>
<?php foreach($state as $value) { ?>
<option value="<?= $value['id'] ?>"><?= $value['state'] ?></option>
<?php } ?>
</select>
</div>
<div class="form-group col-md-6">
<label for="district">District <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="district" name="district" required>
</div>
</div>
<!-- City & Pincode -->
<div class="form-row">
<div class="form-group col-md-6">
<label for="branch_city">City <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="branch_city" name="city" required>
</div>
<div class="form-group col-md-6">
<label for="pincode">Pincode <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="pincode" name="pincode" required>
</div>
</div>
<!-- Submit / Cancel -->
<div class="form-row">
<!-- <div class="form-group col-md-12 mt-2"> -->
<div class="mt-2">
<button type="submit" class="btn app-btn-secondary app-btn-border-radius mr-2" id="btnSubmit">Submit</button>
<button type="button" class="btn app-btn-outline-secondary app-btn-border-radius btnBack" id="btnBack" data-dismiss="modal" aria-hidden="true">Cancel</button>
</div>
<!-- </div> -->
</div>
</div>
<!-- Right Section: Contact Details -->
<div class="col-md-4" style="background:#f8f9fa; max-height: calc(80vh - 50px); overflow-y:auto; border-radius:8px; padding:10px;">
<!-- First Contact -->
<div class="bg-white p-2 mb-2 rounded">
<div class="d-flex justify-content-between align-items-center mb-2">
<h6 class="modal-subtitle mb-0">Contact 1</h6>
<div class="d-flex align-items-center">
<i class="mdi mdi-plus text-primary ac me-3" style="cursor:pointer; font-size:24px !important;" onclick="appendContactHtml()" id="add"></i>
<i class="mdi mdi-trash-can text-danger" style="cursor:pointer; font-size:24px !important;" onclick="removeContact(this)" id="contact_1_remove"></i>
</div>
</div>
<!-- Contact Form Fields -->
<input type="hidden" name="primarykey[]" id="contact_id_1" value=""/>
<div class="form-group mb-2">
<label for="name_1">Name <span class="text-danger">*</span></label>
<input type="text" class="form-control" placeholder="Enter Contact Name" name="name[]" id="name_1" required>
</div>
<div class="form-group mb-2">
<label for="designation_1">Designation <span class="text-danger">*</span></label>
<input type="text" class="form-control" placeholder="Enter Designation Name" name="designation[]" id="designation_1" required>
</div>
<div class="form-group mb-2">
<label for="email_1">Email <span class="text-danger">*</span></label>
<input type="email" class="form-control" placeholder="Enter Contact Email" name="email[]" id="email_1" required>
</div>
<div class="form-group mb-2">
<label for="mobile_1">Mobile <span class="text-danger">*</span></label>
<input type="tel" class="form-control" placeholder="Enter Contact Mobile" pattern="[0-9]{10}" name="mobile[]" id="mobile_1" required>
</div>
</div>
<!-- Container for dynamically added contacts -->
<div id="container"></div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
var contactCount = 1;
$(document).ready(function () {
$('#tpa-branch-datatable').DataTable({
paging: true ,
searching: true
});
var tpa_Branch_PrimaryKey = $('#tpa_id_branch').val();
var tpa_branch_form_action = '';
var myModal = new bootstrap.Modal(document.getElementById('branchModal'));
myModal.hide();
// $('#add_branch').hide();
$('.btnBack').hide();
loadBranchList();
$('#btnBranchAdd').click(function(){
tpa_branch_form_action = '<?= base_url("master/tpa/branch/create"); ?>';
var myModal = new bootstrap.Modal(document.getElementById('branchModal'));
myModal.show();
// $('#add_branch').show();
// $('#branch_table').hide();
$('.btnBack').show();
// $('#btnBranchAdd').hide();
$('#branch_name').val('');
$('#branch_code').val('');
$('#state').val('');
$('#district').val('');
$('#branch_city').val('');
$('#name').val('');
$('#email').val('');
$('#mobile').val('');
$('#designation').val('');
var addButton = document.getElementById('add');
if (addButton.style.display === 'none') {
addButton.style.display = 'block';
}
contactCount = 1
var buttonIds = JSON.parse(localStorage.getItem('buttonIds')) || [];
for (var i = 0; i < buttonIds.length; i++) {
var firstElement = buttonIds[i].split('_')[0];
console.log(firstElement);
// Find the element by its ID
var elementToRemove = document.getElementById(firstElement);
console.log(elementToRemove);
if (elementToRemove) {
console.log(elementToRemove);
elementToRemove.parentNode.removeChild(elementToRemove);
}
localStorage.removeItem('buttonIds')
}
})
$('.btnBack').click(function(){
var myModal = new bootstrap.Modal(document.getElementById('branchModal'));
myModal.hide();
// $('#add_branch').hide();
// $('#branch_table').show();
$('.btnBack').hide();
// $('#btnBranchAdd').show();
$('#branch_name').val('');
$('#branch_code').val('');
$('#state').val('');
$('#district').val('');
$('#branch_city').val('');
$('#name').val('');
$('#email').val('');
$('#mobile').val('');
$('#designation').val('');
})
if(tpa_Branch_PrimaryKey !== ''){
var branchTable = '';
var data = <?= isset($tpa_branch) ? json_encode($tpa_branch) : '[]' ?>;
$.each(data, function(index, item) {
branchTable += `
<tr>
<td>${item.branch_name}</td>
<td>${item.branch_code}</td>
<td><a class="mdi mdi-lead-pencil btnBranchEdit" href="#" data-id="${item.id}" style="font-size:18px;"></a></td>
</tr>
`;
});
$('#tpa_branch_list').append(branchTable);
}
$("#tpa_branch_form").submit(function(events) {
events.preventDefault();
var isValid = $('#tpa_branch_form').parsley().validate();
if (isValid) {
var formData = new FormData($('#tpa_branch_form')[0]);
$.ajax({
data: formData,
url: tpa_branch_form_action,
type: "POST",
dataType: 'json',
processData: false,
contentType: false,
success: function(res) {
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
}, 1000);
console.log(res);
$('#tpa_branch_list tr').remove();
var tpaBranchTableInsert = ''
if (data.length === 0) {
tpaBranchTableInsert += `
<tr>
<td colspan="3" class="text-center app-text-black">No data available in table</td>
</tr>
`;
} else {
$.each(res.data, function(index, item) {
tpaBranchTableInsert += `
<tr>
<td>${item.branch_name}</td>
<td>${item.branch_code}</td>
<td>
<a class="mdi mdi-lead-pencil btnBranchEdit" href="#" data-id="${item.id}" style="font-size:18px;"></a>
<a style="color:#02a8b5" data-id="${item.id}" onclick="removeTPABranch(this)"><i data-id="${item.id}" class="mdi mdi-delete" style="font-size:18px;"></i></a>
</td>
</tr>
`;
});
}
$('#tpa_branch_list').append(tpaBranchTableInsert);
var myModal = new bootstrap.Modal(document.getElementById('branchModal'));
myModal.hide();
// $('#add_branch').hide();
// $('#branch_table').show();
$('.btnBack').hide();
// $('#btnBranchAdd').show();
},
error: function (xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
toastr.warning('Something Wrong!', 'warning');
}, 1000);
}
});
}
});
$('body').on('click', '.btnBranchEdit', function () {
$('#container').html("");
contactCount =1;
var branch_id = $(this).attr('data-id');
tpa_branch_form_action = '<?= base_url("master/tpa/branch/edit"); ?>';
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
$.ajax({
url: '<?php echo base_url('master/tpa/branch/editget/');?>'+branch_id,
type: "GET",
dataType: 'json',
success: function (res) {
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
}, 1000);
console.log("tpa.............");
console.log(res)
var myModal = new bootstrap.Modal(document.getElementById('branchModal'));
myModal.show();
// $('#add_branch').show();
// $('#branch_table').hide();
$('.btnBack').show();
// $('#btnBranchAdd').hide();
$('#branch_name').val(res.tpabranch.branch_name);
$('#branch_code').val(res.tpabranch.branch_code);
$('#state option[value="' + res.tpabranch.state + '"]').prop('selected', true);
$('#district').val(res.tpabranch.district);
$('#branch_city').val(res.tpabranch.city);
$('#tpa_Branch_PrimaryKey').val(res.tpabranch.id);
$('#pincode').val(res.tpabranch.pincode);
$('#address1').val(res.tpabranch.address1);
$('#address2').val(res.tpabranch.address2);
$('.form-row').find('#name').val(res.levelcontact[0].name);
$('#email').val(res.levelcontact[0].email);
$('#mobile').val(res.levelcontact[0].mobile);
$('#designation').val(res.levelcontact[0].designation);
res.levelcontact.shift();
$.each(res.levelcontact, function(index, contact) {
console.log(contact);
if (contact !== undefined) {
appendContactHtml(contact);
}
});
},
error: function (xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
toastr.warning('Something Wrong!', 'warning');
}, 1000);
}
});
});
function loadBranchList()
{
var base_url = '<?= base_url("master/tpa/branch/list/"); ?>';
var tpa_branch_action = base_url + tpa_Branch_PrimaryKey;
if ($('#tpa_id_branch').val() != '') {
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
$.ajax({
url: tpa_branch_action,
type: 'GET',
success: function(response) {
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
}, 1000);
var branchTable = '';
if (data.length === 0) {
branchTable += `
<tr>
<td colspan="3" class="text-center app-text-black ">No data available in table</td>
</tr>
`;
} else {
$.each(response.tpa_branch, function(index, item) {
branchTable += `
<tr>
<td>${item.branch_name}</td>
<td>${item.branch_code}</td>
<td>
<a class="mdi mdi-lead-pencil btnBranchEdit" href="#" data-id="${item.id}" style="font-size:18px;"></a>
<a style="color:#02a8b5" data-id="${item.id}" onclick="removeTPABranch(this)"><i data-id="${item.id}" class="mdi mdi-delete" style="font-size:18px;"></i></a>
</td>
</tr>
`;
});
}
$('#tpa_branch_list').html(branchTable);
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
toastr.warning('Something Wrong!', 'warning');
}, 1000);
}
});
}
}
});
function appendContactHtml(contact = false, reset = false) {
console.log('appendContactHtml function called ')
contactCount++;
var container = document.getElementById('container');
var uniqueId = Date.now().toString();
var html = `
<div id="${uniqueId}">
<hr>
<h6 class="header-title">Contact ${contactCount}</h6>
<br>
<div class="form-row">
<div class="form-group col-md-6">
<label for="${uniqueId}_first_name">Name<span class="text-danger">*</span></label>
<input value="${contact !== undefined && contact !== false ? contact.name : ''}" type="text" class="form-control" placeholder="Enter Contact Name" name="name[]" id="${uniqueId}_name" required>
</div>
<div class="form-group col-md-6">
<label for="${uniqueId}_designation">Designation<span class="text-danger">*</span></label>
<input value="${contact !== undefined && contact !== false ? contact.designation : ''}" type="text" class="form-control" placeholder="Enter Designation Name" name="designation[]" id="${uniqueId}_designation" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="${uniqueId}_last_name">Email<span class="text-danger">*</span></label>
<input data-parsley-type="email" value="${contact !== undefined && contact !== false ? contact.email : ''}" type="email" class="form-control" placeholder="Enter Contact Email" name="email[]" id="${uniqueId}_email" required>
</div>
<div class="form-group col-md-6">
<label for="${uniqueId}_mobile">Mobile<span class="text-danger">*</span></label>
<input value="${contact !== undefined && contact !== false ? contact.mobile : ''}" type="tel" class="form-control" placeholder="Enter Contact Mobile" name="mobile[]" id="${uniqueId}_mobile" required>
</div>
</div>
<div class="form-group" style="display: flex;">
<button style="margin-right: 10px;" type="button" class="btn btn-primary btn-sm ac" onclick="appendContactHtml()" id="${uniqueId}_add">Add Contact</button>
<button type="button" class="btn btn-danger btn-sm" onclick="removeContact(this)" id="${uniqueId}_remove">Remove</button>
</div>
</div>
`;
if(reset == false){
console.log(reset)
container.insertAdjacentHTML('beforeend', html);
storeButtonId(uniqueId + '_add');
if (contactCount >= 3) {
hideStoredAddButtons();
}
}
}
function removeContact(button) {
var uniqueId = button.id.split("_")[0];
var contactSection = document.getElementById(uniqueId);
if (contactSection) {
contactSection.parentNode.removeChild(contactSection);
contactCount --;
if (contactCount < 3) {
var addButton = document.querySelector('.ac');
if (addButton) {
addButton.style.display = 'block';
}
}
}
}
function storeButtonId(id) {
var buttonIds = JSON.parse(localStorage.getItem('buttonIds')) || [];
buttonIds.push(id);
localStorage.setItem('buttonIds', JSON.stringify(buttonIds));
}
function hideStoredAddButtons() {
var storedIds = JSON.parse(localStorage.getItem('buttonIds')) || [];
storedIds.forEach(function(id) {
var addButton = document.getElementById(id);
var first_addButton = document.querySelector('.ac')
if (addButton) {
addButton.style.display = 'none';
first_addButton.style.display = 'none';
}
});
}
function showNextAddButton() {
var storedIds = JSON.parse(localStorage.getItem('buttonIds')) || [];
var addButtonShown = false;
// Iterate through the stored IDs to find the next "Add" button to show
storedIds.forEach(function(id) {
if (!addButtonShown) {
var addButton = document.getElementById(id);
if (addButton && addButton.style.display === 'none') {
addButton.style.display = 'block';
addButtonShown = true; // Set to true once an "Add" button is shown
}
}
});
}
function validateForm() {
var isValid = true;
$('#branch_form input, #branch_form select').each(function() {
if ($(this).is('input[type="text"]') || $(this).is('select')) {
if ($.trim($(this).val()) == '') {
isValid = false;
return false;
}
}
});
return isValid;
}
function removeTPABranch(element) {
Swal.fire({
title: "Are you sure?",
text: "You need to remove this TPA branch.",
icon: "info",
showCancelButton: true,
confirmButtonColor: "#3085d6",
confirmButtonText: "Yes",
}).then((result) => {
if (result.isConfirmed) {
var id = element.getAttribute('data-id');
var form_action = '<?= base_url("master/tpa/branch/remove/") ?>' + id;
$.ajax({
url: form_action,
type: "GET",
dataType: 'json',
processData: false,
contentType: false,
success: function(res) {
// console.log(res.status == true);
if(res){
if (res.status == true) {
toastr.success('TPA branch removed successfully', 'success');
location.reload();
} else {
toastr.warning('Failed to remove TPA branch', 'warning');
}
}
},
error: function (xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
toastr.warning('Failed to remove TPA branch', 'warning');
}
});
}
});
}
</script>