|
${response.data.client_id}
|
${businessName}
|
${contactName}
|
${clientData.email || ''}
|
${clientData.phone || ''}
|
${clientData.username || ''}
|
${roleLabel}
|
|
`;
// Remove "No clients found" placeholder row if it exists.
// The colspan must match what's emitted by the PHP empty
// state and the JS delete handler (currently 8).
$(".client-list-table tbody tr td[colspan='8']").closest("tr").remove();
// Add new row at the top of the table (since we order by ID DESC)
$(".client-list-table tbody").prepend(newRow);
// Update total clients count
var currentCount = parseInt($("h3:contains('Total Clients')").next().text()) || 0;
$("h3:contains('Total Clients')").next().text(currentCount + 1);
// Show server's success message. The request sends
// suppress_global_toast=true (so the global ajaxSuccess
// handler in easy-invoice-toast.js doesn't auto-render a
// duplicate toast); we surface the message manually here.
if (typeof EasyInvoiceToast !== 'undefined' && response.data.message) {
EasyInvoiceToast.success(response.data.message);
}
} else {
if (typeof EasyInvoiceToast !== 'undefined') {
EasyInvoiceToast.error((response.data && response.data.message) || "Error adding client");
}
}
},
error: function(xhr, status, error) {
submitBtn.prop("disabled", false).html(originalText);
if (typeof EasyInvoiceToast !== 'undefined') {
EasyInvoiceToast.error("Error connecting to server. Please try again.");
} else if (typeof showToast === 'function') {
showToast("Error connecting to server. Please try again.", "error");
} else {
console.error("Error connecting to server. Please try again.");
}
},
timeout: function() {
submitBtn.prop("disabled", false).html(originalText);
if (typeof EasyInvoiceToast !== 'undefined') {
EasyInvoiceToast.error("Request timed out. Please try again.");
} else if (typeof showToast === 'function') {
showToast("Request timed out. Please try again.", "error");
} else {
console.error("Request timed out. Please try again.");
}
}
});
});
}
// Setup password field functionality
function setupPasswordFields() {
// Show/hide password functionality for add client modal
$(document).on('click', '#show-password', function() {
const passwordInput = $('#client-password');
const icon = $(this).find('i');
if (passwordInput.attr('type') === 'password') {
passwordInput.attr('type', 'text');
icon.removeClass('fa-eye').addClass('fa-eye-slash');
} else {
passwordInput.attr('type', 'password');
icon.removeClass('fa-eye-slash').addClass('fa-eye');
}
});
// Show/hide password functionality for edit client page
$(document).on('click', '#edit-show-password', function() {
const passwordInput = $('#edit-client-password');
const icon = $(this).find('i');
if (passwordInput.attr('type') === 'password') {
passwordInput.attr('type', 'text');
icon.removeClass('fa-eye').addClass('fa-eye-slash');
} else {
passwordInput.attr('type', 'password');
icon.removeClass('fa-eye-slash').addClass('fa-eye');
}
});
// Generate password functionality for add client modal
$(document).on('click', '#add-generate-password, #edit-generate-password', function() {
const button = $(this);
const isEditMode = button.attr('id') === 'edit-generate-password';
const passwordInput = isEditMode ? $('#edit-client-password') : $('#add-client-password');
const showPasswordBtn = isEditMode ? $('#edit-show-password') : $('#add-show-password');
button.prop('disabled', true);
$.ajax({
url: easyInvoice.ajaxUrl,
type: 'POST',
data: {
action: 'easy_invoice_generate_password',
nonce: easyInvoice.nonce,
suppress_global_toast: true // Prevent global success toast
},
success: function(response) {
if (response.success) {
passwordInput.val(response.data.password);
passwordInput.attr('type', 'text');
showPasswordBtn.find('i').removeClass('fa-eye').addClass('fa-eye-slash');
} else {
}
},
error: function() {
if (typeof EasyInvoiceToast !== 'undefined') {
EasyInvoiceToast.error('Error connecting to server');
} else if (typeof showToast === 'function') {
showToast('Error connecting to server', 'error');
} else {
console.error('Error connecting to server');
}
},
complete: function() {
button.prop('disabled', false);
}
});
});
}
// Setup client edit functionality
function setupClientEdit() {
// Disable HTML5 validation for password field in edit mode
if ($("#edit-client-form").length) {
$("#edit-client-password").removeAttr("required");
}
// Handle client edit form submission
$(document).on("submit", "#edit-client-form", function(e) {
e.preventDefault();
let isSubmitting = false;
if (isSubmitting) return;
isSubmitting = true;
const clientData = {
client_id: $("#edit-client-id").val(),
business_client_name: $("#edit-client-business-name").val(),
email: $("#edit-client-email").val(),
username: $("#edit-client-username").val(),
password: $("#edit-client-password").val(),
address: $("#edit-client-address").val(),
phone: $("#edit-client-phone").val(),
first_name: $("#edit-client-first-name").val(),
last_name: $("#edit-client-last-name").val(),
website: $("#edit-client-website").val(),
action: "easy_invoice_update_client",
nonce: easyInvoice.nonce
};
const submitBtn = $("#edit-save-client-btn");
const originalText = submitBtn.text();
submitBtn.prop("disabled", true).html("