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

class Chartered_Accountant_Contact_Widget extends WP_Widget {
	function __construct() {
		parent::__construct(
			'Chartered_Accountant_Contact_Widget', 
			__('VW Contact us', 'chartered-accountant'),
			array( 'description' => __( 'Widget for contact us section in sidebar', 'chartered-accountant' ), ) 
		);
	}
	
	public function widget( $chartered_accountant_args, $chartered_accountant_instance ) {
		?>
		<aside class="widget">
			<?php
			$chartered_accountant_title = isset( $chartered_accountant_instance['title'] ) ? $chartered_accountant_instance['title'] : '';
			$chartered_accountant_phone = isset( $chartered_accountant_instance['phone'] ) ? $chartered_accountant_instance['phone'] : '';
			$chartered_accountant_email = isset( $chartered_accountant_instance['email'] ) ? $chartered_accountant_instance['email'] : '';
			$chartered_accountant_address = isset( $chartered_accountant_instance['address'] ) ? $chartered_accountant_instance['address'] : '';
			$chartered_accountant_timing = isset( $chartered_accountant_instance['timing'] ) ? $chartered_accountant_instance['timing'] : '';
			$chartered_accountant_longitude = isset( $chartered_accountant_instance['longitude'] ) ? $chartered_accountant_instance['longitude'] : '';
			$chartered_accountant_latitude = isset( $chartered_accountant_instance['latitude'] ) ? $chartered_accountant_instance['latitude'] : '';
			$chartered_accountant_contact_form = isset( $chartered_accountant_instance['contact_form'] ) ? $chartered_accountant_instance['contact_form'] : '';

	        echo '<div class="custom-contact-us">';
	        if(!empty($chartered_accountant_title) ){ ?><h3 class="custom_title1"><?php echo esc_html($chartered_accountant_title); ?></h3><?php } ?>
		        <?php if(!empty($chartered_accountant_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', 'chartered-accountant'); ?></span><span class="custom_desc"><?php echo esc_html($chartered_accountant_phone); ?></span>
		        		</div>		        		
		        	</div>
		        <?php } ?>
		        <?php if(!empty($chartered_accountant_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', 'chartered-accountant'); ?></span><span class="custom_desc"><?php echo esc_html($chartered_accountant_email); ?></span>
		        		</div>
		        	</div>
		        <?php } ?>
		        <?php if(!empty($chartered_accountant_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', 'chartered-accountant'); ?></span><span class="custom_desc"><?php echo esc_html($chartered_accountant_address); ?></span>
			        	</div>
			        </div>
			    <?php } ?> 
		        <?php if(!empty($chartered_accountant_timing) ){ ?><p><span class="custom_details"><?php esc_html_e('Opening Time: ','chartered-accountant'); ?></span><span class="custom_desc"><?php echo esc_html($chartered_accountant_timing); ?></span></p><?php } ?>
		        <?php if(!empty($chartered_accountant_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($chartered_accountant_longitude); ?>,<?php echo esc_html($chartered_accountant_latitude); ?>&hl=es;z=14&amp;output=embed"></embed><?php } ?>
		        <?php if(!empty($chartered_accountant_contact_form) ){ ?><?php echo do_shortcode($chartered_accountant_contact_form); ?><?php } ?>
	        <?php echo '</div>';
			?>
		</aside>
		<?php
	}
	
	// Widget Backend 
	public function form( $chartered_accountant_instance ) {

		$chartered_accountant_title= ''; $chartered_accountant_phone= ''; $chartered_accountant_email = ''; $chartered_accountant_address = ''; $chartered_accountant_timing = ''; $chartered_accountant_longitude = ''; $chartered_accountant_latitude = ''; $chartered_accountant_contact_form = ''; 
		
		$chartered_accountant_title = isset( $chartered_accountant_instance['title'] ) ? $chartered_accountant_instance['title'] : '';
		$chartered_accountant_phone = isset( $chartered_accountant_instance['phone'] ) ? $chartered_accountant_instance['phone'] : '';
		$chartered_accountant_email = isset( $chartered_accountant_instance['email'] ) ? $chartered_accountant_instance['email'] : '';
		$chartered_accountant_address = isset( $chartered_accountant_instance['address'] ) ? $chartered_accountant_instance['address'] : '';
		$chartered_accountant_timing = isset( $chartered_accountant_instance['timing'] ) ? $chartered_accountant_instance['timing'] : '';
		$chartered_accountant_longitude = isset( $chartered_accountant_instance['longitude'] ) ? $chartered_accountant_instance['longitude'] : '';
		$chartered_accountant_latitude = isset( $chartered_accountant_instance['latitude'] ) ? $chartered_accountant_instance['latitude'] : '';
		$chartered_accountant_contact_form = isset( $chartered_accountant_instance['contact_form'] ) ? $chartered_accountant_instance['contact_form'] : '';
		
		?>

		<p>
        	<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','chartered-accountant'); ?></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($chartered_accountant_title); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('phone')); ?>"><?php esc_html_e('Phone Number:','chartered-accountant'); ?></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($chartered_accountant_phone); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('email')); ?>"><?php esc_html_e('Email id:','chartered-accountant'); ?></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($chartered_accountant_email); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('address')); ?>"><?php esc_html_e('Address:','chartered-accountant'); ?></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($chartered_accountant_address); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('timing')); ?>"><?php esc_html_e('Opening Time:','chartered-accountant'); ?></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($chartered_accountant_timing); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('longitude')); ?>"><?php esc_html_e('Longitude:','chartered-accountant'); ?></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($chartered_accountant_longitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('latitude')); ?>"><?php esc_html_e('Latitude:','chartered-accountant'); ?></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($chartered_accountant_latitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('contact_form')); ?>"><?php esc_html_e('Contact Form Shortcode:','chartered-accountant'); ?></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($chartered_accountant_contact_form); ?>">
    	</p>
		
		<?php 
	}
	
	// Updating widget replacing old instances with new
	public function update( $chartered_accountant_new_instance, $chartered_accountant_old_instance ) {
		$chartered_accountant_instance = array();	
		$chartered_accountant_instance['title'] = (!empty($chartered_accountant_new_instance['title']) ) ? strip_tags($chartered_accountant_new_instance['title']) : '';
		$chartered_accountant_instance['phone'] = (!empty($chartered_accountant_new_instance['phone']) ) ? chartered_accountant_sanitize_phone_number($chartered_accountant_new_instance['phone']) : '';
		$chartered_accountant_instance['email'] = (!empty($chartered_accountant_new_instance['email']) ) ? sanitize_email($chartered_accountant_new_instance['email']) : '';
		$chartered_accountant_instance['address'] = (!empty($chartered_accountant_new_instance['address']) ) ? strip_tags($chartered_accountant_new_instance['address']) : '';
		$chartered_accountant_instance['timing'] = (!empty($chartered_accountant_new_instance['timing']) ) ? strip_tags($chartered_accountant_new_instance['timing']) : '';
		$chartered_accountant_instance['longitude'] = (!empty($chartered_accountant_new_instance['longitude']) ) ? strip_tags($chartered_accountant_new_instance['longitude']) : '';
		$chartered_accountant_instance['latitude'] = (!empty($chartered_accountant_new_instance['latitude']) ) ? strip_tags($chartered_accountant_new_instance['latitude']) : '';
		$chartered_accountant_instance['contact_form'] = (!empty($chartered_accountant_new_instance['contact_form']) ) ? strip_tags($chartered_accountant_new_instance['contact_form']) : '';
        
		return $chartered_accountant_instance;
	}
}
// Register and load the widget
function chartered_accountant_contact_custom_load_widget() {
	register_widget( 'Chartered_Accountant_Contact_Widget' );
}
add_action( 'widgets_init', 'chartered_accountant_contact_custom_load_widget' );