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

class Archaeology_Museums_Contact_Widget extends WP_Widget {
	function __construct() {
		parent::__construct(
			'Archaeology_Museums_Contact_Widget', 
			__('VW Contact us', 'archaeology-museums'),
			array( 'description' => __( 'Widget for contact us section in sidebar', 'archaeology-museums' ), ) 
		);
	}
	
	public function widget( $archaeology_museums_args, $archaeology_museums_instance ) {
		?>
		<aside class="widget">
			<?php
			$archaeology_museums_title = isset( $archaeology_museums_instance['title'] ) ? $archaeology_museums_instance['title'] : '';
			$archaeology_museums_phone = isset( $archaeology_museums_instance['phone'] ) ? $archaeology_museums_instance['phone'] : '';
			$archaeology_museums_email = isset( $archaeology_museums_instance['email'] ) ? $archaeology_museums_instance['email'] : '';
			$archaeology_museums_address = isset( $archaeology_museums_instance['address'] ) ? $archaeology_museums_instance['address'] : '';
			$archaeology_museums_timing = isset( $archaeology_museums_instance['timing'] ) ? $archaeology_museums_instance['timing'] : '';
			$archaeology_museums_longitude = isset( $archaeology_museums_instance['longitude'] ) ? $archaeology_museums_instance['longitude'] : '';
			$archaeology_museums_latitude = isset( $archaeology_museums_instance['latitude'] ) ? $archaeology_museums_instance['latitude'] : '';
			$archaeology_museums_contact_form = isset( $archaeology_museums_instance['contact_form'] ) ? $archaeology_museums_instance['contact_form'] : '';

	        echo '<div class="custom-contact-us">';
	        if(!empty($archaeology_museums_title) ){ ?><h3 class="custom_title1"><?php echo esc_html($archaeology_museums_title); ?></h3><?php } ?>
		        <?php if(!empty($archaeology_museums_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', 'archaeology-museums'); ?></span><span class="custom_desc"><?php echo esc_html($archaeology_museums_phone); ?></span>
		        		</div>		        		
		        	</div>
		        <?php } ?>
		        <?php if(!empty($archaeology_museums_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', 'archaeology-museums'); ?></span><span class="custom_desc"><?php echo esc_html($archaeology_museums_email); ?></span>
		        		</div>
		        	</div>
		        <?php } ?>
		        <?php if(!empty($archaeology_museums_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', 'archaeology-museums'); ?></span><span class="custom_desc"><?php echo esc_html($archaeology_museums_address); ?></span>
			        	</div>
			        </div>
			    <?php } ?> 
		        <?php if(!empty($archaeology_museums_timing) ){ ?><p><span class="custom_details"><?php esc_html_e('Opening Time: ','archaeology-museums'); ?></span><span class="custom_desc"><?php echo esc_html($archaeology_museums_timing); ?></span></p><?php } ?>
		        <?php if(!empty($archaeology_museums_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($archaeology_museums_longitude); ?>,<?php echo esc_html($archaeology_museums_latitude); ?>&hl=es;z=14&amp;output=embed"></embed><?php } ?>
		        <?php if(!empty($archaeology_museums_contact_form) ){ ?><?php echo do_shortcode($archaeology_museums_contact_form); ?><?php } ?>
	        <?php echo '</div>';
			?>
		</aside>
		<?php
	}
	
	// Widget Backend 
	public function form( $archaeology_museums_instance ) {

		$archaeology_museums_title= ''; $archaeology_museums_phone= ''; $archaeology_museums_email = ''; $archaeology_museums_address = ''; $archaeology_museums_timing = ''; $archaeology_museums_longitude = ''; $archaeology_museums_latitude = ''; $archaeology_museums_contact_form = ''; 
		
		$archaeology_museums_title = isset( $archaeology_museums_instance['title'] ) ? $archaeology_museums_instance['title'] : '';
		$archaeology_museums_phone = isset( $archaeology_museums_instance['phone'] ) ? $archaeology_museums_instance['phone'] : '';
		$archaeology_museums_email = isset( $archaeology_museums_instance['email'] ) ? $archaeology_museums_instance['email'] : '';
		$archaeology_museums_address = isset( $archaeology_museums_instance['address'] ) ? $archaeology_museums_instance['address'] : '';
		$archaeology_museums_timing = isset( $archaeology_museums_instance['timing'] ) ? $archaeology_museums_instance['timing'] : '';
		$archaeology_museums_longitude = isset( $archaeology_museums_instance['longitude'] ) ? $archaeology_museums_instance['longitude'] : '';
		$archaeology_museums_latitude = isset( $archaeology_museums_instance['latitude'] ) ? $archaeology_museums_instance['latitude'] : '';
		$archaeology_museums_contact_form = isset( $archaeology_museums_instance['contact_form'] ) ? $archaeology_museums_instance['contact_form'] : '';
		
		?>

		<p>
        	<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','archaeology-museums'); ?></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($archaeology_museums_title); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('phone')); ?>"><?php esc_html_e('Phone Number:','archaeology-museums'); ?></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($archaeology_museums_phone); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('email')); ?>"><?php esc_html_e('Email id:','archaeology-museums'); ?></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($archaeology_museums_email); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('address')); ?>"><?php esc_html_e('Address:','archaeology-museums'); ?></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($archaeology_museums_address); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('timing')); ?>"><?php esc_html_e('Opening Time:','archaeology-museums'); ?></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($archaeology_museums_timing); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('longitude')); ?>"><?php esc_html_e('Longitude:','archaeology-museums'); ?></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($archaeology_museums_longitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('latitude')); ?>"><?php esc_html_e('Latitude:','archaeology-museums'); ?></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($archaeology_museums_latitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('contact_form')); ?>"><?php esc_html_e('Contact Form Shortcode:','archaeology-museums'); ?></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($archaeology_museums_contact_form); ?>">
    	</p>
		
		<?php 
	}
	
	// Updating widget replacing old instances with new
	public function update( $archaeology_museums_new_instance, $archaeology_museums_old_instance ) {
		$archaeology_museums_instance = array();	
		$archaeology_museums_instance['title'] = (!empty($archaeology_museums_new_instance['title']) ) ? strip_tags($archaeology_museums_new_instance['title']) : '';
		$archaeology_museums_instance['phone'] = (!empty($archaeology_museums_new_instance['phone']) ) ? archaeology_museums_sanitize_phone_number($archaeology_museums_new_instance['phone']) : '';
		$archaeology_museums_instance['email'] = (!empty($archaeology_museums_new_instance['email']) ) ? sanitize_email($archaeology_museums_new_instance['email']) : '';
		$archaeology_museums_instance['address'] = (!empty($archaeology_museums_new_instance['address']) ) ? strip_tags($archaeology_museums_new_instance['address']) : '';
		$archaeology_museums_instance['timing'] = (!empty($archaeology_museums_new_instance['timing']) ) ? strip_tags($archaeology_museums_new_instance['timing']) : '';
		$archaeology_museums_instance['longitude'] = (!empty($archaeology_museums_new_instance['longitude']) ) ? strip_tags($archaeology_museums_new_instance['longitude']) : '';
		$archaeology_museums_instance['latitude'] = (!empty($archaeology_museums_new_instance['latitude']) ) ? strip_tags($archaeology_museums_new_instance['latitude']) : '';
		$archaeology_museums_instance['contact_form'] = (!empty($archaeology_museums_new_instance['contact_form']) ) ? strip_tags($archaeology_museums_new_instance['contact_form']) : '';
        
		return $archaeology_museums_instance;
	}
}
// Register and load the widget
function archaeology_museums_contact_custom_load_widget() {
	register_widget( 'Archaeology_Museums_Contact_Widget' );
}
add_action( 'widgets_init', 'archaeology_museums_contact_custom_load_widget' );