
function configComboboxUsuarios(usuariosDashboard) {
    $(function () {
        $("#CodigoEmpresa").change(x => {
            $('.ui-autocomplete-input').val("");
            $('#autocompleteUsuariosDash').val("");
        });

        $.widget("custom.combobox", {
            _create: function () {
                this.wrapper = $("<span>")
                    .addClass("custom-combobox")
                    .insertAfter(this.element);

                this.element.hide();
                this._createAutocomplete();
                this._createShowAllButton();
            },

            _createAutocomplete: function () {
                var selected = this.element.children(":selected"),
                    value = selected.val() ? selected.text() : "";

                this.input = $("<input>")
                    .appendTo(this.wrapper)
                    .val(value)
                    .attr("title", "")
                    .attr("type", "text")
                    .attr("placeholder", "Selecione seu usuário do Dashboard")
                    .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left form-control")
                    .autocomplete({
                        delay: 0,
                        minLength: 0,
                        source: $.proxy(this, "_source"),
                        messages: {
                            noResults: '',
                            results: function () { }
                        }
                    })
                    .tooltip({
                        classes: {
                            "ui-tooltip": "ui-state-highlight"
                        }
                    }).on('mousedown', function () {
                        wasOpen = $(this).autocomplete('widget').is(':visible');
                    })
                    .on('click', function () {

                        if (wasOpen) {
                            return;
                        }

                        $(this).autocomplete('search', $(this).val());
                    });

                $('.ui-autocomplete').addClass(`autocomplete-usuarios`);

                this._on(this.input, {
                    autocompleteselect: function (event, ui) {
                        ui.item.option.selected = true;
                        $("#autocompleteUsuariosDash").val(ui.item.option);
                    },
                    autocompletechange: "_removeIfInvalid"
                });
            },

            _createShowAllButton: function () {
                var input = this.input,
                    wasOpen = false;

                $("<a>")
                    .attr("tabIndex", -1)
                    .attr("title", "Show All Items")
                    .tooltip()
                    .appendTo(this.wrapper)
                    .button({
                        icons: {
                            primary: "ui-icon-triangle-1-s"
                        },
                        text: false
                    })
                    .removeClass("ui-corner-all")
                    .addClass("custom-combobox-toggle ui-corner-right")
                    .on("mousedown", function () {
                        wasOpen = input.autocomplete("widget").is(":visible");
                    })
                    .on("click", function () {
                        input.trigger("focus");

                        // Close if already visible
                        if (wasOpen) {
                            return;
                        }
                        // Pass empty string as value to search for, displaying all results
                        input.autocomplete("search", $(this).val());
                    });
            },

            _source: function (request, response) {
                var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                response(usuariosDashboard.filter(usuario => {
                    return (usuario.CodigoEmpresa == parseInt($('#CodigoEmpresa').val()) || !usuario.CodigoEmpresa) && usuario.Usuario && (!request.term || matcher.test(usuario.Usuario));
                }).map((usuario) => {
                    return {
                        label: usuario.Usuario,
                        value: usuario.Usuario,
                        option: usuario.UsuarioID
                    };
                }));
            },

            _removeIfInvalid: (event, ui) => { },
            _destroy: function () {
                this.wrapper.remove();
                this.element.show();
            }
        });

        $("#autocompleteUsuariosDash").combobox();
    });
}