/* 
 * Copyright (c) 2019 Earworks.
 * Codeigniter Project Template Modification
 * Please contact our email for more information :
 *  contact@earworks.co.id
 */


var register = function () {
    // define variable here
    var the = null;
    return {
        // define method here
        init: function () {
            the = this;
            the.typehead_company();
            the.typehead_dealer();
            the.typehead_visitor();
        },
        typehead_company: function () {
            var company = new Bloodhound({
                datumTokenizer: function (d) {
                    return Bloodhound.tokenizers.whitespace(d.name);
                },
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                limit: 10,
                prefetch: {
                    url: companyurl,
                    filter: function (list) {
                        return $.map(list, function (companyname) {
                            return {name: companyname};
                        });
                    }
                }
            });

            company.initialize();

            if (App.isRTL()) {
                $('#companyname').attr("dir", "rtl");
            }
            $('#companyname').typeahead(null, {
                name: 'companyname',
                displayKey: 'name',
                hint: (App.isRTL() ? false : true),
                source: company.ttAdapter()
            });
            $('#companyname').on('typeahead:selected', function (evt, item) {
                $.ajax({
                    'url' : companydetailurl,'type': 'POST','dataType' : 'JSON','data': item,
                    'success': function(res){
                       if(res.status === 1){
                           $('#companytype').html(res.data.companytype);
                           $('#type').val(res.data.type);
                       }else{
                           $('#companytype').empty();
                           $('#type').val('');
                       }
                    }
                });
            })
        },

        typehead_dealer: function () {


            var dealer = new Bloodhound({
                datumTokenizer: function (d) {
                    return Bloodhound.tokenizers.whitespace(d.name);
                },
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                limit: 10,
                prefetch: {
                    url: dealerurl,
                    filter: function (list) {
                        return $.map(list, function (dealername) {
                            return {name: dealername};
                        });
                    }
                }
            });

            dealer.initialize();

            if (App.isRTL()) {
                $('#dealername').attr("dir", "rtl");
            }
            $('#dealername').typeahead(null, {
                name: 'dealername',
                displayKey: 'name',
                hint: (App.isRTL() ? false : true),
                source: dealer.ttAdapter()
            });
        },


        typehead_visitor: function () {


            var visitor = new Bloodhound({
                datumTokenizer: function (d) {
                    return Bloodhound.tokenizers.whitespace(d.name);
                },
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                limit: 10,
                prefetch: {
                    url: visitorurl,
                    filter: function (list) {
                        return $.map(list, function (visitorname) {
                            return {name: visitorname};
                        });
                    }
                }
            });

            visitor.initialize();

            if (App.isRTL()) {
                $('#visitorname').attr("dir", "rtl");
            }
            $('#visitorname').typeahead(null, {
                name: 'visitorname',
                displayKey: 'name',
                hint: (App.isRTL() ? false : true),
                source: visitor.ttAdapter()
            });
        }
    };
}();
$(document).ready(function () {
    register.init();
});