<?php 
/**
* ----------------------------------------------------------------------------------------
*    Featured Deal Companies Widget
* ----------------------------------------------------------------------------------------
*/
Class SSD_Widget_Featured_Deal_Companies extends WP_Widget {

	public function __construct() {
		$widget_ops = array( 'description' => esc_html__('This widget adds selected deal companies to your site.','couponhut') );
		parent::__construct( 'featured_deal_companies', esc_html__('[CouponHut] Featured Deal Companies', 'couponhut'), $widget_ops );
	}

	public function widget( $args, $instance ) {

		extract($args);

		$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);

		echo $before_widget;

		$bgimage = $instance['bgimage'];
		$featured_deal_companies = $instance['featured_deal_companies'];

		if ( $bgimage ) :?>
			<?php
			$bgimage_array = wp_get_attachment_image_src($bgimage, 'ssd_widget-bgimage');
			$bgimage_url = $bgimage_array[0];	
			?>
			<div class="widget-bgimage" data-bgimage="<?php echo esc_url($bgimage_url) ?>"></div>
			<div class="overlay-dark"></div>
		<?php else: ?>
			<div class="overlay-color"></div>
		<?php endif;

		if ($bgimage ) {
			echo '<div class="deal-companies-widget-wrapper widget-list widget-has-bgimage">';
		} else {
			echo '<div class="deal-companies-widget-wrapper widget-list">';
		}
		
		if ( $title ) { echo $before_title . $title . $after_title; }

		echo '<div class="widget-deal-companies-content">';



		if ( ! empty( $featured_deal_companies ) && taxonomy_exists('deal_company') ){

			$featured_deal_companies_ids = explode(',', $featured_deal_companies);

			echo '<ul>';
		
			foreach ( $featured_deal_companies_ids as $id ) {
				$company_term =  get_term( $id, 'deal_company' );
				$category_link = get_term_link( $company_term );
				echo '<li><a href="' . esc_url( $category_link ) . '"><span class="widget-list-number-counter">' . $company_term->count . '</span>' . $company_term->name . '</a></li>';
			}

			echo '</ul>';

		}

		echo '</div> <!-- end widget-deal-companies-content -->';

		echo '</div><!-- end deal-companies-widget-wrapper -->';

		echo $after_widget;

	}

	public function update( $new_instance, $old_instance ) {
		
		$instance = $old_instance;


		$instance['title'] = strip_tags($new_instance['title']);
		$instance['bgimage'] = strip_tags($new_instance['bgimage']);
		$instance['featured_deal_companies'] = strip_tags($new_instance['featured_deal_companies']);


		return $instance;
	}

	public function form( $instance ) {
		
		$defaults = array(
			'title' => esc_html__('Deal Companies', 'couponhut'),
			'bgimage' => '',
			'featured_deal_companies' => ''
		);

		$instance = wp_parse_args((array) $instance, $defaults);

		?>
		<p>
			<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title', 'couponhut') ?>:</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($instance['title']); ?>" />
		</p>
		<p>
			<?php 
			$deal_companies = get_terms('deal_company', array( 
				'hide_empty' => 0
			));

			if ( ! empty( $deal_companies ) && ! is_wp_error( $deal_companies ) ) {

				$companies_array = array();

				foreach ( $deal_companies as $deal_company ) {
					$companies_array[$deal_company->term_id] = $deal_company->name;
				}
			}
			?>
			<?php 
			if ( class_exists('SSD_Widget_Fields') ) {
				$args = array(
					'id' =>  $this->get_field_id('featured_deal_companies'),
					'name' => $this-> get_field_name('featured_deal_companies'),
					'value' => $instance['featured_deal_companies'],
					'type' => 'multi-select',
					'label' =>  esc_html__( 'Featured Deal Companies', 'couponhut' ),
					'choices' => $companies_array
				);
				SSD_Widget_Fields::field($args);
			} ?>
        </p>
		<p>
			<?php if ( class_exists('SSD_Widget_Fields') ) {
				$args = array(
					'id' =>  $this->get_field_id('bgimage'),
					'name' => $this-> get_field_name('bgimage'),
					'value' => $instance['bgimage'],
					'type' => 'image',
					'label' =>  __( 'Background Image', 'couponhut' ),
				);
				SSD_Widget_Fields::field($args);
			} ?>
        </p>
		
	<?php
	}

	
}
?>