"use strict";
(function ($) {
    $.block_trending_destinations_hotel = function (element, options) {
        let defaults = {
            row_state_template: ""
        };
        let plugin = this;
        plugin.settings = {}
        let $element = $(element);
        $element.find('.heart').on('click',function(e){
            let $hotel_item = $(this).closest('.wishlist-item');
            let hotel_id = $hotel_item.data('hotel_id');
            let user_id = $hotel_item.data('user_id');
            let $wishlist_icon = $(this);
            if(user_id==""){
                if ( $('.alert-warning').length == 0){
                    $("body").append('<div class="alert alert-warning" role="alert" style="display: inline-block;position: fixed;right: 60px;top: 30px;z-index:999;" >Please login to use the feature!<button type="button" class="close" data-dismiss="alert" aria-label="Close" style="margin-left: 30px"><span aria-hidden="true">&times;</span></button></div>');
                    setTimeout(function(){
                        $('.alert-warning').remove();
                    },1500)
                }
                return;
            }
            let data = {
                task: "wishlist.change_wishlist",
                state: 1,
                hotel_id: hotel_id,
                user_id: user_id,
            };
            $.ajax({
                type: "POST",
                dataType: "json",
                url: wpbookinghotel_root_url + wpbookinghotel_api_task,
                data: data,
                beforeSend: function () {
                    $wishlist_icon.toggleClass("active");
                    $wishlist_icon.find('i').removeClass("fa-heart").addClass("fa-spinner fa-spin");
                },
                error: function (xhr) {
                    
                },
                complete: function () {
                    $wishlist_icon.find('i').addClass("fa-heart").removeClass("fa-spinner fa-spin");
                },
                success: function (response) {
                    response = JSON.parse(response);
                }
            });
        })
        plugin.init = function () {
            plugin.settings = $.extend({}, defaults, options);
            

        }
        plugin.example_function = function () {
        }
        plugin.init();
    }
    $.fn.block_trending_destinations_hotel = function (options) {
        return this.each(function () {
            if (undefined === $(this).data('block_trending_destinations_hotel')) {
                let plugin = new $.block_trending_destinations_hotel(this, options);
                $(this).data('block_trending_destinations_hotel',plugin);
            }
        });
    }
})(jQuery);
