<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
?>
<script>

	var input = document.getElementById('user_location');
	var autocomplete = new google.maps.places.Autocomplete(input)
	google.maps.event.addListener(autocomplete, 'place_changed', function() {
		var place = autocomplete.getPlace();
		if (!place.geometry)
			return;
		document.getElementById('user_lng').value = lngGetOpn = place.geometry.location.lng();
		document.getElementById('user_lat').value = latGetOpn = place.geometry.location.lat();
		myLatlng = new google.maps.LatLng(place.geometry.location.lat(), place.geometry.location.lng());

		// map.panTo(myLatlng);
		getLocationData(place.geometry.location.lat(), place.geometry.location.lng());

	});

	function getLocationData(lat, lng) {
		var geocoder = new google.maps.Geocoder();
		var city = state = country = postalCode = '';
		geocoder.geocode({
			'latLng': new google.maps.LatLng(lat, lng)
		}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK && results.length) {
				scriptJquery('#user_location').val(results[0].formatted_address);
				if (results[0]) {
					if (typeof results[0].address_components != 'undefined') {
						for (var i = 0; i < results[0].address_components.length; i++) {
							if (results[0].address_components[i].types[0] == 'postal_code') {
								var postalCode = results[0].address_components[i].long_name;
							}
						}
					}
				}
				if (results[1]) {
					var indice = 0;
					for (var j = 0; j < results.length; j++) {
						if (results[j].types[0] == 'locality') {
							indice = j;
							break;
						}
					}
					if (typeof results[j].address_components != 'undefined') {
						for (var i = 0; i < results[j].address_components.length; i++) {
							if (results[j].address_components[i].types[0] == "locality") {
								//this is the object you are looking for
								city = results[j].address_components[i].long_name;
							}
							if (results[j].address_components[i].types[0] == "administrative_area_level_1") {
								//this is the object you are looking for
								state = results[j].address_components[i].long_name;
							}
							if (results[j].address_components[i].types[0] == "country") {
								//this is the object you are looking for
								country = results[j].address_components[i].long_name;
							}
						}
					}
					if (postalCode)
						scriptJquery('#user_zip').val(postalCode);
					else
						scriptJquery('#user_zip').val('');
					if (city)
						scriptJquery('#user_city').val(city);
					else
						scriptJquery('#user_city').val('');
					if (state)
						scriptJquery('#user_state').val(state);
					else
						scriptJquery('#user_state').val('');
					if (country)
						scriptJquery('#user_country').val(country);
					else
						scriptJquery('#user_country').val('');
				}
			} else {
				scriptJquery('#user_location').val('');
				scriptJquery('#user_zip').val('');
				scriptJquery('#user_city').val('');
				scriptJquery('#user_state').val('');
				scriptJquery('#user_country').val('');
			}
		});
	}

	function addLocation(formObject) {

		var validationFm = validateForm();
		if (validationFm) {

			var input = scriptJquery(formObject);
			alert('<?php echo $this->string()->escapeJavascript("Please fill the red mark fields"); ?>');
			if (typeof objectError != 'undefined') {
				var errorFirstObject = scriptJquery(objectError).parent().parent();
				scriptJquery('html, body').animate({
					scrollTop: errorFirstObject.offset().top
				}, 2000);
			}
			return false;
		} else {
			submitLocation(formObject);
		}
	}

	function submitLocation(formObject) {

		scriptJquery('#user_location_overlay').show();
		var formData = new FormData(formObject);
		formData.append('is_ajax', 1);
		formData.append('format', 'json');
		scriptJquery.ajax({
			url: en4.core.baseUrl + "user/index/add-location",
			type: "POST",
			dataType:'json',
			contentType: false,
			processData: false,
			cache: false,
			data: formData,
			success: function(response) {

				var result = response; //scriptJquery.parseJSON(response);
				if (result.status == 1) {
					scriptJquery('#user_location_overlay').hide();
					scriptJquery('#ajaxsmoothbox_container').html("<div id='success_message' class='core_success_message success_message'><i class='fa fa-check-circle'></i><span>Your location is successfully added.</span></div>");

					scriptJquery('#success_message').fadeOut("slow", function() {
						setTimeout(function() {
							ajaxsmoothboxclose();
							window.location.href = result.url;
						}, 1000);
					});
				}
			}
		});
	}
</script>
<div class="user_edit_location_popup add_location_form">
	<div class="core_loading_cont_overlay" id="user_location_overlay" style="display:none;"></div>
	<?php if (empty($this->is_ajax)) { ?>
		<div class="global_form_wrap"><?php echo $this->form->render($this); ?></div>
	<?php } ?>
</div>
<?php die; ?>