<script>
    <?php
        $currentUrl = \Illuminate\Support\Facades\Request::path();
        $segments = explode('/', $currentUrl);
        // Get the last segment that's not empty and not a number (to handle /dashboard/sliders/1 cases)
$segment = '';
for ($i = count($segments) - 1; $i >= 0; $i--) {
    if (!empty($segments[$i]) && !is_numeric($segments[$i]) && $segments[$i] !== 'dashboard') {
                $segment = $segments[$i];
                break;
            }
        }
        $url = url("dashboard/$segment");
    ?>

    // Define base delete URL for current section
    var url = <?php echo json_encode($url, 15, 512) ?>;

    console.log('Delete button component loaded');
    console.log('Current URL:', <?php echo json_encode($currentUrl, 15, 512) ?>);
    console.log('Segment:', <?php echo json_encode($segment, 15, 512) ?>);
    console.log('Delete URL:', url);

    $(document).ready(function() {
        if (typeof $ === 'undefined') {
            console.error('jQuery is not loaded!');
            return;
        }

        if ($('#btn_delete').length === 0) {
            console.error('Delete button not found!');
            return;
        }

        $('#btn_delete').on('click', function(e) {
            e.preventDefault();

            var selectedCheckboxes = $(".check-inputs:checked");
            var selectedIds = [];

            selectedCheckboxes.each(function() {
                selectedIds.push($(this).val());
            });

            if (selectedIds.length === 0) {
                Swal.fire({
                    title: "<?php echo e(__('messages.no_select')); ?>",
                    text: "<?php echo e(__('messages.please select at least one')); ?>",
                    icon: "warning",
                    confirmButtonColor: "#5156be"
                });
                return;
            }

            Swal.fire({
                title: "<?php echo e(__('messages.are you sure')); ?>",
                text: "<?php echo e(__('messages.remove checked values')); ?>",
                icon: "warning",
                showCancelButton: true,
                confirmButtonColor: "#3085d6",
                cancelButtonColor: "#d33",
                confirmButtonText: "<?php echo e(__('messages.yes, delete it')); ?>!"
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        }
                    });

                    // For bulk delete, use the bulk delete route
                    var deleteUrl = selectedIds.length === 1 ? url + '/' + selectedIds[0] :
                        url + '/bulk';

                    $.ajax({
                        url: deleteUrl,
                        type: 'DELETE',
                        data: {
                            selectedIds: selectedIds
                        },
                        beforeSend: function() {
                            Swal.fire({
                                title: "<?php echo e(__('messages.deleting')); ?>",
                                text: "<?php echo e(__('messages.please_wait')); ?>",
                                allowOutsideClick: false,
                                allowEscapeKey: false,
                                showConfirmButton: false,
                                didOpen: () => {
                                    Swal.showLoading();
                                }
                            });
                        },
                        success: function(response) {
                            if (response.success) {
                                Swal.fire({
                                    icon: "success",
                                    title: "<?php echo e(__('messages.deleted')); ?>",
                                    text: response.message,
                                    showConfirmButton: false,
                                    timer: 1500
                                });

                                $(document).trigger('recordDeleted');

                                setTimeout(function() {
                                    location.reload();
                                }, 1500);
                            } else {
                                Swal.fire({
                                    icon: "error",
                                    title: "<?php echo e(__('messages.error')); ?>",
                                    text: response.message ||
                                        "<?php echo e(__('messages.an_error_occurred')); ?>"
                                });
                            }
                        },
                        error: function(xhr) {
                            var message = xhr.responseJSON?.message ||
                                "<?php echo e(__('messages.an_error_occurred')); ?>";
                            Swal.fire({
                                icon: "error",
                                title: "<?php echo e(__('messages.error')); ?>",
                                text: message
                            });
                        }
                    });
                }
            });
        });
    });
</script>
<?php /**PATH /home/egypetoo/public_html/resources/views/components/dashboard/partials/delete-btn.blade.php ENDPATH**/ ?>