/**
 * Base.Buttons 1.0
 *
 * Requiere
 *     Base > 1.0
 *     ...
 */
$(function() {
    $.extend(Base, {

        Button :  {

            Icon : {
                
                Style: {
                    
                    Edit: 'fas fa-edit' ,
                    View: 'fas fa-search',
                    Check: 'fas fa-check',
                    Evaluation: 'fas fa-clipboard',
                    PEProfiles: 'fas fa-graduation-cap',
                    ToggleON: 'fas fa-toggle-on',
                    ToggleOFF: 'fas fa-toggle-off',
                    GrantAccess: 'fas fa-key',
                    Cancel: 'fas fa-close',
                    PreEnrollment: 'fas fa-address-card-o',
                    Enrollment: 'fas fa-address-card',
                    Group: 'fas fa-users',
                    Announcment: 'fas fa-book',
                    Students: 'fas fa-users',
                    Delete: 'fas fa-trash',
                    Download: 'fas fa-file-download'
                },
                
                Edit: function (options) {

                    var defaultOptions = {
                        chkRoles: null,
                        id : Base.guid(),
                        url: '#',
                        title: 'Editar',
                        class: Base.Button.Icon.Style.Edit,
                        containerClass : 'list-table-action-button'
                    };

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


                    return this._icon(finallyOptions);
                },

                View: function (options) {

                    var defaultOptions = {
                        chkRoles: null,
                        id : Base.guid(),
                        url: '#',
                        title: 'Ver Detalles',
                        class: Base.Button.Icon.Style.View,
                        containerClass : 'list-table-action-button'
                    };

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

                    return this._icon(finallyOptions);
                },

                Action: function (options) {

                    var defaultOptions = {
                        chkRoles: null,
                        id : Base.guid(),
                        url: '#',
                        title: 'Acction',
                        class: ((options.class != undefined) ? options.class : Base.Button.Icon.Style.Check),
                        containerClass : 'list-table-action-button'
                    };

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

                    return this._icon(finallyOptions);
                },

                ActionConfirm: function (options) {

                    var defaultOptions = {
                        confirm : {
                            title: 'Confirmación',
                            message: "¿ Seguro desea realizar la acción ?",
                        },
                        chkRoles: null,
                        id : Base.guid(),
                        url: '#',
                        txt: '',
                        title: 'Acction',
                        class: Base.Button.Icon.Style.Check,
                        containerClass : 'list-table-action-button'
                    };

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

                    return this._iconConfirm(finallyOptions);
                },

                CallFunction: function (options) {
                   
                    var defaultOptions = {
                        chkRoles: null,
                        id : Base.guid(),
                        title: 'Acction',
                        class: Base.Button.Icon.Style.Check,
                        containerClass : 'list-table-action-button',
                        functionCall: null,
                        params: null
                    };

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

                    return this._iconFunction(finallyOptions);
                },

                /**
                 * @deprecated Usar CallFunction con Base.Box.prompt
                 */
                CallConfirmFunction: function (options) {

                    var defaultOptions = {
                        confirm : {
                            title: 'Confirmación',
                            message: "¿ Seguro desea realizar la acción ?",
                        },
                        chkRoles: null,
                        id : Base.guid(),
                        title: 'Acction',
                        class: Base.Button.Icon.Style.Check,
                        containerClass : 'list-table-action-button',
                        functionCall: null,
                        params: null
                    };

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

                    return this._iconConfirmCallFunction(finallyOptions);
                },

                _icon: function (options) {

                    if(options.chkRoles != null && options.chkRoles != undefined && options.chkRoles != ''){

                        if (!Base.isSecurityAccessFor(options.chkRoles))
                            return "";
                    }

                    var icon = '<a title="' + options.title + '" id="' + options.id + '" href="' + options.url + '" class="' + options.containerClass + '"><i class="' + options.class + '"></i></a>';


                    return icon;
                },

                _iconConfirm: function (options) {

                    if(options.chkRoles != null && options.chkRoles != undefined && options.chkRoles != ''){

                        if (!Base.isSecurityAccessFor(options.chkRoles))
                            return "";
                    }

                    var onClick = "Base.Box.confirm({" +
                        " title: '" + options.confirm.title + "'," +
                        " message: '" + options.confirm.message + "'," +
                        " callback: function(result){" +
                        "    if(result){" +
                        "      Base.Loading.show({}); " +
                        "      window.location = '" + options.url + "' ;" +
                        "    }" +
                        "     }" +
                        "  });";


                    var icon = '<a title="' + options.title + '" ' +
                               ' id="' + options.id + '" ' +
                               ' href="#" ' +
                               ' class="' + options.containerClass + '" ' +
                               ' onclick="' + onClick + '" >' +
                               ' <i class="' + options.class + '"></i></a>';
                    return icon;
                },

                _iconFunction: function (options) {

                    if(options.chkRoles != null && options.chkRoles != undefined && options.chkRoles != ''){

                        if (!Base.isSecurityAccessFor(options.chkRoles))
                            return "";
                    }

                    var params = JSON.stringify( options.params );

                    var regex = new RegExp("\"", "g");
                    params = params.replace(regex, "'");

                    var icon = '<span title="' + options.title + '" id="' + options.id + '" class="' + options.containerClass + '">'
                        +'<i class="' + options.class + '" onclick="' + options.functionCall + '(this,' + params + ')"></i></span>';

                    return icon;
                },

                _iconConfirmCallFunction: function (options) {

                    if(options.chkRoles != null && options.chkRoles != undefined && options.chkRoles != ''){

                        if (!Base.isSecurityAccessFor(options.chkRoles))
                            return "";
                    }

                    var params = JSON.stringify( options.params );

                    var regex = new RegExp("\"", "g");
                    params = params.replace(regex, "'");



                    var onClick = "Base.Box.confirm({" +
                        " title: '" + options.confirm.title + "'," +
                        " message: '" + options.confirm.message + "'," +
                        " callback: function(result){" +
                        "    if(result){" +
                        "      " + options.functionCall + '(this,' + params + ') ' +
                        "    }" +
                        "     }" +
                        "  });";


                    var icon = '<a title="' + options.title + '" ' +
                               ' id="' + options.id + '" ' +
                               ' href="#" ' +
                               ' class="' + options.containerClass + '" ' +
                               ' onclick="' + onClick + '" >' +
                               ' <i class="' + options.class + '"> ' +
                                ' </i></a>';


                    return icon;
                }
            },

            Button: function () {

            },

            
        }
    });
});