define([
    'ko',
    'jquery',
    'underscore',
    'mage/translate',
    'text!Repertus_Packstation/template/modal/packstation-search-modal.html',
    'mage/url',
    'Magento_Ui/js/modal/modal'
], function (ko, $, _, $t, template, urlBuilder) {
	'use strict'
	
	var mixin = {
		search: function () {
            var me = this;

            me.enableDisableSearchButton(false);
            me.showOverlay();

            $.ajax({
                'url': this.getSearchUrl(),
                'dataType': 'json',
                'type': 'POST',
                'data': {
                    'zipcode': me.postCodeValue(),
                    'city': me.cityValue()
                },
                'error': function (result) {
                    me.errorMessages.push({
                        type: 'error',
                        text: $.mage.__(result.responseJSON.message)
                    });
                    me.hideOverlay();
                    me.enableDisableSearchButton(true);
                },
                'success': function (result) {
                    var stations = result['packstations'];
                    var first = true;

                    if (stations == null) {
                        me.mapContainer.setView({center: new Microsoft.Maps.Location(0, 0)});
                    }

                    me.map.entities.clear();

                    $(stations).each(function () {
//MNM - only packstation
                    	if(this.automatType == 4)
                    	{
	                        var pinLocation = new Microsoft.Maps.Location(this.location.latitude, this.location.longitude),
	                            pin = me.createNewPin(this, pinLocation);
	
	                        me.map.entities.push(pin);
	
	                        if (first) {
	                            first = false;
	                            me.map.setView({center: pinLocation});
	                        }
	
	                        Microsoft.Maps.Events.addHandler(pin, 'click', function (e) {
	                            me.centerInfoBox(pin);
	                            me.showInfoBox(e, me.getInfoBox());
	                        });
	                        Microsoft.Maps.Events.addHandler(pin, 'mouseover', function (e) {
	                            me.showInfoBox(e, me.getInfoBox());
	                        });
                    	}
                    });

                    me.hideOverlay();
                    me.enableDisableSearchButton(true);
                }
            });
        }
	};
	
	return function(widget) {
		$.widget('repertus.packstationSearch', widget, mixin);
		
		return $.repertus.packstationSearch;
	};
});
	