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

class Reseller_Store_Contact_Widget extends WP_Widget {
	function __construct() {
		parent::__construct(
			'Reseller_Store_Contact_Widget', 
			__('VW Contact us', 'reseller-store'),
			array( 'description' => __( 'Widget for contact us section in sidebar', 'reseller-store' ), ) 
		);
	}
	
	public function widget( $reseller_store_args, $reseller_store_instance ) {
		?>
		<aside class="widget">
			<?php
			$reseller_store_title = isset( $reseller_store_instance['title'] ) ? $reseller_store_instance['title'] : '';
			$reseller_store_phone = isset( $reseller_store_instance['phone'] ) ? $reseller_store_instance['phone'] : '';
			$reseller_store_email = isset( $reseller_store_instance['email'] ) ? $reseller_store_instance['email'] : '';
			$reseller_store_address = isset( $reseller_store_instance['address'] ) ? $reseller_store_instance['address'] : '';
			$reseller_store_timing = isset( $reseller_store_instance['timing'] ) ? $reseller_store_instance['timing'] : '';
			$reseller_store_longitude = isset( $reseller_store_instance['longitude'] ) ? $reseller_store_instance['longitude'] : '';
			$reseller_store_latitude = isset( $reseller_store_instance['latitude'] ) ? $reseller_store_instance['latitude'] : '';
			$reseller_store_contact_form = isset( $reseller_store_instance['contact_form'] ) ? $reseller_store_instance['contact_form'] : '';

	        echo '<div class="custom-contact-us">';
	        if(!empty($reseller_store_title) ){ ?><h3 class="custom_title1"><?php echo esc_html($reseller_store_title); ?></h3><?php } ?>
		        <?php if(!empty($reseller_store_phone) ){ ?><p><span class="custom_details"><i class="fa-solid fa-phone me-2"></i></span><span class="custom_desc"><?php echo esc_html($reseller_store_phone); ?></span></p><?php } ?>
		        <?php if(!empty($reseller_store_email) ){ ?><p><span class="custom_details"><i class="fa-solid fa-envelope me-2"></i></span><span class="custom_desc"><?php echo esc_html($reseller_store_email); ?></span></p><?php } ?>
		        <?php if(!empty($reseller_store_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($reseller_store_address); ?></span>
			        	</div></p><?php } ?> 
		        <?php if(!empty($reseller_store_timing) ){ ?><p><span class="custom_details"><?php esc_html_e('Opening Time: ','reseller-store'); ?></span><span class="custom_desc"><?php echo esc_html($reseller_store_timing); ?></span></p><?php } ?>
		        <?php if(!empty($reseller_store_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($reseller_store_longitude); ?>,<?php echo esc_html($reseller_store_latitude); ?>&hl=es;z=14&amp;output=embed"></embed><?php } ?>
		        <?php if(!empty($reseller_store_contact_form) ){ ?><?php echo do_shortcode($reseller_store_contact_form); ?><?php } ?>
	        <?php echo '</div>';
			?>
		</aside>
		<?php
	}
	
	// Widget Backend 
	public function form( $reseller_store_instance ) {

		$reseller_store_title= ''; $reseller_store_phone= ''; $reseller_store_email = ''; $reseller_store_address = ''; $reseller_store_timing = ''; $reseller_store_longitude = ''; $reseller_store_latitude = ''; $reseller_store_contact_form = ''; 
		
		$reseller_store_title = isset( $reseller_store_instance['title'] ) ? $reseller_store_instance['title'] : '';
		$reseller_store_phone = isset( $reseller_store_instance['phone'] ) ? $reseller_store_instance['phone'] : '';
		$reseller_store_email = isset( $reseller_store_instance['email'] ) ? $reseller_store_instance['email'] : '';
		$reseller_store_address = isset( $reseller_store_instance['address'] ) ? $reseller_store_instance['address'] : '';
		$reseller_store_timing = isset( $reseller_store_instance['timing'] ) ? $reseller_store_instance['timing'] : '';
		$reseller_store_longitude = isset( $reseller_store_instance['longitude'] ) ? $reseller_store_instance['longitude'] : '';
		$reseller_store_latitude = isset( $reseller_store_instance['latitude'] ) ? $reseller_store_instance['latitude'] : '';
		$reseller_store_contact_form = isset( $reseller_store_instance['contact_form'] ) ? $reseller_store_instance['contact_form'] : '';
		
		?>

		<p>
        	<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','reseller-store'); ?></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($reseller_store_title); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('phone')); ?>"><?php esc_html_e('Phone Number:','reseller-store'); ?></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($reseller_store_phone); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('email')); ?>"><?php esc_html_e('Email id:','reseller-store'); ?></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($reseller_store_email); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('address')); ?>"><?php esc_html_e('Address:','reseller-store'); ?></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($reseller_store_address); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('timing')); ?>"><?php esc_html_e('Opening Time:','reseller-store'); ?></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($reseller_store_timing); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('longitude')); ?>"><?php esc_html_e('Longitude:','reseller-store'); ?></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($reseller_store_longitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('latitude')); ?>"><?php esc_html_e('Latitude:','reseller-store'); ?></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($reseller_store_latitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('contact_form')); ?>"><?php esc_html_e('Contact Form Shortcode:','reseller-store'); ?></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($reseller_store_contact_form); ?>">
    	</p>
		
		<?php 
	}
	
	// Updating widget replacing old instances with new
	public function update( $reseller_store_new_instance, $reseller_store_old_instance ) {
		$reseller_store_instance = array();	
		$reseller_store_instance['title'] = (!empty($reseller_store_new_instance['title']) ) ? strip_tags($reseller_store_new_instance['title']) : '';
		$reseller_store_instance['phone'] = (!empty($reseller_store_new_instance['phone']) ) ? reseller_store_sanitize_phone_number($reseller_store_new_instance['phone']) : '';
		$reseller_store_instance['email'] = (!empty($reseller_store_new_instance['email']) ) ? sanitize_email($reseller_store_new_instance['email']) : '';
		$reseller_store_instance['address'] = (!empty($reseller_store_new_instance['address']) ) ? strip_tags($reseller_store_new_instance['address']) : '';
		$reseller_store_instance['timing'] = (!empty($reseller_store_new_instance['timing']) ) ? strip_tags($reseller_store_new_instance['timing']) : '';
		$reseller_store_instance['longitude'] = (!empty($reseller_store_new_instance['longitude']) ) ? strip_tags($reseller_store_new_instance['longitude']) : '';
		$reseller_store_instance['latitude'] = (!empty($reseller_store_new_instance['latitude']) ) ? strip_tags($reseller_store_new_instance['latitude']) : '';
		$reseller_store_instance['contact_form'] = (!empty($reseller_store_new_instance['contact_form']) ) ? strip_tags($reseller_store_new_instance['contact_form']) : '';
        
		return $reseller_store_instance;
	}
}
// Register and load the widget
function reseller_store_contact_custom_load_widget() {
	register_widget( 'Reseller_Store_Contact_Widget' );
}
add_action( 'widgets_init', 'reseller_store_contact_custom_load_widget' );