(function ($) {
    'use strict';

    /**
     * Handle image removal by setting hidden flag and hiding preview
     */
    $(document).ready(function () {
        $('body').on('click', '.oddw-button-remove-picture', function (e) {
            // Stop all event propagation
            e.preventDefault();
            e.stopPropagation();
            e.stopImmediatePropagation();

            const $button = $(this);
            const imageType = $button.data('image-type') || 'driver';
            const previewId = $button.data('preview-id');
            const $preview = $(previewId);

            if (!$preview.length) {
                console.error('Preview element not found:', previewId);
                return false;
            }

            // Determine which hidden input to update
            const flagInputName = 'car' === imageType ? 'oddw_driver_car_image_remove' : 'oddw_driver_picture_remove';

            // Set the hidden flag to indicate removal
            $('input[name="' + flagInputName + '"]').val('1');

            // Hide the preview immediately
            $preview.fadeOut(300, function () {
                $(this).remove();
            });

            return false;
        });
    });
})(jQuery);

