<?php
/**
 * Register Featured Post Section.
 *
 * @package hasium
 */

function hasium_action_featured_post_grid() {

	register_widget( 'hasium_featured_post_grid' );

}
add_action( 'widgets_init', 'hasium_action_featured_post_grid' );

class hasium_featured_post_grid extends WP_Widget{

	function __construct() {
		global $control_ops;
		$widget_ops = array(
			'classname'   => 'featured-posts',
			'description' => esc_html__( 'Add Widget to Display Featured Post.', 'hasium' )
		);
		parent::__construct( 'hasium_featured_post_grid',esc_html__( 'HS: Featured Grid', 'hasium' ), $widget_ops, $control_ops );
	}

	function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, array(
			'category'         	=> '',
		) );

		$category 			= isset( $instance['category'] ) ? absint( $instance['category'] ) : 0;
		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>">
				<?php esc_html_e( 'Post Category:', 'hasium' ); ?>
			</label>

			<?php
			wp_dropdown_categories(array(
				'show_option_none' => '',
				'class' 		  => 'widefat',
				'show_option_all'  => esc_html__('From Recent Post','hasium'),
				'name'             => esc_attr($this->get_field_name( 'category' )),
				'selected'         => absint( $category ),
			) );
			?>
		</p>

		<?php
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['category'] 			= absint( $new_instance['category'] );
		$instance['show_post_meta'] 	= (bool) $new_instance['show_post_meta'];

		return $instance;
	}

	function widget( $args, $instance ) {

		extract( $args );
		$category  			= isset( $instance[ 'category' ] ) ? $instance[ 'category' ] : 0;
		$featured_category  = isset( $instance[ 'featured_category' ] ) ? $instance[ 'featured_category' ] : 0;

		echo $before_widget; ?>

		<?php $slider_args = array(
			'posts_per_page' => 4,
			'post_type' => 'post',
			'post_status' => 'publish',
			'post__not_in' => get_option( 'sticky_posts' ),
		);

		if ( absint( $category ) > 0 ) {
			$slider_args['cat'] = absint( $category );
		}
		$the_query = new WP_Query( $slider_args );

		if ($the_query->have_posts()) : $count= 0; ?>
			<div class="col-lg-6">
				<div class="row">
					<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
						<div class="col-sm-6">
							<div class="slider-banner-right-4 <?php if($count > 1){echo 'mt-30';} ?>">
								<?php if ( has_post_thumbnail() ){ ?>
									<div class="slider-banner-image">
										<?php the_post_thumbnail( 'hasium-featured-post-grid' );?>
									</div>
								<?php } else { ?>
									<div class="slider-banner-image">
										<img src="<?php echo esc_url(get_template_directory_uri() );?>/assets/img/no-image-01.png);">
									</div>
								<?php } ?>
								<div class="slider-banner-content p-3">
									<span><?php if ( absint( $category ) > 0 ) {echo get_cat_name($category); } ?></span>
									<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
									<p><?php echo esc_html(get_the_time( get_option('date_format') )); ?>  -  <a href="<?php the_permalink(); ?>"><?php echo esc_html(get_the_time( get_option('date_format') )); ?></a></p>
								</div>
							</div> <!-- slider banner right -->
						</div>
						<?php $count++; endwhile; ?>
				</div>
			</div>
			<?php wp_reset_postdata(); ?>
		<?php endif;?>

		<?php echo $after_widget;

	}

}