updated client views and routes

This commit is contained in:
Kwesi Banson Jnr
2026-07-23 21:16:22 +00:00
parent 7b29bb278c
commit 08ded57875
10 changed files with 1685 additions and 188 deletions

View File

@@ -3,8 +3,35 @@
@section('title', 'Click ERP - Client Management')
@push('styles')
<style>
.avatar-circle { width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 50%; font-weight: bold; font-size: 0.85rem; }
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<style>
/* Custom styling to make Select2 match Bootstrap 5 form controls */
.select2-container .select2-selection--multiple {
min-height: 38px;
border: 1px solid #dee2e6;
border-radius: 0.375rem;
}
.select2-container--default .select2-selection--multiple .select2-selection__choice {
background-color: #5c4df0;
border: none;
color: white;
border-radius: 4px;
padding: 2px 8px;
margin-top: 5px;
}
.select2-container--default .select2-selection--multiple .select2-selection__choice__remove {
color: white;
margin-right: 5px;
}
.select2-container--default .select2-selection--multiple .select2-selection__choice__remove:hover {
color: #ffcccc;
background: transparent;
}
/* Fix for validation red border */
.is-invalid + .select2-container .select2-selection {
border-color: #dc3545;
}
.avatar-circle { width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 50%; font-weight: bold; font-size: 0.85rem; }
</style>
@endpush
@@ -83,6 +110,7 @@
<th>Primary Contact</th>
<th>Services</th>
<th>Billing</th>
<th>Date Added</th>
<th>Status</th>
<th class="text-end">Actions</th>
</tr>
@@ -110,8 +138,14 @@
@endpush
@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
$(document).ready(function() {
$('#clientService').select2({
placeholder: "-- Select Services --",
allowClear: true,
dropdownParent: $('#createClientModal')
});
let searchTimer;
// Initialize the table on page load
@@ -134,18 +168,13 @@
let page = $(this).data('page');
if (page) fetchClients(page);
});
// ---------------------------------------------------------
// 1. CREATE BUTTON (Open Modal)
// ---------------------------------------------------------
$('#btnOpenClientModal').on('click', function(e) {
e.preventDefault();
// Replace '#createClientModal' with the actual ID of the modal in your clients.partials.create file
$('#createClientModal').modal('show');
});
// ---------------------------------------------------------
// 2. VIEW BUTTON (Event Delegation for AJAX buttons)
// ---------------------------------------------------------
$(document).on('click', '.btn-view-client', function(e) {
e.preventDefault();
let clientId = $(this).data('id');
@@ -158,9 +187,7 @@
// $('#viewClientModal').modal('show');
});
// ---------------------------------------------------------
// 3. EDIT BUTTON (Event Delegation for AJAX buttons)
// ---------------------------------------------------------
$(document).on('click', '.btn-edit-client', function(e) {
e.preventDefault();
let clientId = $(this).data('id');
@@ -173,6 +200,100 @@
// $('#editClientModal').modal('show');
});
// ---------------------------------------------------------
// AJAX FORM SUBMISSION (Create Client)
// ---------------------------------------------------------
$('#createClientForm').on('submit', function(e) {
e.preventDefault();
let $form = $(this);
let $submitBtn = $('#btnSubmitClient');
let originalText = $submitBtn.html();
let $alertBox = $('#clientModalAlert');
// 1. Show loading state & reset previous alerts/errors
$submitBtn.html('<span class="spinner-border spinner-border-sm me-2"></span>Saving...').prop('disabled', true);
$alertBox.html('');
$form.find('.is-invalid').removeClass('is-invalid');
$form.find('.invalid-feedback').remove();
$.ajax({
url: $form.attr('action'),
method: 'POST',
data: $form.serialize(),
success: function(response) {
if(response.success) {
// Show success message inside the modal
$alertBox.html(`
<div class="alert alert-success d-flex align-items-center" role="alert">
<i class="bi bi-check-circle-fill me-2"></i>
<div>${response.message || 'Client added successfully!'}</div>
</div>
`);
// Refresh the background datatable
fetchClients(1);
// Optional: Automatically close the modal after 2 seconds
setTimeout(() => {
$('#createClientModal').modal('hide');
$form[0].reset();
$alertBox.html('');
}, 2000);
}
},
error: function(xhr) {
// Handle Laravel Validation Errors (Status 422)
if (xhr.status === 422) {
let errors = xhr.responseJSON.errors;
// Show general warning alert at the top
$alertBox.html(`
<div class="alert alert-danger d-flex align-items-center" role="alert">
<i class="bi bi-exclamation-triangle-fill me-2"></i>
<div>Please fix the errors highlighted below.</div>
</div>
`);
// Highlight specific fields
$.each(errors, function(key, value) {
// Account for array names like services[]
let fieldName = key;
if(key === 'services') fieldName = 'services[]';
let $input = $form.find('[name="' + fieldName + '"]');
if ($input.length) {
$input.addClass('is-invalid');
// Append the specific error message right below the field
$input.parent().append('<div class="invalid-feedback d-block fw-medium">' + value[0] + '</div>');
}
});
} else {
// Handle standard 500 server errors
$alertBox.html(`
<div class="alert alert-danger d-flex align-items-center" role="alert">
<i class="bi bi-x-circle-fill me-2"></i>
<div>An unexpected server error occurred. Please try again.</div>
</div>
`);
}
},
complete: function() {
// Restore the button to its original state
$submitBtn.html(originalText).prop('disabled', false);
}
});
});
// Clear alerts and errors when the modal is closed manually
$('#createClientModal').on('hidden.bs.modal', function () {
$('#createClientForm')[0].reset();
$('#clientService').val(null).trigger('change');
$('#clientModalAlert').html('');
$('#createClientForm').find('.is-invalid').removeClass('is-invalid');
$('#createClientForm').find('.invalid-feedback').remove();
});
// AJAX Fetch Function
function fetchClients(page = 1) {
const search = $('#searchClient').val();
@@ -224,7 +345,7 @@
window.location.href = "{{ route('clients.export') }}?" + queryParams;
});
// Render Table Rows
function renderTable(clients) {
function renderTableOld(clients) {
let html = '';
if (clients.length === 0) {
@@ -275,6 +396,91 @@
${servicesHtml}
</td>
<td><span class="badge bg-info bg-opacity-10 text-info text-uppercase">${client.pay_mode || 'N/A'}</span></td>
<td><span class="fw-medium text-primary">${client.created_at}</span></td>
<td>${statusBadge}</td>
<td class="text-end">
<button class="btn btn-sm btn-light text-primary me-1 btn-view-client"
data-id="${client.id}">
<i class="bi bi-eye"></i>
</button>
<button class="btn btn-sm btn-light text-primary me-1 btn-edit-client"
data-id="${client.id}">
<i class="bi bi-pencil"></i>
</button>
</td>
</tr>
`;
});
$('#clientTableBody').html(html);
}
function renderTable(clients) {
let html = '';
if (clients.length === 0) {
$('#clientTableBody').html('<tr><td colspan="7" class="text-center text-secondary py-4">No clients found matching your criteria.</td></tr>');
return;
}
clients.forEach(client => {
// 1. Extract initials for the avatar
let initials = client.name.substring(0, 2).toUpperCase();
// 2. Handle Services
let servicesHtml = '<span class="text-secondary" style="font-size: 0.8rem;">N/A</span>';
if (client.services) {
try {
let servicesArray = JSON.parse(client.services);
if (Array.isArray(servicesArray) && servicesArray.length > 0) {
servicesHtml = servicesArray.map(service =>
`<span class="badge bg-secondary bg-opacity-10 text-secondary border me-1 mb-1" style="font-size: 0.7rem;">${service}</span>`
).join('');
}
} catch (e) {
console.error("Could not parse services for client: " + client.name);
}
}
// 3. Status Badge Logic
let statusBadge = client.status === 'Live' || client.status === 'active'
? '<span class="badge bg-success bg-opacity-10 text-success px-2 py-1"><i class="bi bi-check-circle me-1"></i>Active</span>'
: '<span class="badge bg-warning bg-opacity-10 text-warning px-2 py-1"><i class="bi bi-clock me-1"></i>' + client.status + '</span>';
// 4. Format the Date (Human Friendly)
let formattedDate = 'N/A';
if (client.created_at) {
const dateObj = new Date(client.created_at);
// Formats to: "23 Jul 2026"
formattedDate = dateObj.toLocaleDateString('en-GB', {
day: 'numeric',
month: 'short',
year: 'numeric'
});
}
html += `
<tr>
<td>
<div class="d-flex align-items-center">
<div class="avatar-circle bg-primary bg-opacity-10 text-primary me-3">${initials}</div>
<div>
<div class="fw-bold text-dark">${client.name}</div>
<div class="text-secondary" style="font-size: 0.8rem;"><i class="bi bi-geo-alt me-1"></i>${client.country || 'N/A'}</div>
</div>
</div>
</td>
<td>
<div class="fw-semibold">${client.contact_person || 'N/A'}</div>
<div class="text-secondary" style="font-size: 0.8rem;">${client.email || 'N/A'}</div>
</td>
<td style="max-width: 200px; white-space: normal;">
${servicesHtml}
</td>
<td><span class="badge bg-info bg-opacity-10 text-info text-uppercase">${client.pay_mode || 'N/A'}</span></td>
<!-- Updated Date Column -->
<td><span class="fw-medium text-primary">${formattedDate}</span></td>
<td>${statusBadge}</td>
<td class="text-end">
<button class="btn btn-sm btn-light text-primary me-1 btn-view-client"