nhance/app/Views/client_kyc_2.php

359 lines
14 KiB
PHP
Executable File

<style> .card-body{ margin-top: 0px !important; } </style>
<div class="tab-pane fade" id="KYC-DOC-tab">
<div id="others">
<div class="col-lg-12 col-sm-12 col-md-12">
<div class="card" style="margin-bottom: unset">
<div class="card-body"
style="margin-top: 0px !important;
margin-bottom: 0px !important;
padding-top: 0px !important;
padding-bottom: 0px !important;">
<h3>Documents</h3>
<form role="form" class="parsley-examples" method="post" id="kyc_form_add"
enctype="multipart/form-data">
<input type="hidden" class="form-control" value="<?= csrf_hash() ?>" name="<?= csrf_token() ?>" />
<input type="hidden" name="PrimaryKey" id="kyc_PrimaryKey" value="<?= isset($client['id']) ? $client['id'] : '' ?>"/>
<input type="hidden" name="client_id" id="client_id_kyc" value="<?= isset($client['id']) ? $client['id'] : '' ?>"/>
<input type="hidden" name="kyc_doc_type_id" value="">
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-3">
<label for="document_select">Document Name<span class="text-danger">*</span></label>
<select class="form-control document-select" id="docs_type_id" name="docs_type_id" required onchange="handleDocumentSelectChange(this)">
<option value="">Select Document</option>
<option value="other">Additional Document</option>
</select>
</div>
<div class="form-group col-md-3 other-docs-name-group" style="display:none;">
<label for="other_docs_name">Enter Document Name<span class="text-danger">*</span></label>
<input type="text" class="form-control other-docs-name-input" name="other_docs_name" placeholder="Enter file name">
</div>
<div class="form-group col-md-3 file-input-group">
<label for="file_input">Browser File<span class="text-danger">*</span></label>
<input type="file" class="form-control file-input" id="kyc_docs_file" name="file_name" required
accept=".pdf,.jpg,.jpeg,.png"
style="box-shadow: none !important; outline: none !important; border: none; height: unset !important;padding: 0px !important;background: transparent !important;">
</div>
<div class="form-group col-md-3" style="<?= isset($client['id']) ? 'margin-top: 41px;' : 'margin-top: 30px;' ?>">
<button type="submit" class="btn btn-sm waves-effect waves-light mr-1"id="btnSubmit">Save</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div> <!-- end col-->
</div> <!-- end row -->
<div class="col-lg-12">
<div class="card" id="collapseOne">
<div class="card-body">
<div class="table-responsive">
<table data-custom-table-css="second-table" id="kyc_table" class="table table-sm mb-0">
<thead>
<tr>
<th>S. No</th>
<th>Document Name</th>
<th>File Name</th>
<th>Action</th>
</tr>
</thead>
<tbody id="tbody">
<?= isset($client_kyc_single_table) ? $client_kyc_single_table : "" ?>
</tbody>
</table>
</div> <!-- end table-responsive-->
</div>
</div> <!-- end card -->
</div> <!-- end col -->
</div>
<!-- end -->
<script>
var kycPrimaryKey = $('#client_id_kyc').val();
function handleDocumentSelectChange(selectElement) {
var $row = $(selectElement).closest('.form-row');
var $otherDocsGroup = $row.find('.other-docs-name-group');
var $otherDocsInput = $row.find('.other-docs-name-input');
var $fileInputGroup = $row.find('.file-input-group');
if (selectElement.value === 'other') {
$otherDocsGroup.removeClass('d-none').show();
$otherDocsInput.prop('required', true);
$fileInputGroup.removeClass('col-md-3 col-md-4').addClass('col-md-3');
} else {
$otherDocsGroup.addClass('d-none').hide();
$otherDocsInput.prop('required', false).val('');
$fileInputGroup.removeClass('col-md-3 col-md-4').addClass('col-md-4');
}
}
// Attach the change handler to all dropdowns
$(document).on('change', '.document-select', function() {
handleDocumentSelectChange(this);
});
$(document).ready(function(){
$("#kyc_form_add")[0].reset();
kycPrimaryKey = $('#kyc_PrimaryKey').val();
$('.document-select').each(function() {
handleDocumentSelectChange(this);
});
})
// ADD BUTTON---
$('#kyc_form_add').on('submit', function (e) {
e.preventDefault();
var docSelect = $('#docs_type_id');
var fileInput = $('#kyc_docs_file');
if (docSelect.val() === '' || fileInput[0].files.length === 0) {
toastr.error("Please select a document name and browser a file.");
return;
}
addKycDoc();
});
// ADD Functionality ---
function addKycDoc() {
var form = document.getElementById('kyc_form_add');
var formData = new FormData(form);
var docTypeVal = $('#docs_type_id').val();
// ✅ Correct handling
if (docTypeVal === 'other') {
formData.set('kyc_doc_type_id', '');
formData.set('other_docs_name', $('.other-docs-name-input').val());
} else {
formData.set('kyc_doc_type_id', docTypeVal);
formData.set('other_docs_name', '');
}
formData.set('client_id', $('#client_id_kyc').val());
formData.set('is_active', 1);
var fileInput = $('#kyc_docs_file')[0].files[0];
if (fileInput) {
formData.set('file_name', fileInput);
}
$('.loader').fadeIn();
$.ajax({
url: '<?= base_url("client/kyc/create_2"); ?>',
type: 'POST',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success: function (res) {
$('.loader').fadeOut();
if (res.status) {
$('#other_docs').html(res.data);
toastr.success('Document added successfully');
$('.other-docs-name-group').addClass('d-none');
$('.other-docs-name-input').val('');
$('#docs_type_id').empty();
$('#docs_type_id').append(res.dropdown);
$('#tbody').empty();
$('#tbody').append(res.html);
$('#kyc_form_add')[0].reset();
} else {
toastr.warning('Failed to add document');
}
},
error: function () {
$('.loader').fadeOut();
toastr.error('Upload error');
}
});
}
$(document).ready(function() {
// EDIT BUTTON---
$(document).on('click', '.btn-edit-kyc', function() {
let id = $(this).data('id');
$(`#data_row_${id}`).addClass('d-none');
$(`#edit_row_${id}`).removeClass('d-none');
});
// CANCEL BUTTON IN EDIT ---
$(document).on('click', '.btn-cancel-kyc', function() {
let id = $(this).data('id');
$(`#edit_row_${id}`).addClass('d-none');
$(`#data_row_${id}`).removeClass('d-none');
$(`#kyc_form_${id}`)[0].reset();
});
// UPDATE CLICK HANDLER ---
$(document).on('click', '.btn-update-kyc', function() {
let id = $(this).data('id');
var client_id = $(this).data('client_id');
var formElement = $(this).closest('form')[0]; // Fails if button isn't inside the form
updateKycDocWithForm(formElement, client_id);
});
});
// UPDATE Functionality ---
function updateKycDocWithForm(formElement, client_id) {
var formData = new FormData(formElement); // Use the element directly
formData.set('client_id', client_id);
let id = formData.get('id');
let fileInput = $(`#kyc_docs_file_${id}`)[0].files[0];
if (!fileInput) { console.log("file input not available here"); }
$('.loader').fadeIn();
$.ajax({
url: '<?= base_url("client/kyc/edit_2"); ?>',
type: 'POST',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success: function (res) {
$('.loader').fadeOut();
if (res.status) {
$(`#edit_row_${id}`).addClass('d-none');
$(`#data_row_${id}`).removeClass('d-none');
$('#docs_type_id').empty();
$('#docs_type_id').append(res.dropdown);
$('#tbody').empty();
$('#tbody').append(res.html);
toastr.success('Document updated successfully');
} else {
toastr.warning('Update failed: ' + (res.message || 'Server did not return a status message'));
}
},
error: function (xhr, status, error) {
$('.loader').fadeOut();
toastr.error('Server error: Check server logs for details.');
}
});
}
// SOFT DELETE CLICK HANDLER ---
$(document).on('click', '.btn-delete-kyc', function () {
var kyc_id = $(this).attr('data-id');
var client_id = $(this).attr('data-client_id');
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, delete it!"
}).then((result) => {
if (result.isConfirmed) {
deleteKycDoc(kyc_id, client_id);
}
});
});
$(document).on('click', '.btn-download-kyc', function (e) {
e.preventDefault(); // Prevent default link behavior
// var kyc_id = $(this).attr('data-id');
// var client_id = $(this).attr('data-client_id');
var file = $(this).attr('data-file');
if (file) {
window.location.href = "<?= base_url('client/kyc/download_2') ?>/" + file;
} else {
toastr.warning('File is missing.');
}
});
// SOFT DELETE Functionality ---
function deleteKycDoc(kyc_id,client_id) {
var formData = new FormData();
formData.append('is_active', 0);
formData.append('id', kyc_id);
formData.append('client_id', client_id);
$('.loader').fadeIn();
$.ajax({
url: '<?= base_url("client/kyc/delete_2"); ?>',
type: 'POST',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success: function (res) {
$('.loader').fadeOut();
if (res.status) {
$('#docs_type_id').empty();
$('#docs_type_id').append(res.dropdown);
$('#tbody').empty();
$('#tbody').append(res.html);
toastr.success('Document deleted successfully');
} else {
toastr.warning('Delete failed: ' + (res.message || 'Server error.'));
}
},
error: function () {
$('.loader').fadeOut();
toastr.error('Server error!');
}
});
}
// Centralized Validation Function
function validateFile(fileInput) {
var file = fileInput.files[0];
if (!file) {
return true; // No file selected, considered valid or neutral
}
var type = file.type;
// Check if the type is PDF or starts with 'image/'
if (type === 'application/pdf' || type.startsWith('image/')) {
return true; // Valid
}
// Invalid format
toastr.warning('Invalid file format. Only PDF or Image files are allowed.', 'WARNING!');
$(fileInput).val(''); // clear file
return false; // Invalid
}
// Attach the centralized handler to BOTH classes
$(document).on('change', '.file-input, .edit_file-input', function () {
validateFile(this);
});
</script>