<?php
/**
 * Custom Contact us Widget
 */

class VW_Radio_Station_Contact_Widget extends WP_Widget {
	function __construct() {
		parent::__construct(
			'VW_Radio_Station_Contact_Widget', 
			__('VW Contact us', 'vw-radio-station'),
			array( 'description' => __( 'Widget for contact us section in sidebar', 'vw-radio-station' ), ) 
		);
	}
	
	public function widget( $vw_radio_station_args, $vw_radio_station_instance ) {
		?>
		<aside class="widget">
			<?php
			$vw_radio_station_title = isset( $vw_radio_station_instance['title'] ) ? $vw_radio_station_instance['title'] : '';
			$vw_radio_station_phone = isset( $vw_radio_station_instance['phone'] ) ? $vw_radio_station_instance['phone'] : '';
			$vw_radio_station_email = isset( $vw_radio_station_instance['email'] ) ? $vw_radio_station_instance['email'] : '';
			$vw_radio_station_address = isset( $vw_radio_station_instance['address'] ) ? $vw_radio_station_instance['address'] : '';
			$vw_radio_station_timing = isset( $vw_radio_station_instance['timing'] ) ? $vw_radio_station_instance['timing'] : '';
			$vw_radio_station_longitude = isset( $vw_radio_station_instance['longitude'] ) ? $vw_radio_station_instance['longitude'] : '';
			$vw_radio_station_latitude = isset( $vw_radio_station_instance['latitude'] ) ? $vw_radio_station_instance['latitude'] : '';
			$vw_radio_station_contact_form = isset( $vw_radio_station_instance['contact_form'] ) ? $vw_radio_station_instance['contact_form'] : '';

	        echo '<div class="custom-contact-us">';
	        if(!empty($vw_radio_station_title) ){ ?><h3 class="custom_title1"><?php echo esc_html($vw_radio_station_title); ?></h3><?php } ?>
		        <?php if(!empty($vw_radio_station_phone) ){ ?>
		        	<div class="row contact-detail">
		        		<div class="col-lg-2 col-md-2 align-self-center">
		        			<span class="custom_details"><i class="fa-solid fa-phone-volume me-2"></i></span>
		        		</div>
		        		<div class="col-lg-10 col-md-10 align-self-center">
		        			<span class="contact-title"><?php echo esc_html('Contact', 'vw-radio-station'); ?></span><span class="custom_desc"><?php echo esc_html($vw_radio_station_phone); ?></span>
		        		</div>		        		
		        	</div>
		        <?php } ?>
		        <?php if(!empty($vw_radio_station_email) ){ ?>
		        	<div class="row contact-detail">
		        		<div class="col-lg-2 col-md-2 align-self-center">
		        			<span class="custom_details"><i class="fa-regular fa-envelope me-2"></i></span>
		        		</div>
		        		<div class="col-lg-10 col-md-10 align-self-center">
		        			<span class="contact-title"><?php echo esc_html('Mail Address', 'vw-radio-station'); ?></span><span class="custom_desc"><?php echo esc_html($vw_radio_station_email); ?></span>
		        		</div>
		        	</div>
		        <?php } ?>
		        <?php if(!empty($vw_radio_station_address) ){ ?>
		        	<div class="row contact-detail">
		        		<div class="col-lg-2 col-md-2 align-self-center">
		        			<span class="custom_details"><i class="fa-solid fa-location-dot me-2"></i></span>
		        		</div>
			        	<div class="col-lg-10 col-md-10 align-self-center">
			        		<span class="contact-title"><?php echo esc_html('Location', 'vw-radio-station'); ?></span><span class="custom_desc"><?php echo esc_html($vw_radio_station_address); ?></span>
			        	</div>
			        </div>
			    <?php } ?> 
		        <?php if(!empty($vw_radio_station_timing) ){ ?><p><span class="custom_details"><?php esc_html_e('Opening Time: ','vw-radio-station'); ?></span><span class="custom_desc"><?php echo esc_html($vw_radio_station_timing); ?></span></p><?php } ?>
		        <?php if(!empty($vw_radio_station_longitude) ){ ?><embed width="100%" height="200px" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?q=<?php echo esc_html($vw_radio_station_longitude); ?>,<?php echo esc_html($vw_radio_station_latitude); ?>&hl=es;z=14&amp;output=embed"></embed><?php } ?>
		        <?php if(!empty($vw_radio_station_contact_form) ){ ?><?php echo do_shortcode($vw_radio_station_contact_form); ?><?php } ?>
	        <?php echo '</div>';
			?>
		</aside>
		<?php
	}
	
	// Widget Backend 
	public function form( $vw_radio_station_instance ) {

		$vw_radio_station_title= ''; $vw_radio_station_phone= ''; $vw_radio_station_email = ''; $vw_radio_station_address = ''; $vw_radio_station_timing = ''; $vw_radio_station_longitude = ''; $vw_radio_station_latitude = ''; $vw_radio_station_contact_form = ''; 
		
		$vw_radio_station_title = isset( $vw_radio_station_instance['title'] ) ? $vw_radio_station_instance['title'] : '';
		$vw_radio_station_phone = isset( $vw_radio_station_instance['phone'] ) ? $vw_radio_station_instance['phone'] : '';
		$vw_radio_station_email = isset( $vw_radio_station_instance['email'] ) ? $vw_radio_station_instance['email'] : '';
		$vw_radio_station_address = isset( $vw_radio_station_instance['address'] ) ? $vw_radio_station_instance['address'] : '';
		$vw_radio_station_timing = isset( $vw_radio_station_instance['timing'] ) ? $vw_radio_station_instance['timing'] : '';
		$vw_radio_station_longitude = isset( $vw_radio_station_instance['longitude'] ) ? $vw_radio_station_instance['longitude'] : '';
		$vw_radio_station_latitude = isset( $vw_radio_station_instance['latitude'] ) ? $vw_radio_station_instance['latitude'] : '';
		$vw_radio_station_contact_form = isset( $vw_radio_station_instance['contact_form'] ) ? $vw_radio_station_instance['contact_form'] : '';
		
		?>

		<p>
        	<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','vw-radio-station'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($vw_radio_station_title); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('phone')); ?>"><?php esc_html_e('Phone Number:','vw-radio-station'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('phone')); ?>" name="<?php echo esc_attr($this->get_field_name('phone')); ?>" type="text" value="<?php echo esc_attr($vw_radio_station_phone); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('email')); ?>"><?php esc_html_e('Email id:','vw-radio-station'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('email')); ?>" name="<?php echo esc_attr($this->get_field_name('email')); ?>" type="text" value="<?php echo esc_attr($vw_radio_station_email); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('address')); ?>"><?php esc_html_e('Address:','vw-radio-station'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('address')); ?>" name="<?php echo esc_attr($this->get_field_name('address')); ?>" type="text" value="<?php echo esc_attr($vw_radio_station_address); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('timing')); ?>"><?php esc_html_e('Opening Time:','vw-radio-station'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('timing')); ?>" name="<?php echo esc_attr($this->get_field_name('timing')); ?>" type="text" value="<?php echo esc_attr($vw_radio_station_timing); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('longitude')); ?>"><?php esc_html_e('Longitude:','vw-radio-station'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('longitude')); ?>" name="<?php echo esc_attr($this->get_field_name('longitude')); ?>" type="text" value="<?php echo esc_attr($vw_radio_station_longitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('latitude')); ?>"><?php esc_html_e('Latitude:','vw-radio-station'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('latitude')); ?>" name="<?php echo esc_attr($this->get_field_name('latitude')); ?>" type="text" value="<?php echo esc_attr($vw_radio_station_latitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('contact_form')); ?>"><?php esc_html_e('Contact Form Shortcode:','vw-radio-station'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('contact_form')); ?>" name="<?php echo esc_attr($this->get_field_name('contact_form')); ?>" type="text" value="<?php echo esc_attr($vw_radio_station_contact_form); ?>">
    	</p>
		
		<?php 
	}
	
	// Updating widget replacing old instances with new
	public function update( $vw_radio_station_new_instance, $vw_radio_station_old_instance ) {
		$vw_radio_station_instance = array();	
		$vw_radio_station_instance['title'] = (!empty($vw_radio_station_new_instance['title']) ) ? strip_tags($vw_radio_station_new_instance['title']) : '';
		$vw_radio_station_instance['phone'] = (!empty($vw_radio_station_new_instance['phone']) ) ? vw_radio_station_sanitize_phone_number($vw_radio_station_new_instance['phone']) : '';
		$vw_radio_station_instance['email'] = (!empty($vw_radio_station_new_instance['email']) ) ? sanitize_email($vw_radio_station_new_instance['email']) : '';
		$vw_radio_station_instance['address'] = (!empty($vw_radio_station_new_instance['address']) ) ? strip_tags($vw_radio_station_new_instance['address']) : '';
		$vw_radio_station_instance['timing'] = (!empty($vw_radio_station_new_instance['timing']) ) ? strip_tags($vw_radio_station_new_instance['timing']) : '';
		$vw_radio_station_instance['longitude'] = (!empty($vw_radio_station_new_instance['longitude']) ) ? strip_tags($vw_radio_station_new_instance['longitude']) : '';
		$vw_radio_station_instance['latitude'] = (!empty($vw_radio_station_new_instance['latitude']) ) ? strip_tags($vw_radio_station_new_instance['latitude']) : '';
		$vw_radio_station_instance['contact_form'] = (!empty($vw_radio_station_new_instance['contact_form']) ) ? strip_tags($vw_radio_station_new_instance['contact_form']) : '';
        
		return $vw_radio_station_instance;
	}
}
// Register and load the widget
function vw_radio_station_contact_custom_load_widget() {
	register_widget( 'VW_Radio_Station_Contact_Widget' );
}
add_action( 'widgets_init', 'vw_radio_station_contact_custom_load_widget' );