<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
?>
<div class="generic_layout_container layout_top">
	<div class="generic_layout_container layout_middle">
		<?php echo $this->content()->renderWidget('user.user-setting-cover-photo'); ?>
	</div>
</div>


<?php
$enableglocation = Engine_Api::_()->getApi('settings', 'core')->getSetting('enableglocation', '0');
$optionsenableglotion = unserialize(Engine_Api::_()->getApi('settings', 'core')->getSetting('optionsenableglotion', ''));
?>

<div class="generic_layout_container layout_main user_setting_main_page_main">
	<div class="generic_layout_container layout_left">
		<div class="theiaStickySidebar">
			<?php echo $this->content()->renderWidget('user.settings-menu'); ?>
		</div>
	</div>
	<div class="generic_layout_container layout_middle user_setting_main_middle user_edit_location_form">
		<div class="theiaStickySidebar">
			<div class="user_setting_global_form">
				<div class="global_form">
					<?php if ($this->form->saveSuccessful): ?>
						<h3><?php echo $this->translate('Settings were successfully saved.'); ?></h3>
					<?php endif; ?>
					<?php echo $this->form->render($this) ?>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="application/javascript">
	en4.core.runonce.add(function() {
		<?php if (!empty($optionsenableglotion) && empty($enableglocation)) { ?>
			<?php if (!engine_in_array('country', $optionsenableglotion)) { ?>
				scriptJquery('#user_country-wrapper').hide();
			<?php } ?>
			<?php if (!engine_in_array('state', $optionsenableglotion)) { ?>
				scriptJquery('#user_state-wrapper').hide();
			<?php } ?>
			<?php if (!engine_in_array('city', $optionsenableglotion)) { ?>
				scriptJquery('#user_city-wrapper').hide();
			<?php } ?>
			<?php if (!engine_in_array('zip', $optionsenableglotion)) { ?>
				scriptJquery('#user_zip-wrapper').hide();
			<?php } ?>
			<?php if (!engine_in_array('lat', $optionsenableglotion)) { ?>
				scriptJquery('#user_lat-wrapper').hide();
			<?php } ?>
			<?php if (!engine_in_array('lng', $optionsenableglotion)) { ?>
				scriptJquery('#user_lng-wrapper').hide();
			<?php } ?>
		<?php } ?>
	});

	var input = document.getElementById('user_edit_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());
		marker.setPosition(myLatlng);
		map.panTo(myLatlng);
		getLocationData(place.geometry.location.lat(), place.geometry.location.lng());

	});

	var latlng = new google.maps.LatLng(<?php echo $this->locationLatLng->lat; ?>, <?php echo $this->locationLatLng->lng; ?>);
	map = new google.maps.Map(document.getElementById('locationSesEdit'), {
		zoom: 14,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});
	var marker = new google.maps.Marker({
		position: latlng,
		map: map,
		zoom: 25,
		title: 'Current Location',
		draggable: true,
	});

	google.maps.event.addListener(marker, 'drag', function(event) {
		document.getElementById('user_lat').value = event.latLng.lat();
		document.getElementById('user_lng').value = event.latLng.lng();
	});
	var lngGetOpn = '<?php echo $this->locationLatLng->lng; ?>';
	var latGetOpn = '<?php echo $this->locationLatLng->lat; ?>';
	google.maps.event.addListener(marker, 'dragend', function(event) {
		document.getElementById('user_lat').value = latGetOpn = event.latLng.lat();
		document.getElementById('user_lng').value = lngGetOpn = event.latLng.lng();
		myLatlng = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
		getLocationData(event.latLng.lat(), event.latLng.lng());
	});
	var myLatlng = new google.maps.LatLng(<?php echo $this->locationLatLng->lat; ?>, <?php echo $this->locationLatLng->lng; ?>);
	google.maps.event.addListener(marker, 'click', function() {
		var infowindow = new google.maps.InfoWindow({
			content: 'Latitude: ' + latGetOpn + '<br>Longitude: ' + lngGetOpn
		});
		infowindow.open(map, marker);
	});

	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_edit_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_edit_location').val('');
				scriptJquery('#user_zip').val('');
				scriptJquery('#user_city').val('');
				scriptJquery('#user_state').val('');
				scriptJquery('#user_country').val('');
			}
		});
	}
</script>