jQuery(document).ready(function($) {
    $('#set-featured-image-form').on('submit', function(e) {
        e.preventDefault();
        
        var imageUrl = $('#image_url').val();
        var postId = $('input[name="post_id"]').val();
        var nonce = $('input[name="nonce"]').val();
        
        $.ajax({
            url: ajax_object.ajax_url,
            type: 'POST',
            data: {
                action: 'set_featured_image_from_url',
                image_url: imageUrl,
                post_id: postId,
                nonce: nonce
            },
            success: function(response) {
                if (response.success) {
                    $('#response').html('<p>Featured image set successfully!</p>');
                } else {
                    $('#response').html('<p>Error: ' + response.data.message + '</p>');
                }
            },
            error: function() {
                $('#response').html('<p>An error occurred. Please try again.</p>');
            }
        });
    });
});