jQuery(document).ready(function($) { // Delete client functionality $(document).on("click", ".delete-client", function(e) { e.preventDefault(); const $button = $(this); const clientId = $button.data("id"); const clientEmail = $button.closest("tr").find("td:nth-child(2) div").text().trim(); // Get email from second column // Always show our custom choice dialog (ignore existing confirmation systems) // Create custom confirmation dialog with choice const choiceDialog = `

Delete Client

Are you sure you want to delete client "${clientEmail}"?

Important: This client may have associated invoices, quotes, and payments.

`; // Remove any existing confirmation dialogs first $('body').find('.fixed.inset-0').remove(); // Add our dialog to page $('body').append(choiceDialog); // Handle delete client only (preserve documents) $('.delete-client-only').on('click', function() { $('body').find('.fixed.inset-0').remove(); deleteClient(clientId, false); }); // Handle delete client and all documents $('.delete-client-and-docs').on('click', function() { $('body').find('.fixed.inset-0').remove(); deleteClient(clientId, true); }); // Handle cancel $('.cancel-delete').on('click', function() { $('body').find('.fixed.inset-0').remove(); }); }); // Function to handle client deletion function deleteClient(clientId, deleteAssociatedDocuments) { const $button = $('.delete-client[data-id="' + clientId + '"]'); const clientName = $button.closest("tr").find("td:first-child div").text().trim(); // Disable the button to prevent double-clicks $button.prop('disabled', true); $.ajax({ url: easyInvoice.ajaxUrl, type: "POST", data: { action: "easy_invoice_delete_client", client_id: clientId, delete_associated_documents: deleteAssociatedDocuments, nonce: easyInvoice.nonce }, success: function(response) { if (response.success) { const message = deleteAssociatedDocuments ? 'Client and all associated documents deleted successfully' : 'Client deleted successfully. Documents preserved.'; if (typeof EasyInvoiceToast !== 'undefined') { EasyInvoiceToast.show('success', response.data.message || message); } // Remove the row const $row = $button.closest("tr"); $row.fadeOut(400, function() { $row.remove(); // Update total clients count const $countElement = $("h3:contains('Total Clients')").next(); const currentCount = parseInt($countElement.text()) || 0; $countElement.text(Math.max(0, currentCount - 1)); // Check if table is empty and show "No clients found" if ($("tbody tr").length === 0) { $("tbody").html('No clients found'); } }); // Reload the page after a short delay to ensure everything is updated setTimeout(function() { window.location.reload(); }, 1000); } else { const errorMessage = response.data.message || 'Error deleting client'; if (typeof EasyInvoiceToast !== 'undefined') { EasyInvoiceToast.show('error', errorMessage); } $button.prop('disabled', false); } }, error: function(xhr) { let errorMessage = 'Error connecting to server'; if (xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) { errorMessage = xhr.responseJSON.data.message; } if (typeof EasyInvoiceToast !== 'undefined') { EasyInvoiceToast.show('error', errorMessage); } $button.prop('disabled', false); } }); } });