"use strict";
(function ($) {
    $.view_main_app = function (element, options) {
        let defaults = {
            row_state_template: ""
        };
        let plugin = this;
        plugin.settings = {}
        let $element = $(element);
        plugin.action = function (self) {
            let task=$(self).data('task');
            let $form=$element.find('form[name="admin_woopanel-form"]');
            if($form.length===0){
                console.log("Error:","You must set form name is \"admin_woopanel-form\"");
            }
            let $input_hidden_task=$form.find('input[type="hidden"][name="task"]');
            if($input_hidden_task.length===0){
                $input_hidden_task=$('<input type="hidden" name="task" value="">');
                $input_hidden_task.appendTo($form);
            }
            $input_hidden_task.val(task);
            $form.submit();
        };
        plugin.init = function () {
            plugin.settings = $.extend({}, defaults, options);
            $('body').addClass('wpbooking-hotel-view-admin');
            $element.find('.btn-wpbooking-hotel-submit').click(function (e) {
                let btn=this;
                let must_selected=$(this).data('must_selected');
                let class_checkbox=$(this).data('class_checkbox');
                let $form=$element.find('form[name="admin_woopanel-form"]');
                if(typeof must_selected!=="undefined" && must_selected===1){
                    if($form.find(`.${class_checkbox}:checked`).length===0){
                        $.alert({
                            title: 'Error',
                            content: 'please select item',
                        });
                        return false;
                    }
                }
                let msg_config=$(this).data('msg_config');
                if(typeof  msg_config!=="undefined" && msg_config!==""){
                    $.confirm({
                        title: 'Confirm',
                        content: msg_config,
                        buttons: {
                            confirm: function () {
                                plugin.action(btn);
                            },
                            cancel: function () {

                            }
                        }
                    });

                }else{
                    plugin.action(btn);
                }
            });
            $element.find('.m-grid__item').addClass('customize-for-hotel');
            $element.find('ul.state-tab li').on('click', function () {
                $element.find('ul.state-tab li').removeClass('active');
                $(this).addClass('active');
            })
            $element.find('ul.nav-tabs li').on('click',function () {
                $element.find('ul.nav-tabs li').removeClass('active')
                $(this).addClass('active');
            })
        };
        plugin.example_function = function () {


        }
        plugin.init();
    }
    $.fn.view_main_app = function (options) {
        return this.each(function () {
            if (undefined == $(this).data('view_main_app')) {
                let plugin = new $.view_main_app(this, options);
                $(this).data('view_main_app'
                    , plugin);
            }
        });
    }
})(jQuery);
//let $woo_booking_debug = jQuery('.wrapper-woo-booking-debug').woo_booking_debug({});
let $wooBooking = jQuery('body').view_main_app();
let $ = jQuery;
let WooBooking = {
    submitform: function (e) {
        $('form[name="adminForm"]').submit();
    }
};
jQuery.getMultiScripts = function (arr, path) {
    var _arr = $.map(arr, function (scr) {
        return $.getScript(scr);
    });

    _arr.push($.Deferred(function (deferred) {
        $(deferred.resolve);
    }));

    return $.when.apply($, _arr);
};
jQuery.getMultiLess = function (arr, path) {
    var _arr_less = $.map(arr, function (scr) {
        let current_less = document.createElement('link');
        current_less.setAttribute('rel', 'stylesheet/less');
        current_less.setAttribute('type', 'text/css');
        current_less.setAttribute('href', scr);
        document.head.appendChild(current_less);
    });
    _arr_less.push($.Deferred(function (deferred) {
        $(deferred.resolve);
    }));
    return $.when.apply($, _arr_less);
};
let list_js_installed = [];
function loadScripts(response) {
    let $ = jQuery;
    $('link[rel="stylesheet/less"]').remove();
    var styleSheets = response.styleSheets;
    $.each(styleSheets, function (src, value) {
        if (src.indexOf('http') >= 0) {
            $('head').append(`<link rel="stylesheet" href="${src}" type="text/css" />`);
        } else {
            $('head').append(`<link rel="stylesheet" href="${wpbookinghotel_root_url_plugin}${src}" type="text/css" />`);
        }


    });
    var less_arr = [];
    var lessStyleSheets = response.lessStyleSheets;
    $.each(lessStyleSheets, function (src, value) {
        if (src.indexOf('http') >= 0) {
            less_arr.push(src);
        } else {
            less_arr.push(wpbookinghotel_root_url_plugin + src);
        }
    });
    $.getMultiLess(less_arr, wpbookinghotel_root_url_plugin).done(function () {
        let script_arr = [];

        var scripts = response.scripts;
        $.each(scripts, function (src, value) {
            if( src.indexOf('http') >= 0){
                script_arr.push(src);
            }else{
                script_arr.push(wpbookinghotel_root_url_plugin+src);
            }

        });
        $.getMultiScripts(script_arr, wpbookinghotel_root_url_plugin).done(function () {
            let script = response.script;
            $('head').append(`<script  type="text/javascript">${script['text/javascript']}</script>`);
            $wooBooking.getDrawer().find('form').validate({
                ignore: ""
            });
        });
    });
}


