<?php
class Custom_Live_Chat extends WP_Widget {
	function Custom_Live_Chat() {
		$widget_ops = array('classname' => 'Custom_Live_Chat', 'description' => 'The Live Chat Site' );
		$this->WP_Widget('Custom_Live_Chat', THEMESTUDIO_THEME_NAME.' Live Chat', $widget_ops);
	}

	function widget($args, $instance) {
		extract($args, EXTR_SKIP);

		echo $before_widget;
		$title 		= empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
		$link_image = empty($instance['link_image']) ? '' : apply_filters('widget_link_image', $instance['link_image']);
		$title_head	= empty($instance['title_head']) ? '' : apply_filters('widget_title_head', $instance['title_head']);
		$desc 	 	= empty($instance['desc']) ? '' : apply_filters('widget_desc', $instance['desc']);
		$link 		= empty($instance['link']) ? '' : apply_filters('widget_link', $instance['link']);
		
		if($title){ 
			echo $before_title.$title.$after_title; 
		}
		
		?>
		
		<div class="live_chat">
            <?php if ($link_image): ?>
            	<a href="<?php echo $link ?>"><img src="<?php echo $link_image; ?>" alt="" /></a>
            <?php endif ?>
            <?php if ($title_head || $desc): ?>
            	<h2><a href="<?php echo $link ?>"><?php if($title_head){ echo $title_head; } ?> <?php if($desc){ echo '<i>'.$desc.'</i>'; } ?></a></h2>
            <?php endif ?>
        </div>
			
		<?php

		
		echo $after_widget;
		
	}

	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] 		= strip_tags($new_instance['title']);
		$instance['link_image'] = strip_tags($new_instance['link_image']);
		$instance['title_head'] = strip_tags($new_instance['title_head']);
		$instance['desc'] 		= strip_tags($new_instance['desc']);
		$instance['link'] 		= strip_tags($new_instance['link']);

		return $instance;
	}

	function form($instance) {
		$instance 	= wp_parse_args( (array) $instance, array('title'=>'Live Chat', 'link_image' => get_template_directory_uri().'/assets/images/chat-icon.png', 'desc' => 'with our agents', 'title_head' => 'live chat', 'link' => '#'));
		$title 		= strip_tags($instance['title']);
		$link_image = strip_tags($instance['link_image']);
		$title_head = strip_tags($instance['title_head']);
		$desc 		= strip_tags($instance['desc']);
		$link 		= strip_tags($instance['link']);

?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>">
				Widget title: 
				<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
			</label>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('link_image'); ?>">
				Link Image: 
				<input class="widefat" id="<?php echo $this->get_field_id('link_image'); ?>" name="<?php echo $this->get_field_name('link_image'); ?>" type="text" value="<?php echo esc_attr($link_image); ?>" />
			</label>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('title_head'); ?>">
				Title head: 
				<input class="widefat" id="<?php echo $this->get_field_id('title_head'); ?>" name="<?php echo $this->get_field_name('title_head'); ?>" type="text" value="<?php echo esc_attr($title_head); ?>" />
			</label>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('desc'); ?>">
				Description: 
				<input class="widefat" id="<?php echo $this->get_field_id('desc'); ?>" name="<?php echo $this->get_field_name('desc'); ?>" type="text" value="<?php echo esc_attr($desc); ?>" />
			</label>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('link'); ?>">
				Link: 
				<input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo esc_attr($link); ?>" />
			</label>
		</p>
<?php
	}
}

register_widget('Custom_Live_Chat');
?>