function initBulkActions(deleteUrl) {
    const selectAllCheckbox = document.getElementById('select-all');
    const itemCheckboxes = document.querySelectorAll('.select-single');
    const deleteBulkBtn = document.getElementById('delete-bulk-btn');

    // Select all functionality
    selectAllCheckbox.addEventListener('click', function () {
        const isChecked = this.checked;
        itemCheckboxes.forEach(checkbox => {
            checkbox.checked = isChecked;
        });
        toggleDeleteButton();
    });

    // Individual checkbox click functionality
    itemCheckboxes.forEach(checkbox => {
        checkbox.addEventListener('click', function () {
            const allChecked = [...itemCheckboxes].every(checkbox => checkbox.checked);
            selectAllCheckbox.checked = allChecked;

            if (!this.checked) {
                selectAllCheckbox.checked = false;
            }

            toggleDeleteButton();
        });
    });

    // Show/hide the delete button based on checkbox state
    function toggleDeleteButton() {
        const isChecked = [...itemCheckboxes].some(checkbox => checkbox.checked);
        deleteBulkBtn.style.display = isChecked ? 'inline-block' : 'none';
    }

    // Get selected items for deletion
    function getSelectedItems() {
        const selectedItems = [];
        document.querySelectorAll('.select-single:checked').forEach(function (checkbox) {
            selectedItems.push(checkbox.value);
        });
        return selectedItems;
    }

    // Delete selected orders
    deleteBulkBtn.addEventListener('click', function () {
        const selectedItems = getSelectedItems();

        if (selectedItems.length === 0) {
            alert('Please select at least one item.');
            return;
        }

        if (!confirm('Are you sure you want to delete the selected items?')) {
            return;
        }

        axios.delete(deleteUrl, {
            data: {
                item_ids: selectedItems
            }
        })
            .then((response) => {
                if (response.data.success) {
                    location.reload();
                } else {
                    alert('Failed to delete items.');
                }
            })
            .catch((error) => {
                console.error(error);
                alert('An error occurred. Please try again.');
            });
    });

    toggleDeleteButton();
}
