// ============ SINGLE CLIENT APPROVE ============ $(document).on('click', '.open-approve-modal', function() { var clientId = $(this).data('id'); // Set modal for single mode $('.confirm-approve').data('id', clientId).data('mode', 'single'); $('#approve-modal-text').text('You are about to approve this client addition request. Once approved, the client will be added to the system and available for future transactions.'); $('.approve-client-request').modal('show'); }); // ============ SINGLE CLIENT REJECT ============ $(document).on('click', '.open-reject-modal', function() { var clientId = $(this).data('id'); var reason = $(this).data('reason'); // Set modal for single mode $('.confirm-reject').data('id', clientId).data('mode', 'single'); $('#reject_reason').val(reason || ''); $('#reject-modal-text').text('You are about to reject this client addition request. Please provide a reason for rejection so the requester can be notified.'); $('.reject-client-request').modal('show'); }); // ============ CONFIRM APPROVE (Handles both single and bulk) ============ $(document).on('click', '.confirm-approve', function() { const mode = $(this).data('mode'); const button = $(this); button.prop('disabled', true).text('Processing...'); if (mode === 'single') { // Single client approve var clientId = $(this).data('id'); if (!clientId) { alert('Client ID is missing'); button.prop('disabled', false).text('Approve Request'); return; } $.ajax({ url: "{{ route('backend.manage-clients.approve', ':id') }}".replace(':id', clientId), type: 'POST', data: { _token: '{{ csrf_token() }}' }, success: function(response) { if (response.status) { $('.approve-client-request').modal('hide'); // Update toast message $('#approve-toast-message').text('Client has been added successfully!'); // Show toast var toastEl = document.getElementById('ApproveClientRequest'); var toast = new bootstrap.Toast(toastEl); toast.show(); // Reload table setTimeout(function() { @if (isset($currentTable)) currentTable.ajax.reload(null, false); @else window.location.reload(); @endif }, 1000); } button.prop('disabled', false).text('Approve Request'); }, error: function(xhr) { alert('Error approving client: ' + (xhr.responseJSON?.message || 'Unknown error')); button.prop('disabled', false).text('Approve Request'); } }); } else if (mode === 'bulk') { // Bulk approve const selectedClients = getSelectedClients(); const clientIds = selectedClients.map(c => c.id); if (clientIds.length === 0) { alert('No clients selected'); button.prop('disabled', false).text('Approve Request'); return; } $.ajax({ url: "{{ route('backend.manage-clients.bulk-approve') }}", type: "POST", data: { _token: "{{ csrf_token() }}", client_ids: clientIds }, success: function(res) { if (res.status) { $('.approve-client-request').modal('hide'); // Update toast message $('#approve-toast-message').text(`${clientIds.length} client(s) have been approved successfully!`); // Show toast var toastEl = document.getElementById('ApproveClientRequest'); var toast = new bootstrap.Toast(toastEl); toast.show(); setTimeout(function() { currentTable.ajax.reload(null, false); $('.row-check').prop('checked', false); const checkAllEl = document.getElementById('cardtableCheckAll'); if (checkAllEl) { checkAllEl.checked = false; } const actionBtns = document.getElementById('bulk-action-btns'); if (actionBtns) { actionBtns.style.display = 'none'; } }, 1000); } button.prop('disabled', false).text('Approve Request'); }, error: function(xhr) { alert('Error approving clients: ' + (xhr.responseJSON?.message || 'Unknown error')); button.prop('disabled', false).text('Approve Request'); } }); } }); // ============ CONFIRM REJECT (Handles both single and bulk) ============ $(document).on('click', '.confirm-reject', function() { const mode = $(this).data('mode'); var reason = $('#reject_reason').val().trim(); const button = $(this); if (!reason) { alert('Please provide a reason for rejection'); return; } button.prop('disabled', true).text('Processing...'); if (mode === 'single') { // Single client reject var clientId = $(this).data('id'); if (!clientId) { alert('Client ID is missing'); button.prop('disabled', false).text('Reject Request'); return; } $.ajax({ url: "{{ route('backend.manage-clients.reject', ':id') }}".replace(':id', clientId), type: 'POST', data: { _token: '{{ csrf_token() }}', reason: reason }, success: function(response) { if (response.status) { $('.reject-client-request').modal('hide'); // Update toast message $('#reject-toast-message').text('Client request has been rejected successfully!'); // Show toast var toastEl = document.getElementById('RejectClientRequest'); var toast = new bootstrap.Toast(toastEl); toast.show(); // Clear the textarea $('#reject_reason').val(''); // Reload table setTimeout(function() { @if (isset($currentTable)) currentTable.ajax.reload(null, false); @else window.location.reload(); @endif }, 1000); } button.prop('disabled', false).text('Reject Request'); }, error: function(xhr) { alert('Error rejecting client: ' + (xhr.responseJSON?.message || 'Unknown error')); button.prop('disabled', false).text('Reject Request'); } }); } else if (mode === 'bulk') { // Bulk reject const selectedClients = getSelectedClients(); const clientIds = selectedClients.map(c => c.id); if (clientIds.length === 0) { alert('No clients selected'); button.prop('disabled', false).text('Reject Request'); return; } $.ajax({ url: "{{ route('backend.manage-clients.bulk-reject') }}", type: "POST", data: { _token: "{{ csrf_token() }}", client_ids: clientIds, reason: reason }, success: function(res) { if (res.status) { $('.reject-client-request').modal('hide'); $('#reject_reason').val(''); // Update toast message $('#reject-toast-message').text(`${clientIds.length} client request(s) have been rejected successfully!`); // Show toast var toastEl = document.getElementById('RejectClientRequest'); var toast = new bootstrap.Toast(toastEl); toast.show(); setTimeout(function() { currentTable.ajax.reload(null, false); $('.row-check').prop('checked', false); const checkAllEl = document.getElementById('cardtableCheckAll'); if (checkAllEl) { checkAllEl.checked = false; } const actionBtns = document.getElementById('bulk-action-btns'); if (actionBtns) { actionBtns.style.display = 'none'; } }, 1000); } button.prop('disabled', false).text('Reject Request'); }, error: function(xhr) { alert('Error rejecting clients: ' + (xhr.responseJSON?.message || 'Unknown error')); button.prop('disabled', false).text('Reject Request'); } }); } });