completed clients and MNO module
This commit is contained in:
@@ -249,5 +249,134 @@ document.addEventListener("DOMContentLoaded", function() {
|
||||
</div>
|
||||
`);
|
||||
});
|
||||
|
||||
// 1. Populate Edit Client Modal when opened
|
||||
$(document).on('click', '[data-bs-target="#editClientModal"]', function() {
|
||||
let clientId = $(this).data('client-id');
|
||||
let $form = $('#editClientForm');
|
||||
|
||||
$form.attr('action', base_url + '/clients/' + clientId);
|
||||
|
||||
// Fetch Services first, then Client data
|
||||
$.ajax({
|
||||
url: base_url + '/api/services',
|
||||
type: 'GET',
|
||||
success: function(services) {
|
||||
let $servicesSelect = $('#edit_services');
|
||||
$servicesSelect.empty();
|
||||
services.forEach(function(s) {
|
||||
$servicesSelect.append(new Option(s.name, s.id));
|
||||
});
|
||||
|
||||
// Now fetch client details
|
||||
$.ajax({
|
||||
url: base_url + '/clients/' + clientId + '/json',
|
||||
type: 'GET',
|
||||
success: function(client) {
|
||||
$('#edit_name').val(client.name);
|
||||
$('#edit_email').val(client.email);
|
||||
$('#edit_phone').val(client.phone);
|
||||
$('#edit_contact_person').val(client.contact_person);
|
||||
$('#edit_company_type').val(client.company_type);
|
||||
$('#edit_contract_type').val(client.contract_type);
|
||||
$('#edit_industry').val(client.industry);
|
||||
$('#edit_status').val(client.status);
|
||||
$('#edit_currency').val(client.currency);
|
||||
$('#edit_country').val(client.country);
|
||||
|
||||
// Helper function to set values for Select2 multi-select tags fields
|
||||
function setSelect2Values(selector, values) {
|
||||
let $el = $(selector);
|
||||
$el.val(null).trigger('change');
|
||||
if (values && Array.isArray(values)) {
|
||||
values.forEach(function(val) {
|
||||
if ($el.find("option[value='" + val + "']").length === 0) {
|
||||
$el.append(new Option(val, val, true, true));
|
||||
}
|
||||
});
|
||||
$el.val(values).trigger('change');
|
||||
}
|
||||
}
|
||||
|
||||
setSelect2Values('#edit_services', client.services);
|
||||
setSelect2Values('#edit_message_types', client.message_types);
|
||||
setSelect2Values('#edit_connections', client.connections);
|
||||
setSelect2Values('#edit_support_phones', client.support_phones);
|
||||
setSelect2Values('#edit_support_emails', client.support_emails);
|
||||
setSelect2Values('#edit_rate_emails', client.rate_emails);
|
||||
setSelect2Values('#edit_support_skype', client.support_skype);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Initialize Select2 with tags option enabled for all multi-select fields inside edit modal
|
||||
$('#editClientModal').on('shown.bs.modal', function () {
|
||||
$('#editClientModal .select2-tags').each(function() {
|
||||
if (!$(this).hasClass("select2-hidden-accessible")) {
|
||||
$(this).select2({
|
||||
theme: 'bootstrap-5',
|
||||
dropdownParent: $('#editClientModal'),
|
||||
tags: true, // Allows typing custom entries for emails, phones, etc.
|
||||
tokenSeparators: [',', ' '],
|
||||
placeholder: 'Select or type and hit enter...'
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Clean up Select2 when modal closes
|
||||
$('#editClientModal').on('hidden.bs.modal', function () {
|
||||
$('#editClientModal .select2-tags').val(null).trigger('change');
|
||||
});
|
||||
|
||||
// Submit Edit Client Form via AJAX
|
||||
$(document).on('submit', '#editClientForm', function(e) {
|
||||
e.preventDefault(); // Hard stop on native form post
|
||||
|
||||
let $form = $(this);
|
||||
let $submitBtn = $form.find('button[type="submit"]');
|
||||
let originalBtnText = $submitBtn.text();
|
||||
|
||||
$submitBtn.prop('disabled', true).text('Updating...');
|
||||
|
||||
$.ajax({
|
||||
url: $form.attr('action'),
|
||||
type: 'POST', // Spoofed as PUT via hidden input
|
||||
data: $form.serialize(),
|
||||
headers: {
|
||||
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
|
||||
},
|
||||
success: function(response) {
|
||||
$('#editClientModal').modal('hide');
|
||||
|
||||
Swal.fire({
|
||||
icon: 'success',
|
||||
title: 'Updated!',
|
||||
text: response.message || 'Client updated successfully.',
|
||||
confirmButtonColor: '#0d6efd'
|
||||
}).then(() => {
|
||||
location.reload();
|
||||
});
|
||||
},
|
||||
error: function(xhr) {
|
||||
let errors = xhr.responseJSON?.errors;
|
||||
let errorMsg = 'Something went wrong. Please check your inputs.';
|
||||
if (errors) {
|
||||
errorMsg = Object.values(errors).flat().join('\n');
|
||||
}
|
||||
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'Validation Error',
|
||||
text: errorMsg,
|
||||
confirmButtonColor: '#dc3545'
|
||||
});
|
||||
|
||||
$submitBtn.prop('disabled', false).text(originalBtnText);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user