/**
 * Base.Select 1.0
 *
 * Requiere
 *     Base 1.0
 */
$(function () {

    $.extend(Base, {
        Select: {

            create: function (selector, options) {

                var _i18n = {
                    language: {}
                };


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

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

                var defaultOptions = {
                    allowClear: true,
                    closeOnSelect: true
                };

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

                return $(selector).select2(finallyOptions);
            },

            _qxSelect: function (select) {

                return {

                    ver: function () {

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

                    reset: function (defaultOption) {

                        defaultOption = (defaultOption == undefined) ? null : defaultOption;

                        $(select).val(defaultOption);
                        $(select).trigger("change");
                    },

                    clean: function () {

                        $(select).empty();
                        $(select).trigger("change");
                    },

                    destroy: function () {

                        $(select).select2("destroy");
                    },


                    reload: function (url, postData, callBackFunction) {
                        Base.ajax(
                            url,
                            postData,
                            function (data) {
                                $(select).empty();

                                if (!$(select).prop('multiple'))
                                    select.append("<option></option>");

                                $.each(data, function (i, item) {

                                    var addSelected = "";
                                    if (item.selected) {
                                        addSelected = ' selected="selected" '
                                    }

                                    var option = '<option '
                                        + 'value="' + item.val + '" '

                                        + addSelected

                                        + 'data-one="' + item.dataOne + '" '
                                        + 'data-tow="' + item.dataTow + '" '
                                        + 'data-three="' + item.dataThree + '"'
                                        + 'data-four="' + item.dataFour + '"'
                                        + 'data-five="' + item.dataFive + '"'
                                        + '>'
                                        + item.txt
                                        + '</option>';

                                    select.append(option);
                                });

                                $(select).trigger("change");

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

                                    return callBackFunction(data);
                                }
                            }
                        );
                    },

                    advange: function (url, defaultValue, postData) {
                        $(select).select2({
                            minimumInputLength: 0,
                            triggerChange: true,
                            data: [
                                { // Each of these gets processed by fnRenderResults.
                                    id: defaultValue.id,
                                    text: defaultValue.text,
                                    selected: true // Causes the selection to actually get selected.
                                }
                            ],
                            ajax: {
                                url: url,
                                delay: 0,
                                data: function (params) {
                                    return {
                                        q: params.term, // search term
                                        page: params.page,
                                        value_1: postData.value_1,
                                        value_2: postData.value_2
                                    };
                                },
                                processResults: function (data, params) {
                                    // parse the results into the format expected by Select2
                                    // since we are using custom formatting functions we do not need to
                                    // alter the remote JSON data, except to indicate that infinite
                                    // scrolling can be used
                                    params.page = params.page || 1;
                                    return {
                                        results: data,
                                        pagination: {
                                            more: (params.page * 10) < data.total_count
                                        }
                                    };
                                },
                                cache: true
                            }
                        });
                        $(select).val(defaultValue.id).trigger("change.select2");
                    },

                    advange_with_button: function (url, defaultValue, postData, addUrl) {
                        $(select).select2({
                            minimumInputLength: 0,
                            triggerChange: true,
                            data: [
                                { // Each of these gets processed by fnRenderResults.
                                    id: defaultValue.id,
                                    text: defaultValue.text,
                                    selected: true // Causes the selection to actually get selected.
                                }
                            ],
                            ajax: {
                                url: url,
                                delay: 0,
                                data: function (params) {
                                    return {
                                        q: params.term, // search term
                                        page: params.page,
                                        value_1: postData.value_1,
                                        value_2: postData.value_2
                                    };
                                },
                                processResults: function (data, params) {
                                    // parse the results into the format expected by Select2
                                    // since we are using custom formatting functions we do not need to
                                    // alter the remote JSON data, except to indicate that infinite
                                    // scrolling can be used
                                    params.page = params.page || 1;
                                    return {
                                        results: data,
                                        pagination: {
                                            more: (params.page * 10) < data.total_count
                                        }
                                    };
                                },
                                cache: true
                            },
                            language: {
                                noResults: function () {
                                    return $('<div>No se encontraron resultados. <a href="' + addUrl + '">Adicionar nuevo</a></div>');
                                }
                            },
                        });
                        $(select).val(defaultValue.id).trigger("change.select2");
                    }
                }
            }
        }
    });

    $.fn.QxSelect = function () {

        var select = this;

        return Base.Select._qxSelect(select);
    }
});