@section('scripts')
@parent
var map = null;
var post_office = null;
var zoom = 16;
var infos = [];
var markers = [];
var post_offices = [];
var url = '{{route('postoffice-streets')}}';
var labels = {{json_encode(array_merge(range('A', 'Z'), range('0', '99')))}};
$(document).ready(function()
{
$('form select[name="shipment_city"]').on('change', function(event)
{
infos = [];
markers = [];
var city = $(this).val();
var street = null;
$('.loading.streets').addClass('active');
$('form select[name=shipment_street]').attr('readonly', true);
$.ajax({
url: url,
type: 'GET',
data: {city:city},
success: function(response)
{
post_offices = response.post_offices;
street = $('form select[name=shipment_street]').val();
$('form select[name=shipment_street]').attr('readonly', false);
$('form select[name=shipment_street]').find('option').remove();
$.each(post_offices, function(index, value)
{
$('form .loading.streets').removeClass('active');
$('form select[name=shipment_street]').append($('' + value['name'] + '
' + value['street'] + ', ' + value['postcode'] + ' ' + city + '
' + String(value['opening_hours']).replace(/;/g, '
') + '
'});
var marker = new google.maps.Marker({position: {lat: parseFloat(value['gpsn']), lng: parseFloat(value['gpse'])}, label: { text: String(labels[key]), color: "#ffffff", fontWeight: "bold"}, title: value['name'], map: map});
marker.addListener('click', function()
{
if (!$('form select[name="shipment_street"] option[value="' + value['street'] + '"]').is(':selected'))
{
$.each(infos, function(index, info) { info.close(); });
$('form select[name="shipment_street"]').val(value['street']).change();
}
info.open(map, marker);
});
infos.push(info);
markers.push(marker);
});
}
}
});
});
$('form select[name="shipment_street"]').on('change', function(event)
{
var city = $('form select[name="shipment_city"]').val();
var street = $('form select[name="shipment_street"]').val();
var post_office = post_offices.filter(function(post_office) { return post_office['street'] == street; });
if (post_office.length)
{
map.setCenter({lat: parseFloat(post_office[0]['gpsn']), lng: parseFloat(post_office[0]['gpse'])});
map.setZoom(16);
}
});
if ($('form select[name="shipment_city"]').val())
{
$('form select[name="shipment_city"]').trigger('change');
}
});
@stop