<?php
/**
 * WP Submenu and Add and Update Options
 *
 * @since 1.0.0
 * @todo Replace only if your creating your own Plugin
 * @todo dst - Find all and replace text
 * @todo dealer - Find all and replace text
 * @todo Note - Find all and replace text
 */

// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;

/**
 * Sub menu hooks (unused)
 * 
 *
 * @since 1.0
 * @return void
 */

function dst_register_submenu() {
	add_submenu_page( 
		'edit.php?post_type=dealer', // for tools 'tools.php', for options 'options.php'
		__( 'Note Settings', 'dst-txt' ), // Page Title
		__( 'Settings', 'dst-txt' ), // Menu Title
		 'manage_options', // Capability
		 'dst-submenu-page', // Menu Slug 
		 'dst_submenu_admin_page_callback' ); // Function
}

add_action( 'admin_menu', 'dst_register_submenu');

/**
 * WP Options (Get, Update, and Add)
 *
 * @see Options API (https://codex.wordpress.org/Options_API)
 */
function dst_submenu_admin_page_callback() {

	$map_key = DST()->db->get_option('','dst_options', 0);
	$map_key_str = '';
    
    // May Key Check
    if(!empty($map_key)){
        $map_key_str = '&key='.$map_key;
    }

	// $_POST needs to be sanitized
	if(isset($_POST['submit'])
		&& check_admin_referer('dst_option_action','dst_option_field') // @see WP Docs for check_admin_referer()
	){

		/** Array DB $options; if emtpy assign empty string */
		$options = array(
			'dst_options' => isset($_POST["dst_options"]) ? $_POST["dst_options"]  : array(),
		);

		/* Handling var Array */	
		foreach($options as $option_name => $option_value) {
			// If option name exist, update it; else add it!
			if ( get_option( $option_name ) !== false ) {
				update_option($option_name, $option_value);
			} else {	
				add_option( $option_name, $option_value, '', 'yes');
			}
		}
	}
?>	
	<div id="dst-setting-page" class="wrap">
		<h1><?php _e('Note Settings', 'dst-txt'); ?></h1>
		<span class="title"><?php _e('General Configuration Settings for Notes', 'dst-txt'); ?></span>

		<form method="post" action="<?php echo esc_attr($_SERVER["REQUEST_URI"]); ?>">
			<?php wp_nonce_field('dst_option_action','dst_option_field'); ?>
			<table class="form-table">
				<tbody>
				</tbody>
			</table>
			<h2 class="dst-title"><?php _e('Google Maps Settings', 'dst-txt'); ?></h2>
			<p><?php _e('This section is for google maps settings.', 'dst-txt'); ?></p>
			<table class="form-table">
				<tbody> 
					<tr>
						<th scope="row"><label for="dst-googlemap-api"><?php _e('Insert Google Maps API', 'dst-txt'); ?></label></th>
						<td>
							<input id="dst-googlemap-api" class="dst-googlemap-api regular-text" name="dst_options[]" type="text" value="<?php echo DST()->db->get_option('','dst_options', 0) ?>">
							<p class="dst-description"><?php _e('You can get your Google API <a target="_blank" href="https://developers.google.com/maps/documentation/javascript/get-api-key">here</a>', 'dst-txt'); ?></p>
							<p class="dst-description"><?php _e('Important Notice: You have to click save button first.', 'dst-txt'); ?></p>
						</td>
					</tr>
					<tr>
					<th scope="row"><label for="dst-default-zoom"><?php _e('Zoom Default', 'dst-txt'); ?></label></th>
						<td><input class="dst-default-zoom regular-text" name="dst_options[]" type="number" value="<?php echo DST()->db->get_option('','dst_options', 1) ?>">
						</td>
					</tr>
				</tbody>
			</table>
			<p class="submit"><input type="submit" name="submit" class="button-primary" value="<?php _e('Save Changes', 'dst-txt'); ?>" /></p>
			<h2 class="dst-title"><?php _e('Google Maps Center', 'dst-txt'); ?></h2>
			<p><?php _e('This section is for default center of the map.', 'dst-txt'); ?></p>
			<table class="form-table">
				<tbody>
					<th scope="row"><label for="dst-center-map-address"><?php _e('Center Map Address', 'dst-txt'); ?></label></th>
						<td><input id="dst-address"  class="dst-center-map-address regular-text" name="dst_options[]" type="text" value="<?php echo DST()->db->get_option('','dst_options', 2) ?>">
							<p class="dst-description"><?php _e('e.g. <code>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</code>', 'dst-txt'); ?></p>
						</td>
					</tr>
					<th scope="row"><label for="dst-default-lat"><?php _e('Latitude', 'dst-txt'); ?></label></th>
						<td><input id="dst-latitude" class="dst-default-lat gst-map-refresh regular-text" name="dst_options[]" type="text" value="<?php echo DST()->db->get_option('','dst_options', 3) ?>">
							<p class="dst-description"><?php _e('This value will be automatically generated or click to manually refresh', 'dst-txt'); ?> <a href="#"><span class="gst-map-refresh dashicons dashicons-image-rotate"></span></a></p>
						</td>
					</tr>
					<th scope="row"><label for="dst-default-lng"><?php _e('Longitude', 'dst-txt'); ?></label></th>
						<td><input id="dst-longitude" class="dst-default-lng gst-map-refresh regular-text" name="dst_options[]" type="text" value="<?php echo DST()->db->get_option('','dst_options', 4) ?>">
							<p class="dst-description"><?php _e('This value will be automatically generated or click to manually refresh', 'dst-txt'); ?><a href="#"><span class="gst-map-refresh dashicons dashicons-image-rotate"></span></a></p>
						</td>
					</tr>
				</tbody>
			</table>
			<h2 class="title"><?php _e('Shortcode Features and Usage', 'dst-txt'); ?></h2>
			<p><?php _e('Just insert into Posts or Pages :)', 'dst-txt'); ?></p>
			<table class="form-table">
				<tbody> 
					<tr>
						<ul>
							<li><code>[dst-sc]</code> - Display Dealer Search</li>
						</ul>
					</tr>
				
				</tbody>
			</table>
			<p class="submit"><input type="submit" name="submit" class="button-primary" value="<?php _e('Save Changes', 'dst-txt'); ?>" /></p>
		</form>
	</div>
<script src="https://maps.google.com/maps/api/js?sensor=false<?php echo $map_key_str; ?>"></script>
	<script type="text/javascript">
		jQuery(document).ready(function($){
			'use strict'
			var addressSeclects = $("#dst-address"),
				getAddress = $("#dst-address").val(), 
			    getKey = $("#dst-googlemap-api").val();

			function dstGeoMap(address){
					var geocoder = new google.maps.Geocoder();
					if (geocoder) {
						geocoder.geocode({ 'address': address }, function (results, status) {
							if (status == google.maps.GeocoderStatus.OK) {
								$("#dst-latitude").val(results[0].geometry.location.lat());
								$("#dst-longitude").val(results[0].geometry.location.lng());
							} else {
								$("#dst-latitude").val('Latitude Geocoding Failed');
								$("#dst-longitude").val('Longitude Geocoding Failed');
								console.log("Geocoding failed: " + status);
							}
						          
						});
					} 
			}

		    $(addressSeclects).change(function() {
		  		// Google Maps Geocoder
		  		dstGeoMap(getAddress);
		    });

		    // Manual Refresh
			$(".gst-map-refresh").click(function(e) {
				e.preventDefault();
				dstGeoMap(getAddress);  				
		    });		
		  
		});
</script>
<?php 	
}





