"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-form"]');
            if($form.length===0){
                console.log("Error:","You must set form name is \"admin-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.trigger('submit');

        };
        plugin.init = function () {
            plugin.settings = $.extend({}, defaults, options);
            $('body').addClass('tour-operator-pro-view-admin');
            $element.find('.btn-tour-operator-pro-submit').on('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-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);
                }




            });

        };
        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"]').trigger('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="${wpbookingpro_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(wpbookingpro_root_url_plugin + src);
        }
    });
    $.getMultiLess(less_arr, wpbookingpro_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(wpbookingpro_root_url_plugin+src);
            }

        });
        $.getMultiScripts(script_arr, wpbookingpro_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: ""
            });
        });
    });
}


