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

class Commercial_Automation_Services_Contact_Widget extends WP_Widget {
	function __construct() {
		parent::__construct(
			'Commercial_Automation_Services_Contact_Widget', 
			__('VW Contact us', 'commercial-automation-services'),
			array( 'description' => __( 'Widget for contact us section in sidebar', 'commercial-automation-services' ), ) 
		);
	}
	
	public function widget( $commercial_automation_services_args, $commercial_automation_services_instance ) {
		?>
		<aside class="widget">
			<?php
			$commercial_automation_services_title = isset( $commercial_automation_services_instance['title'] ) ? $commercial_automation_services_instance['title'] : '';
			$commercial_automation_services_phone = isset( $commercial_automation_services_instance['phone'] ) ? $commercial_automation_services_instance['phone'] : '';
			$commercial_automation_services_timing = isset( $commercial_automation_services_instance['timing'] ) ? $commercial_automation_services_instance['timing'] : '';
			$commercial_automation_services_email = isset( $commercial_automation_services_instance['email'] ) ? $commercial_automation_services_instance['email'] : '';
			$commercial_automation_services_address = isset( $commercial_automation_services_instance['address'] ) ? $commercial_automation_services_instance['address'] : '';
			$commercial_automation_services_longitude = isset( $commercial_automation_services_instance['longitude'] ) ? $commercial_automation_services_instance['longitude'] : '';
			$commercial_automation_services_latitude = isset( $commercial_automation_services_instance['latitude'] ) ? $commercial_automation_services_instance['latitude'] : '';
			$commercial_automation_services_contact_form = isset( $commercial_automation_services_instance['contact_form'] ) ? $commercial_automation_services_instance['contact_form'] : '';

	        echo '<div class="custom-contact-us">';
	        if(!empty($commercial_automation_services_title) ){ ?><h3 class="custom_title1"><?php echo esc_html($commercial_automation_services_title); ?></h3><?php } ?>
		        <?php if(!empty($commercial_automation_services_phone) ){ ?><p><span class="custom_details"><i class="fa-solid fa-phone me-2"></i></span><span class="custom_desc1"><?php echo esc_html($commercial_automation_services_phone); ?></span></p><?php } ?>
		        <?php if(!empty($commercial_automation_services_timing) ){ ?><p><span class="custom_details"><?php esc_html_e('Opening Time: ','commercial-automation-services'); ?></span><span class="custom_desc"><?php echo esc_html($commercial_automation_services_timing); ?></span></p><?php } ?>
		        <hr class="opening-time-footer">
		        <?php if(!empty($commercial_automation_services_email) ){ ?><p><span class="custom_details"><i class="fa-solid fa-envelope me-2"></i></span><span class="custom_desc"><?php echo esc_html($commercial_automation_services_email); ?></span></p><?php } ?>
		        <?php if(!empty($commercial_automation_services_address) ){ ?><p>
		        	<div class="d-flex gap-2 location-text">
			        	<span class="custom_details">
			        	<i class="fa-solid fa-location-dot me-2">
			        	</i></span>
			        	<span class="custom_desc"><?php echo esc_html($commercial_automation_services_address); ?></span>
		        	</div></p><?php } ?> 
		        <?php if(!empty($commercial_automation_services_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($commercial_automation_services_longitude); ?>,<?php echo esc_html($commercial_automation_services_latitude); ?>&hl=es;z=14&amp;output=embed"></embed><?php } ?>
		        <?php if(!empty($commercial_automation_services_contact_form) ){ ?><?php echo do_shortcode($commercial_automation_services_contact_form); ?><?php } ?>
	        <?php echo '</div>';
			?>
		</aside>
		<?php
	}
	
	// Widget Backend 
	public function form( $commercial_automation_services_instance ) {

		$commercial_automation_services_title= ''; $commercial_automation_services_phone= ''; $commercial_automation_services_email = ''; $commercial_automation_services_address = ''; $commercial_automation_services_timing = ''; $commercial_automation_services_longitude = ''; $commercial_automation_services_latitude = ''; $commercial_automation_services_contact_form = ''; 
		
		$commercial_automation_services_title = isset( $commercial_automation_services_instance['title'] ) ? $commercial_automation_services_instance['title'] : '';
		$commercial_automation_services_phone = isset( $commercial_automation_services_instance['phone'] ) ? $commercial_automation_services_instance['phone'] : '';
		$commercial_automation_services_timing = isset( $commercial_automation_services_instance['timing'] ) ? $commercial_automation_services_instance['timing'] : '';
		$commercial_automation_services_email = isset( $commercial_automation_services_instance['email'] ) ? $commercial_automation_services_instance['email'] : '';
		$commercial_automation_services_address = isset( $commercial_automation_services_instance['address'] ) ? $commercial_automation_services_instance['address'] : '';
		$commercial_automation_services_longitude = isset( $commercial_automation_services_instance['longitude'] ) ? $commercial_automation_services_instance['longitude'] : '';
		$commercial_automation_services_latitude = isset( $commercial_automation_services_instance['latitude'] ) ? $commercial_automation_services_instance['latitude'] : '';
		$commercial_automation_services_contact_form = isset( $commercial_automation_services_instance['contact_form'] ) ? $commercial_automation_services_instance['contact_form'] : '';
		
		?>

		<p>
        	<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','commercial-automation-services'); ?></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($commercial_automation_services_title); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('phone')); ?>"><?php esc_html_e('Phone Number:','commercial-automation-services'); ?></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($commercial_automation_services_phone); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('timing')); ?>"><?php esc_html_e('Opening Time:','commercial-automation-services'); ?></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($commercial_automation_services_timing); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('email')); ?>"><?php esc_html_e('Email id:','commercial-automation-services'); ?></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($commercial_automation_services_email); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('address')); ?>"><?php esc_html_e('Address:','commercial-automation-services'); ?></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($commercial_automation_services_address); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('longitude')); ?>"><?php esc_html_e('Longitude:','commercial-automation-services'); ?></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($commercial_automation_services_longitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('latitude')); ?>"><?php esc_html_e('Latitude:','commercial-automation-services'); ?></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($commercial_automation_services_latitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('contact_form')); ?>"><?php esc_html_e('Contact Form Shortcode:','commercial-automation-services'); ?></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($commercial_automation_services_contact_form); ?>">
    	</p>
		
		<?php 
	}
	
	// Updating widget replacing old instances with new
	public function update( $commercial_automation_services_new_instance, $commercial_automation_services_old_instance ) {
		$commercial_automation_services_instance = array();	
		$commercial_automation_services_instance['title'] = (!empty($commercial_automation_services_new_instance['title']) ) ? strip_tags($commercial_automation_services_new_instance['title']) : '';
		$commercial_automation_services_instance['phone'] = (!empty($commercial_automation_services_new_instance['phone']) ) ? commercial_automation_services_sanitize_phone_number($commercial_automation_services_new_instance['phone']) : '';
		$commercial_automation_services_instance['email'] = (!empty($commercial_automation_services_new_instance['email']) ) ? sanitize_email($commercial_automation_services_new_instance['email']) : '';
		$commercial_automation_services_instance['address'] = (!empty($commercial_automation_services_new_instance['address']) ) ? strip_tags($commercial_automation_services_new_instance['address']) : '';
		$commercial_automation_services_instance['timing'] = (!empty($commercial_automation_services_new_instance['timing']) ) ? strip_tags($commercial_automation_services_new_instance['timing']) : '';
		$commercial_automation_services_instance['longitude'] = (!empty($commercial_automation_services_new_instance['longitude']) ) ? strip_tags($commercial_automation_services_new_instance['longitude']) : '';
		$commercial_automation_services_instance['latitude'] = (!empty($commercial_automation_services_new_instance['latitude']) ) ? strip_tags($commercial_automation_services_new_instance['latitude']) : '';
		$commercial_automation_services_instance['contact_form'] = (!empty($commercial_automation_services_new_instance['contact_form']) ) ? strip_tags($commercial_automation_services_new_instance['contact_form']) : '';
        
		return $commercial_automation_services_instance;
	}
}
// Register and load the widget
function commercial_automation_services_contact_custom_load_widget() {
	register_widget( 'Commercial_Automation_Services_Contact_Widget' );
}
add_action( 'widgets_init', 'commercial_automation_services_contact_custom_load_widget' );