(function ($) {
jQuery(document).ready( function( $ ) {
    $('body').on('click', '.ultimate-hotel-booking-media-btn.upload-btn', function (e) {
        var $this = $(this);
        frame = wp.media({
            title: "Select Image",
            button: {
                text: "Insert Image"
            },
            multiple: false
        });
        frame.on('select', function () {
            var attachment = frame.state().get('selection').first().toJSON();
            $(".ultimate-hotel-booking-media-url").val(attachment.url);
            $(".ultimate-hotel-booking-media-url-preview").val(attachment.url);
        });
        frame.open();
        return false;
    });
    $('body').on('click', '.ultimate-hotel-booking-media-btn.remove-btn', function (e) {
        e.preventDefault();
        var attachment_url = '';
        $(".ultimate-hotel-booking-media-url").val(attachment_url);
        $(".ultimate-hotel-booking-media-url-preview").val(attachment_url);
    });
});

// ultimate_hotel_booking Toolkit Installation
jQuery(document).on('click', '.ultimate-hotel-booking-toolkit-installation', function (e) {
    e.preventDefault();

    var current = $(this);
    var plugin_slug = current.attr("data-plugin-slug");

    current.addClass('installation-ongoing').text(ultimate_hotel_booking_script_params.installing);
    var data = {
        action: 'travelfic-toolkit_ajax_install_plugin',
        _ajax_nonce: ultimate_hotel_booking_script_params.ultimate_hotel_booking_theme_nonce,
        slug: plugin_slug,
    };
    // Installing Function
    jQuery.post(ultimate_hotel_booking_script_params.ajax_url, data, function (response) {
        if(response){
            current.text(ultimate_hotel_booking_script_params.activating);
            ultimate_hotel_booking_Activation(plugin_slug);
        }
    })

});

// Activation Functions
const ultimate_hotel_booking_Activation = (plugin_slug) => {
    $.ajax({
        type: 'post',
        url: ultimate_hotel_booking_script_params.ajax_url,
        data: {
            action: 'travelfic-toolkit_ajax_active_plugin',
            _ajax_nonce: ultimate_hotel_booking_script_params.ultimate_hotel_booking_theme_nonce,
            slug: plugin_slug,
        },
        success: function(response1) {
            if(response1.success){
                window.location.replace(ultimate_hotel_booking_script_params.redirect_url);
            }
        },
        error: function(error) {
            console.log(error);
        }
    });
}

})(jQuery);