/**
 * Base.DashboardFilter 1.0
 *
 * Requiere
 *     Base 1.0
 */

$(function() {
    $.extend(Base, {
        
        DashboardFilter : {

            create: function(selector, options){

                selector = selector.replace("#", "");
                dashboardFilterSelector = "#dashboard_filter_container_" + selector;

                var _i18n = {
                    language: {}
                };

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

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

                var defaultOptions = {
                    callOnChange: null
                };

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

                $(dashboardFilterSelector).on('onChange', function(event, selector, dashboardFilter){

                   if(finallyOptions.callOnChange != undefined && finallyOptions.callOnChange != null && $.isFunction(finallyOptions.callOnChange)) {

                       return finallyOptions.callOnChange(selector, dashboardFilter);
                   }
                });

                return $(dashboardFilterSelector);
            },

    
            buildFiltersRequest : function (selector) {

                selector = selector.replace("#", "");
                var searchSelector = "#fsearchtext_" + selector;
                var filterSelector = "[filter-selector='" + "filters_" + selector + "']";
        
                var _buildSearch = function (_selector) {
        
                    return $(_selector).val();
                }
        
                var _builFilters = function (_selector) {
        
                    var filters = [];
        
                    var idx = 0;
                    $.each($(_selector), function (i, obj) {
        
                        if (typeof ($(obj).attr('name')) !== 'undefined') {
        
                            var fname = $(obj).attr('name');
                            var fvalue = '';
        
                            //Checkbox and Radio
                            if (($(obj).attr('type')) === 'checkbox' || ($(obj).attr('type')) === 'radio') {
                                //Add values
                                fvalue = Array();
        
                                $('input[name="' + fname + '"]:checked').each(function () {
                                    var val = this.value;
        
                                    if (val !== 'undefined' && val !== '' && val !== null && val !== 'null') {
                                        fvalue.push(val);
                                    }
                                });
                            }
                            //Others
                            else {
                                var val = $(obj).val();
        
                                if (val !== 'undefined' && val !== '' && val !== null && val !== 'null') {
        
                                    //Add values
                                    fvalue = val;
                                }
                            }
        
                            //Add Filter
                            if (fvalue !== 'undefined'
                                && fvalue !== ''
                                && fvalue !== null
                                && fvalue !== 'null'
                                && fvalue.length > 0
                        )
                            {
        
                                var filter = {
                                    name: fname,
                                    value: fvalue
                                };
        
                                filters[idx++] = filter;
                            }
                        }
                    });
        
                    return filters;
                }
        
                dashboarFilter = {
                    search: _buildSearch(searchSelector),
                    filters: _builFilters(filterSelector)
                };
        
                return dashboarFilter;
            },
            
            isFilterReady : function (selector) {

                selector = selector.replace("#", "");
                var filterSelector = "[filter-selector='" + "filters_" + selector + "']";

                if ($('#fsearchtext_' + selector).attr('filter-obligatory')  == 'true'){

                    if($('#fsearchtext_' + selector).val().trim() == ""){

                        return false;
                    }
                }

                var _obligatoryFiltersReady = function (_selector) {

                    var ofr = true;

                     $.each($(_selector), function (i, obj) {

                        if (typeof ($(obj).attr('name')) !== 'undefined') {

                            var fname = $(obj).attr('name');
                           
                            //Check  must has value if is obligatory
                            if ($(obj).attr('filter-obligatory') == 'true') {
                             
                                //Checkbox and Radio
                                if (($(obj).attr('type')) === 'checkbox' || ($(obj).attr('type')) === 'radio') {

                                    if ($('input[name="' + fname + '"]:checked').length == 0) {

                                        ofr = false;

                                        return;
                                    }
                                }
                                    
                                //Others
                                else {
                                    
                                    var val = $(obj).val();

                                    if (val == 'undefined' || val == '' || val == null || val == 'null') {

                                        ofr = false;
                                        
                                        return;
                                    }
                                }

                            }
                        }    
                    });

                    return ofr;                    
                };

                return _obligatoryFiltersReady(filterSelector);
            },

            _qxDashboardFilter : function (dashboarFilter) {

                return {

                    ver : function () {

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

                    triggerChange: function () {

                        var data  = $(dashboarFilter).data("dashboardFilterData");
                        $(dashboarFilter).trigger('onChange', [data.tableSelector, Base.DashboardFilter.buildFiltersRequest(data.tableSelector)]);
                    },
                }
            }
        }
    });

    $.fn.QxDashboardFilter = function () {

        var dashboarFilter = this;

        return Base.DashboardFilter._qxDashboardFilter(dashboarFilter);
    }
});