
mainApp = null;
(function ($) {
    mainApp = {
        initTooltip: function () {
            var tooltips = $('[data-toggle="tooltip"]');
            $.each(tooltips, function(i, tooltip){
                var html = $(tooltip).data('html');
                var params = {};
                if(html){
                    params.html = true;
                    $(tooltip).tooltip(params);
                }
            });
        },
        initMask: function(){
            $('.ssn').mask('000-00-0000');
            $('.date').mask('00/00/0000');
            $('.phone, [name=cellNumber], [name=cellNumber]').mask('000-000-0000');
        },
        initMaskPhone: function(){
            $('.phone').mask('000-000-0000');
        },
        initAutoSuggestRecord: function(){
            var suggestInputs = $("[auto-suggest-record]");
            $.each(suggestInputs,  function(i, input){
                var placeholder = $(input).attr('placeholder');
                var url = $(input).data('url');
                var valueProp = $(input).data('valueprop');
                var companyId = $(input).data('companyid');
                var select2 = $(input).select2({
                    placeholder: placeholder,
                    minimumInputLength: 2,
                    ajax: {
                        url: url,
                        dataType: 'json',
                        type: "GET",
                        quietMillis: 50,
                        data: function (params) {
                            return {
                                term: params.term,
                                companyId: companyId,
                                json: true
                            };
                        },
                        processResults: function (data) {
                            data = data.results || [];
                            return {
                                results: $.map(data, function (item) {
                                    return {
                                        text: item.text,
                                        id: valueProp ? item[valueProp] : item.uid
                                    }
                                })
                            };
                        }
                    }
                });
                console.log({select2});
            });

        },
        initPopover: function(){
            $('[data-toggle="popover"]').popover();
        },
        init: function () {
            var self = this;
            self.initAutoSuggestRecord();
            self.initMask();
            $(document).on('ready', function () {
                self.initTooltip();
                self.initPopover();
            });

        },
    };
    mainApp.init();
})(jQuery);
