"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.trigger('submit');
        };
        plugin.action_change_currency = 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"]');
            let $input_hidden_currency=$form.find('input[type="hidden"][name="currency"]');
            if($input_hidden_task.length===0 && $input_hidden_currency.length === 0){
                $input_hidden_task=$('<input type="hidden" name="task" value="">');
                $input_hidden_currency=$('<input type="hidden" name="currency" value="">');
                $input_hidden_task.appendTo($form);
                $input_hidden_currency.appendTo($form);
            }
            $input_hidden_task.val(task);
            $input_hidden_currency.val($(self).val());
            $form.trigger('submit');

        };
        plugin.init = function () {
            plugin.settings = $.extend({}, defaults, options);
            $('body').addClass('wpbooking-pro-view-admin');
            $element.find('.btn-wpbooking-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_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-appointment');
            $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');
            })
            $element.find('.view-list-package .event-item span.label-info').text(function (index,currenText){
                return currenText.substr(0,25)+'...';
            })

            $.fn.digits = function () {
                return this.each(function () {
                    $(this).text($(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") );
                })
            }
            $element.find('.view-db-appointments-dashboard .monthly .number-right').digits();

            $element.find('.view-db-appointments-dashboard .monthly .number-right').text(function (index,currenText){
                return currenText.substr(0,5)+'k';
            })

            $element.find('select.searchByCurrency').on('change', function () {
                plugin.action_change_currency(this);
            });

        };
        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: ""
            });
        });
    });
}


