<?php
/**
 * Posts with Featured Image widget.
 *
 * @package Nevo
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Posts with Featured Image widget.
 *
 * Class nevo_posts_list_featured_widget
 */
class nevo_posts_list_featured_widget extends Nevo_Widget {

	/**
	 * Constructor.
	 */
	public function __construct() {

		$this->widget_cssclass    = 'widget_posts_list_with_image';
		$this->widget_name        = esc_html__( '*Νevo* Posts with Featured Image', 'nevo' );
		$this->widget_description = esc_html__( 'Posts with thumbnail images', 'nevo' );
		
		$this->settings           = array(
			'title'         => array(
				'type'    => 'text',
				'default' => '',
				'label'   => esc_html__( 'Title:', 'nevo' ),
			),
			'layout'      => array(
				'type'    => 'image_select',
				'default' => 'content-52',
				'label'   => esc_html__( 'Layout:', 'nevo' ),
				'choices' => array(
					'content-9' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-41.png',
					'content-10' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-14.png',
					'content-50' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-49.png',
					'content-51' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-48.png',
					'content-52' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-522.png',
					'content-53' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-533.png',
					'content-54' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-45.png',
					'content-55' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-44.png',
					'content-56' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-23.png',
					'content-57' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-24.png',
					'content-58' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-11.png',
					'content-59' => esc_url( get_template_directory_uri() ) . '/assets/images/content/content-27.png',
				),
			),
			'number'      => array(
				'type'    => 'number',
				'default' => 5,
				'label'   => esc_html__( 'Number Posts to show:', 'nevo' ),
			),
			'all_categories' => array(
				'type'    => 'checkbox',
				'default' => 0,
				'label'   => esc_html__( 'Query from All Categories?', 'nevo' ),
			),
			'category'    => array(
				'type'    => 'categories',
				'default' => '',
				'label'   => esc_html__( 'Choose One Or More Specific Categories:', 'nevo' ),
				'description' => esc_html__( 'Hold down the Ctrl key to select One or More Categories.', 'nevo' ),
			),
			'show_cat'   => array(
				'type'    => 'checkbox',
				'col'     => 'col-1',
				'default' => 1,
				'label'   => esc_html__( 'Show Categories:', 'nevo' ),
			),
			'show_date'   => array(
				'type'    => 'checkbox',
				'col'     => 'col-2',
				'default' => 1,
				'label'   => esc_html__( 'Show Date:', 'nevo' ),
			),
			'orderby'       => array(
				'type'    => 'select',
				'col'     => 'col-1',
				'default' => 'post_date',
				'label'   => esc_html__( 'Sort By:', 'nevo' ),
				'choices' => array(
					'post_date' => esc_html__( 'Recent Published', 'nevo' ),
					'rand'      => esc_html__( 'Random', 'nevo' ),
				),
			),
			'image_width'       => array(
				'type'    => 'select',
				'col'     => 'col-2',
				'default' => 'cv-w-120',
				'label'   => esc_html__( 'Image Small Width:', 'nevo' ),
				'choices' => array(
					'cv-w-100' => esc_html__( 'Width 100px', 'nevo' ),
					'cv-w-120' => esc_html__( 'Width 120px', 'nevo' ),
				),
			),
			'image_size'       => array(
				'type'    => 'select',
				'default' => 'thumbnail',
				'label'   => esc_html__( 'Image Size:', 'nevo' ),
				'choices'     => array_merge(
					array(
						'' => __( 'Hidden Image', 'nevo' ),
					),
					nevo_blog_get_image_sizes()
				),
			),
			'separator1'      => array(
				'type'    => 'separator',
				'default' => 1,
			),
		);

		parent::__construct();

	}

	/**
	 * Output widget.
	 *
	 * @param array $args     Arguments.
	 * @param array $instance Widget instance.
	 *
	 * @see WP_Widget
	 */
	public function widget( $args, $instance ) {
		global $post;
		$cats = get_categories();
		$category_default = array();
		foreach ( $cats as $cat ) $category_default[] = $cat->term_id;
		$title    		= apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' );
		$layout   		= isset( $instance['layout'] ) ? $instance['layout'] : 'content-52';
		$number   		= empty( $instance['number'] ) ? 5 : $instance['number'];
		$all_categories = isset( $instance['all_categories'] ) ? (bool) $instance['all_categories'] : false;
		$category       = isset( $instance['category'] ) ? $instance['category'] : $category_default;
		$show_cat   	= isset( $instance['show_cat'] ) ? $instance['show_cat'] : 1;
		$show_date   	= isset( $instance['show_date'] ) ? $instance['show_date'] : 1;
		$orderby   		= isset( $instance['orderby'] ) ? $instance['orderby'] : 'post_date';
		$image_width   	= isset( $instance['image_width'] ) ? $instance['image_width'] : 'cv-w-120';
		$image_size   	= isset( $instance['image_size'] ) ? $instance['image_size'] : 'thumbnail';
		
		$this->widget_start( $args );
		$spacing = '';
		$data_num = '';
		// Displays the widget title.
		if ( ! empty( $title ) ) {
			echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
		}
		// Thêm id có thứ tự vào widget
		//$widget_id = $this->id;
		//$widget_id_with_order = 'custom-widget-id-' . $widget_id;
		$widget_number = $this->number;
		$widget_id_with_order = 'featured-post-' . $widget_number;
		
		$query = $this->query_posts( $number, $all_categories, $category, $orderby );
		
		$postcounter = 1;
		ob_start();
		
		echo '<div id="' . esc_attr( $widget_id_with_order ) . '" class="post-list-wrap cv-row">';
			while ( $query->have_posts() ) :
				$query->the_post();
				
				$post_class = 'nevo-list-item cv-col-12 position-relative ';
				$post_class .= $layout;
				$content_wrap_open = '';
				$content_wrap_close = '';
				$inside_article_open = '';
				$inside_article_close = '';
				$image_class = 'size-full cv-col-12';
				$aspect_ratio = 'aspect-ratio:4/3;';
				$title_class = 'title-small cv-fs-sm-6 title-lg-small';
				$display_cat = false;
				$header_cat = false;
				if ($number % 2 === 1 && $postcounter > 1 && $number >= 3) {
					$post_class = 'nevo-list-item cv-col-12 cv-col-md-6 cv-col-lg-12 position-relative ';
				} else {
					$post_class = 'nevo-list-item cv-col-12 position-relative ';
				}
				switch ($layout) {
					case 'content-9':
						if ( has_post_thumbnail() ) {
							$content_wrap_open = '<div class="block-content position-relative">';
							$content_wrap_close = '</div>';
						}
						if ($number % 2 == 0) {
							$post_class = 'nevo-list-item cv-col-12 cv-col-sm-6 cv-col-lg-12 position-relative ';
						} else {
							$post_class = 'nevo-list-item cv-col-12 position-relative ';
						}
						$image_wrap_class = 'aligncenter cv-mb-0 cv-col-12';
						$aspect_ratio = 'aspect-ratio:16/9;';
						$header_cat = $show_cat;
						break;
					case 'content-10':
						$inside_article_open = '<div class="triangle-marker inside--article">';
						$inside_article_close = '</div>';
						$image_size = false;
						$image_wrap_class = 'aligncenter cv-mb-0 cv-col-12';
						$aspect_ratio = 'aspect-ratio:16/9;';
						break;
					case 'content-50':
						$post_class .= ' cat-bottom';
						if ( $postcounter === 1 ) {
							$image_wrap_class = 'aligncenter cv-mb-3 cv-col-12';
							$aspect_ratio = 'aspect-ratio:16/9;';
							$display_cat = $show_cat;
							$title_class = 'cv-fs-6 cv-fs-sm-5 title-lg-small';
						} else {
							$image_wrap_class = 'alignleft cv-mb-0 cv-ms-0 cv-me-3 ' . $image_width;
						}
						if ( $postcounter > 1 && $image_width === 'cv-w-100' ) {
							$spacing = ' small-left';
						}
						elseif ( $postcounter > 1 && $image_width === 'cv-w-120' ) {
							$spacing = ' xsmall-left';
						}
						break;
					case 'content-51':
						$post_class .= ' cat-bottom';
						if ( $postcounter === 1 ) {
							$image_wrap_class = 'aligncenter cv-mb-3 cv-col-12';
							$aspect_ratio = 'aspect-ratio:16/9;';
							$display_cat = $show_cat;
							$title_class = 'cv-fs-6 cv-fs-sm-5 title-lg-small';
						} else {
							$image_wrap_class = 'alignright cv-mb-0 cv-me-0 cv-ms-3 ' . $image_width;
						}
						if ( $postcounter > 1 && $image_width === 'cv-w-100' ) {
							$spacing = ' small-right';
						}
						elseif ( $postcounter > 1 && $image_width === 'cv-w-120' ) {
							$spacing = ' xsmall-right';
						}
						break;
					case 'content-52':
						if ($number % 2 == 0) {
							$post_class = 'nevo-list-item cv-col-12 cv-col-sm-6 cv-col-lg-12 position-relative ';
						} else {
							$post_class = 'nevo-list-item cv-col-12 position-relative ';
						}
						$image_wrap_class = 'alignleft cv-mb-0 cv-me-3 ' . $image_width;
						if ( $postcounter > 1 && $image_width === 'cv-w-100' ) {
							$spacing = ' small-left';
						}
						elseif ( $postcounter > 1 && $image_width === 'cv-w-120' ) {
							$spacing = ' xsmall-left';
						}
						break;
					case 'content-53':
						if ($number % 2 == 0) {
							$post_class = 'nevo-list-item cv-col-12 cv-col-sm-6 cv-col-lg-12 position-relative ';
						} else {
							$post_class = 'nevo-list-item cv-col-12 position-relative ';
						}
						$image_wrap_class = 'alignright cv-mb-0 cv-ms-3 ' . $image_width;
						if ( $postcounter > 1 && $image_width === 'cv-w-100' ) {
							$spacing = ' small-right';
						}
						elseif ( $postcounter > 1 && $image_width === 'cv-w-120' ) {
							$spacing = ' xsmall-right';
						}
						break;
					case 'content-54':
						if ( $postcounter === 1 ) {
							$image_wrap_class = 'aligncenter cv-mb-0 cv-col-12';
							$inside_article_open = '<div class="box-wrap position-relative">';
							$inside_article_close = '</div>';
							$header_cat = $show_cat;
						} else {
							$image_wrap_class = 'alignleft cv-mb-0 cv-ms-0 cv-me-3 ' . $image_width;
						}
						if ( $postcounter > 1 && $image_width === 'cv-w-100' ) {
							$spacing = ' small-left';
						}
						elseif ( $postcounter > 1 && $image_width === 'cv-w-120' ) {
							$spacing = ' xsmall-left';
						}
						break;
					case 'content-55':
						if ( $postcounter === 1 ) {
							$image_wrap_class = 'aligncenter cv-mb-0 cv-col-12';
							$inside_article_open = '<div class="box-wrap position-relative">';
							$inside_article_close = '</div>';
							$header_cat = $show_cat;
							$title_class = 'cv-fs-6 cv-fs-sm-5 title-lg-small';
						} else {
							$image_wrap_class = 'alignright cv-mb-0 cv-me-0 cv-ms-3 ' . $image_width;
						}
						if ( $postcounter > 1 && $image_width === 'cv-w-100' ) {
							$spacing = ' small-right';
						}
						elseif ( $postcounter > 1 && $image_width === 'cv-w-120' ) {
							$spacing = ' xsmall-right';
						}
						break;
					case 'content-56':
						if ($number % 2 == 0) {
							$post_class = 'nevo-list-item cv-col-12 cv-col-sm-6 cv-col-lg-12 position-relative cat-bottom';
						} else {
							$post_class = 'nevo-list-item cv-col-12 position-relative cat-bottom';
						}
						$image_wrap_class = 'aligncenter cv-mb-3 cv-col-12';
						$aspect_ratio = 'aspect-ratio:16/9;';
						$display_cat = $show_cat;
						break;
					case 'content-57':
						$post_class = ' cat-bottom cv-col-12 cv-col-sm-6';
						$image_wrap_class = 'aligncenter cv-mb-3 cv-col-12';
						$aspect_ratio = 'aspect-ratio:4/3;';
						$display_cat = $show_cat;
						break;
					case 'content-58':
						$post_class .= ' cat-bottom';
						if ( $postcounter === 1 ) {
							$image_size = $image_size;
							$display_cat = $show_cat;
						} else {
							$inside_article_open = '<div class="triangle-marker inside--article">';
							$inside_article_close = '</div>';
							$image_size = false;
						}
						$image_wrap_class = 'aligncenter cv-mb-3 cv-col-12';
						$aspect_ratio = 'aspect-ratio:16/9;';
						break;
					case 'content-59':
						$post_class .= ' cat-bottom number-marker';
						if ( $postcounter === 1 ) {
							$image_size = $image_size;
							$display_cat = $show_cat;
							$title_class = 'cv-fs-6 cv-fs-sm-5 title-lg-small';
						} else {
							$image_size = false;
						}
						$image_wrap_class = 'aligncenter cv-mb-3 cv-col-12';
						$aspect_ratio = 'aspect-ratio:16/9;';
						$data_num = 'data-num="0' . htmlspecialchars($postcounter) . '"';
						break;
					default:
				}
				
				?>
				<article id="post-<?php echo $post->ID; ?>" class="<?php echo esc_attr( $post_class ); ?>" <?php nevo_do_microdata( 'article' ); ?>>
					<?php
					echo $inside_article_open;
					nevo_post_image($image_size, $image_wrap_class, $image_class, $aspect_ratio, $display_cat);
					echo $content_wrap_open;
					?>
					<header class="entry-header<?php echo esc_attr( $image_size ? $spacing : '' ); ?>">
						<?php
						$setting_values = array( 'categories' => $header_cat);
						nevo_display_entry_meta_items($setting_values);
						?>
						<h3 class="entry-title post-title text-line three-line <?php echo esc_attr( $title_class ); ?> cv-mb-1">
							<a class="title-post-list" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark" <?php echo $data_num; ?>><?php the_title(); ?></a>
						</h3>
						<?php if ( $show_date ) : ?>
							<div class="entry-meta"><?php echo nevo_do_post_meta_item( 'date' ); ?></div>
						<?php endif; ?>
					</header>
					<?php echo $content_wrap_close;
					echo $inside_article_close; ?>
				</article>
			<?php $postcounter ++;
			endwhile;
			
			// Reset Post Data.
			wp_reset_postdata();
		ob_end_flush();
		echo '</div>';
		
		$this->widget_end( $args );
	}

}