/**
 * Base.Box 1.0
 *
 * Requiere
 *     Base 1.0
 *     Base.Box 1.0
 */
$(function() {
    $.extend(Base, {
        Box : {

            formAjax: function (formId, url, data, options) {

                Base.Message.clear();

                var _i18n = {
                };

                _i18n = $.extend(true, _i18n, Base.i18n.box);

                if (options != undefined && options._i18n !== undefined && options._i18n != null) {
                    _i18n = $.extend(true, _i18n, options._i18n);
                }

                var showLoading = (null != options.showLoading && options.showLoading != undefined) ? options.showLoading : true;
                var loadingMessage =  (null != options.loadingMessage && options.loadingMessage != undefined) ? options.loadingMessage : "Cargando...";

                var buttons = {
                    submit: {
                        label: "Aceptar",
                        className: 'btn btn-primary',
                        callback: function(e){

                            submitButton = e.currentTarget;
                            submitButton.setAttribute('data-kt-indicator', 'on');
                            submitButton.disabled = true;

                            Base.Message.clear();

                            var formSelector =  '#' + formId;
                            var formValidator = $(formSelector).QxForm().validatorInstance;

                            if(formValidator){
                                formValidator.validate().then(function (status){

                                    if (status == 'Valid'){

                                        submitButton.disabled = true;
                                        // if(showLoading) {
                                           //Base.Loading.show({message: loadingMessage})
                                        // }

                                        var _defaultAjaxOptions = {

                                            success: function (data, textStatus, jqXHR, form) {
                                                 // Base.Loading.hide();

                                                if(data.type == 'Success' || data.type == 'Warning' || data.type == 'Info') {

                                                    if(data.message != null & data.message != "") {
                                                        Base.Message.flash(data.message, data.cssClass);
                                                    }

                                                    Base.Box.clearAll();

                                                    if(options.mySuccessCallBack != undefined && options.mySuccessCallBack != null && $.isFunction(options.mySuccessCallBack)) {

                                                        return options.mySuccessCallBack(data);
                                                    }

                                                    return true;
                                                }
                                                else if(data.type == 'Error' || data.type == 'Exception'){

                                                    Base.Message.flash(data.message, data.cssClass);

                                                    // Show error message.
                                                    submitButton.disabled = false;
                                                    submitButton.removeAttribute('data-kt-indicator');

                                                    if(options.myErrorCallBack != undefined && options.myErrorCallBack != null && $.isFunction(options.myErrorCallBack)) {

                                                        return options.myErrorCallBack(data);
                                                    }

                                                    return false;
                                                }
                                                else{
                                                    if(options.mySuccessCallBack != undefined && options.mySuccessCallBack != null && $.isFunction(options.mySuccessCallBack)) {

                                                        return options.mySuccessCallBack(data);
                                                    }
                                                }
                                            }
                                        };

                                        var _ajaxOption = $.extend(true, _defaultAjaxOptions, options.ajaxFormOption);

                                        $(formSelector).ajaxSubmit(_ajaxOption);

                                        return true;
                                    }
                                    else{

                                        // Show error message.
                                        submitButton.disabled = false;
                                        submitButton.removeAttribute('data-kt-indicator');

                                        Base.Alert.custom({
                                            text: "Errores detectados al validar el formulario, por favor intente nuevamente.",
                                            icon: "error",
                                            buttonsStyling: false,
                                            confirmButtonText: "Volver al formulario!",
                                            customClass: {
                                                confirmButton: "btn btn-primary"
                                            },
                                            callBackThen: function(result){
                                                KTUtil.scrollTop();
                                            }
                                        });

                                        return false;
                                    }
                                });
                            }

                            return false;
                        }
                    },
                    cancel: {
                        label: "Cancelar",
                        className: 'btn btn-light',
                        callback: function(){
                           Base.Loading.hide();

                           return true;
                        }
                    },
                }

                //Delete
                if(options.actionDelete != undefined && options.actionDelete != null && $.isFunction(options.actionDelete)){
                    buttons = $.extend(true,
                        {
                            submit: {},
                            delete : {
                                label: "Eliminar",
                                className: 'btn btn-warning',
                                callback: function(){
                                    options.actionDelete(data)
                                }
                            },
                            cancel : {},
                        },
                        buttons);
                }

                var defaultOptions = {

                    title: 'Mensaje',
                    message: " ",
                    size: 'large',
                    scrollable: false,
                    mySuccessCallBack: null,
                    myErrorCallBack: null,
                    swapButtonOrder: false,
                    ajaxFormOption : {},
                    buttons: buttons
                };

                var finallyOptions = $.extend(true, defaultOptions, options);

                Base.ajax(
                    url,
                    data,
                    function (text) {

                       Base.Loading.hide();
                       // Base.Message.clear();

                        newOptions = $.extend(true, finallyOptions,
                            {message : text});

                        bootbox.dialog(finallyOptions);
                        $('#base-bootbox-id select').each(function(idx, element){
                            $(element).attr("data-dropdown-parent", "#base-bootbox-id");
                        });
                    },
                    {
                        type: 'POST',
                        dataType: "html"
                    });
            },

            viewAjax: function (url, data, options) {

                var _i18n = {
                };

                _i18n = $.extend(true, _i18n, Base.i18n.box);

                if (options._i18n !== undefined && options._i18n != null) {
                    _i18n = $.extend(true, _i18n, options._i18n);
                }


                buttons = {};

                //Botones por Defecto
                if (options.showCloseButton  == undefined ||
                    options.showCloseButton == null || options.showCloseButton){

                    buttons = {
                        accept: {
                            label: "Aceptar",
                            className: 'btn btn-primary',
                            callback: function(){
                            }
                        },
                        cancel: {
                            label: "Cancelar",
                            className: 'btn btn-light',
                            callback: function(){
                            }
                        },
                    }
                }
                else if(!options.showCloseButton){

                    buttons = {
                        accept: {
                            label: "Aceptar",
                            className: 'btn btn-primary',
                            callback: function(){
                            }
                        }
                    }
                }

                var defaultOptions = {

                    title: 'Mensaje',
                    message: " ",
                    size: 'medium',
                    scrollable: false,
                    buttons: buttons
                };

                var finallyOptions = $.extend(true, defaultOptions, options);


                Base.ajax(
                    url,
                    data,
                    function (text) {

                        newOptions = $.extend(true, finallyOptions,
                            {message : text});

                        bootbox.dialog(finallyOptions);
                        $('#base-bootbox-id select').each(function(idx, element){
                            $(element).attr("data-dropdown-parent", "#base-bootbox-id");
                        });
                    },
                    {
                        type: 'POST',
                        dataType: "html"
                    });
            },

            dialog: function (options) {

                var _i18n = {
                };

                _i18n = $.extend(true, _i18n, Base.i18n.box);

                if (options._i18n !== undefined && options._i18n != null) {
                    _i18n = $.extend(true, _i18n, options._i18n);
                }

                var defaultOptions = {

                    title: 'Mensaje',
                    message: " ",
                    size: 'medium',
                    scrollable: false,
                    swapButtonOrder: true,
                    buttons: {
                       accept: {
                            label: "Aceptar",
                            className: 'btn btn-primary',
                            callback: function(){
                            }
                        },
                        cancel: {
                            label: "Cancelar",
                            className: 'btn btn-light',
                            callback: function(){
                            }
                        }
                    }
                };

                var finallyOptions = $.extend(true, defaultOptions, options);

               Base.Loading.hide();

                var dialog = bootbox.dialog(finallyOptions);

                return dialog;
            },

            confirm: function(options){

                alert("Deprecado para este Tema. Use Base.Alert()");
                console.log("Deprecado para este Tema. Use Base.Alert()");

                var _i18n = {
                };

                _i18n = $.extend(true, _i18n, Base.i18n.box);

                if (options._i18n !== undefined && options._i18n != null) {
                    _i18n = $.extend(true, _i18n, options._i18n);
                }

                var defaultOptions = {
                    title: 'Confirmación',
                    message: "¿ Seguro desea realizar la acción ?",
                    size: 'small',
                    inputType: 'text',
                    swapButtonOrder: true,
                    centerVertical: true,
                    buttons: {
                        cancel: {
                            label: "Cancelar",
                            className: 'btn btn-light'
                        },
                        confirm: {
                            label: "Aceptar",
                            className: 'btn btn-primary'
                        }
                    },
                    callback: function(result){

                    },
                };

                var finallyOptions = $.extend(true, defaultOptions, options);

                //Base.Loading.hide();

                var dialog = bootbox.confirm(finallyOptions);

                return dialog;
            },

            prompt: function (options) {

                var _i18n = {
                };

                _i18n = $.extend(true, _i18n, Base.i18n.box);

                if (options._i18n !== undefined && options._i18n != null) {
                    _i18n = $.extend(true, _i18n, options._i18n);
                }

                var defaultOptions = {
                    title: 'Confirmación',
                    message: " ",
                    size: 'small',
                    inputType: 'text',
                    centerVertical: true,
                    swapButtonOrder: true,
                    buttons: {
                        cancel: {
                            label: "Cancelar",
                            className: 'btn btn-light'
                        },
                        confirm: {
                            label: "Aceptar",
                            className: 'btn btn-primary'
                        }
                    },
                    callback: function(value){

                    },
                };

                var finallyOptions = $.extend(true, defaultOptions, options);

                //Base.Loading.hide();

                var dialog = bootbox.prompt(finallyOptions);

                return dialog;
            },

            alert: function (options) {

                alert("Deprecado para este Tema. Use Base.Alert()");
                console.log("Deprecado para este Tema. Use Base.Alert()");

                var _i18n = {
                };

                _i18n = $.extend(true, _i18n, Base.i18n.box);

                if (options._i18n !== undefined && options._i18n != null) {
                    _i18n = $.extend(true, _i18n, options._i18n);
                }

                var defaultOptions = {
                    title: "Mensaje",
                    backdrop: true,
                    message: " ",
                    size: 'small',
                    buttons: {
                        ok: {
                            label: "Aceptar",
                            className: 'btn btn-primary',
                            callback: function(){
                            }
                        }
                    }
                };

                var finallyOptions = $.extend(true, defaultOptions, options);

               Base.Loading.hide();

                return bootbox.alert(finallyOptions);
            },

            clearAll: function(){

                bootbox.hideAll();
            },

            _qxBox : function (box) {

                return {

                    ver : function () {

                        console.log("Base.Box 1.0");
                        console.log("Requiere: Base 1.0");
                    },
                }
            }
        }
    });

    $.fn.QxBox = function () {

        var box = this;

        return Base.Box._qxBox(box);
    }
    
}); 