| 1 |
jQuery(document).ready(function($) { |
| 2 |
// Delete client functionality |
| 3 |
$(document).on("click", ".delete-client", function(e) { |
| 4 |
e.preventDefault(); |
| 5 |
|
| 6 |
const $button = $(this); |
| 7 |
const clientId = $button.data("id"); |
| 8 |
const clientEmail = $button.closest("tr").find("td:nth-child(2) div").text().trim(); // Get email from second column |
| 9 |
|
| 10 |
// Always show our custom choice dialog (ignore existing confirmation systems) |
| 11 |
// Create custom confirmation dialog with choice |
| 12 |
const choiceDialog = ` |
| 13 |
<div class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full z-50"> |
| 14 |
<div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white"> |
| 15 |
<div class="mt-3 text-center"> |
| 16 |
<div class="mx-auto flex items-center justify-center h-12 w-12 rounded-full bg-red-100"> |
| 17 |
<svg class="h-6 w-6 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 18 |
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"></path> |
| 19 |
</svg> |
| 20 |
</div> |
| 21 |
<h3 class="text-lg leading-6 font-medium text-gray-900 mt-4">Delete Client</h3> |
| 22 |
<div class="mt-2 px-7"> |
| 23 |
<p class="text-sm text-gray-500"> |
| 24 |
Are you sure you want to delete client "<strong>${clientEmail}</strong>"? |
| 25 |
</p> |
| 26 |
<div class="mt-4 p-3 bg-yellow-50 border border-yellow-200 rounded-md"> |
| 27 |
<p class="text-sm text-yellow-800"> |
| 28 |
<strong>Important:</strong> This client may have associated invoices, quotes, and payments. |
| 29 |
</p> |
| 30 |
</div> |
| 31 |
</div> |
| 32 |
</div> |
| 33 |
<div class="mt-4 flex justify-center space-x-3"> |
| 34 |
<button type="button" class="delete-client-only px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500"> |
| 35 |
Delete Client Only |
| 36 |
</button> |
| 37 |
<button type="button" class="delete-client-and-docs px-4 py-2 bg-red-600 text-white text-sm font-medium rounded-md hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500"> |
| 38 |
Delete All |
| 39 |
</button> |
| 40 |
<button type="button" class="cancel-delete px-4 py-2 bg-gray-300 text-gray-700 text-sm font-medium rounded-md hover:bg-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-500"> |
| 41 |
Cancel |
| 42 |
</button> |
| 43 |
</div> |
| 44 |
</div> |
| 45 |
</div> |
| 46 |
`; |
| 47 |
|
| 48 |
// Remove any existing confirmation dialogs first |
| 49 |
$('body').find('.fixed.inset-0').remove(); |
| 50 |
|
| 51 |
// Add our dialog to page |
| 52 |
$('body').append(choiceDialog); |
| 53 |
|
| 54 |
// Handle delete client only (preserve documents) |
| 55 |
$('.delete-client-only').on('click', function() { |
| 56 |
$('body').find('.fixed.inset-0').remove(); |
| 57 |
deleteClient(clientId, false); |
| 58 |
}); |
| 59 |
|
| 60 |
// Handle delete client and all documents |
| 61 |
$('.delete-client-and-docs').on('click', function() { |
| 62 |
$('body').find('.fixed.inset-0').remove(); |
| 63 |
deleteClient(clientId, true); |
| 64 |
}); |
| 65 |
|
| 66 |
// Handle cancel |
| 67 |
$('.cancel-delete').on('click', function() { |
| 68 |
$('body').find('.fixed.inset-0').remove(); |
| 69 |
}); |
| 70 |
}); |
| 71 |
|
| 72 |
// Function to handle client deletion |
| 73 |
function deleteClient(clientId, deleteAssociatedDocuments) { |
| 74 |
const $button = $('.delete-client[data-id="' + clientId + '"]'); |
| 75 |
const clientName = $button.closest("tr").find("td:first-child div").text().trim(); |
| 76 |
|
| 77 |
// Disable the button to prevent double-clicks |
| 78 |
$button.prop('disabled', true); |
| 79 |
|
| 80 |
$.ajax({ |
| 81 |
url: easyInvoice.ajaxUrl, |
| 82 |
type: "POST", |
| 83 |
data: { |
| 84 |
action: "easy_invoice_delete_client", |
| 85 |
client_id: clientId, |
| 86 |
delete_associated_documents: deleteAssociatedDocuments, |
| 87 |
nonce: easyInvoice.nonce |
| 88 |
}, |
| 89 |
success: function(response) { |
| 90 |
if (response.success) { |
| 91 |
const message = deleteAssociatedDocuments ? |
| 92 |
'Client and all associated documents deleted successfully' : |
| 93 |
'Client deleted successfully. Documents preserved.'; |
| 94 |
|
| 95 |
if (typeof EasyInvoiceToast !== 'undefined') { |
| 96 |
EasyInvoiceToast.show('success', response.data.message || message); |
| 97 |
} |
| 98 |
|
| 99 |
// Remove the row |
| 100 |
const $row = $button.closest("tr"); |
| 101 |
$row.fadeOut(400, function() { |
| 102 |
$row.remove(); |
| 103 |
|
| 104 |
// Update total clients count |
| 105 |
const $countElement = $("h3:contains('Total Clients')").next(); |
| 106 |
const currentCount = parseInt($countElement.text()) || 0; |
| 107 |
$countElement.text(Math.max(0, currentCount - 1)); |
| 108 |
|
| 109 |
// Check if table is empty and show "No clients found" |
| 110 |
if ($("tbody tr").length === 0) { |
| 111 |
$("tbody").html('<tr><td colspan="6" class="px-6 py-4 whitespace-nowrap text-center text-sm text-gray-500">No clients found</td></tr>'); |
| 112 |
} |
| 113 |
}); |
| 114 |
|
| 115 |
// Reload the page after a short delay to ensure everything is updated |
| 116 |
setTimeout(function() { |
| 117 |
window.location.reload(); |
| 118 |
}, 1000); |
| 119 |
} else { |
| 120 |
const errorMessage = response.data.message || 'Error deleting client'; |
| 121 |
if (typeof EasyInvoiceToast !== 'undefined') { |
| 122 |
EasyInvoiceToast.show('error', errorMessage); |
| 123 |
} |
| 124 |
$button.prop('disabled', false); |
| 125 |
} |
| 126 |
}, |
| 127 |
error: function(xhr) { |
| 128 |
let errorMessage = 'Error connecting to server'; |
| 129 |
if (xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) { |
| 130 |
errorMessage = xhr.responseJSON.data.message; |
| 131 |
} |
| 132 |
if (typeof EasyInvoiceToast !== 'undefined') { |
| 133 |
EasyInvoiceToast.show('error', errorMessage); |
| 134 |
} |
| 135 |
$button.prop('disabled', false); |
| 136 |
} |
| 137 |
}); |
| 138 |
} |
| 139 |
}); |
| 140 |
|