"use strict";
(function ($) {
    $.field_change_symols = function (element, options) {
        var defaults = {
        }
        var plugin = this;

        plugin.settings = {}
        var $element = $(element),
            element = element;
        plugin.init = function () {
            plugin.settings = $.extend({}, defaults, options);
            let cookie = $element.find(".currency-selector option:selected");
            $element.find(".currency-symbol").text($.cookie('symbol'));
            $element.find('.currency-addon-fixed').text(cookie.text($.cookie('currency')));
            $element.find(".currency-selector").on("change", function () {
                plugin.updateSymbol();
            });
        };
        plugin.updateSymbol = function () {
            let cookie = document.cookie;
            cookie = $element.find(".currency-selector option:selected");
            $element.find(".currency-symbol").text(cookie.data("symbol"))
            $element.find('.currency-addon-fixed').text(cookie.text());
            $.cookie('currency', cookie.val());
            $.cookie('symbol', cookie.data("symbol"));
            let currency = $.cookie('currency');
            let symbol = $.cookie('symbol');
            plugin.change_Currency_Ajax(currency,symbol);
        }
        plugin.change_Currency_Ajax = function(currency,symbol){
            let data = {
                currency: currency,
                symbol: symbol,
                task: "hotels.changeCurrencyAjax"
            };
            $.ajax({
                cache: false,
                type: "POST",
                dataType: "json",
                url: wpbookinghotel_root_url + wpbookinghotel_api_task,
                data: data,
                beforeSend: function () {
                    $('body').loadingModal();
                },
                error: function (xhr) {
                    $.alert({
                        title: 'Error',
                        content: 'there are some error please contact admin !',
                    });
                },
                complete: function () {
                    location.reload(true);
                    $('body').loadingModal('destroy');
                },
                success: function (response) {
                    response = JSON.parse(response);
                    console.log(response)
                }
            });
        }
        plugin.example_function = function () {
        };
        plugin.init();
    }

    $.fn.field_change_symols = function (options) {

        return this.each(function () {

            if (undefined == $(this).data('field_change_symols')) {
                var plugin = new $.field_change_symols(this, options);
                $(this).data('field_change_symols', plugin);
            }
        });
    }
})(jQuery);

