<?php
/**
 * Template part for displaying Homepage Products Block 1 section
 *
 * @package Roven-Blog
 */

if ( is_plugin_active( 'woocommerce/woocommerce.php' ) && rovenblog_check_products() ) {
	$count_h    = get_theme_mod( 'rovenblog_home_header_hero4_posts_nr', 3 );
	$posts_type = get_theme_mod( 'rovenblog_home_header_hero4_content', 'recent' );

	if ( 'recent' === $posts_type ) {
		$args_hero = array(
			'ignore_sticky_posts' => true,
			'post_type'           => 'product',
			'order'               => 'desc',
			'posts_per_page'      => $count_h,
		);
	} elseif ( 'popular' === $posts_type ) {
		$args_hero = array(
			'ignore_sticky_posts' => true,
			'post_type'           => 'product',
			'meta_key'            => 'total_sales',
			'orderby'             => 'meta_value_num',
			'order'               => 'desc',
			'posts_per_page'      => $count_h,
		);
	} elseif ( 'discounted' === $posts_type ) {
		$args_hero = array(
			'ignore_sticky_posts' => true,
			'posts_per_page'      => $count_h,
			'post_type'           => 'product',
			'order'               => 'desc',
			'meta_query'          => array(
				array(
					'key'     => '_sale_price',
					'value'   => 0,
					'compare' => '>',
					'type'    => 'numeric',
				),
			),
		);
	} else {
		$specific = get_theme_mod( 'rovenblog_home_header_hero4_post_specific', false );

		if ( false === $specific || empty( $specific ) ) {
			$count = 0;
		} else {
			$count = count( $specific );
		}

		$args_hero = array(
			'ignore_sticky_posts' => true,
			'post_type'           => 'product',
			'order'               => 'desc',
			'post__in'            => $specific,
			'posts_per_page'      => $count,
		);
	}

	$query_posts = new WP_Query( $args_hero );

	if ( $query_posts->have_posts() ) {
		// Post Block 2 Customizer arguments.
		$section_title  = get_theme_mod( 'rovenblog_home_header_hero4_title', esc_html__( 'Latest products', 'roven-blog' ) );
		$style_type     = get_theme_mod( 'rovenblog_home_header_hero4_style', 'style3' );
		$thumbnail_type = get_theme_mod( 'rovenblog_home_header_hero4_aspect', 'landscape' );
		$price_enable   = get_theme_mod( 'rovenblog_home_header_hero4_price', true );
		$structure_type = get_theme_mod( 'rovenblog_home_header_hero4_type', 'grid' );
		$rb_fullwidth   = get_theme_mod( 'rovenblog_home_header_hero4_fullwidth', false );
		$grid_width     = intval( get_theme_mod( 'rovenblog_max_width_content', 1230 ) );


		// Determine the most suitable image size for the selected image aspect based on the number of columns and grid width.
		if ( 'grid' === $structure_type || 'masonry' === $structure_type ) {
			$nr_cols = intval( get_theme_mod( 'rovenblog_home_header_hero4_cols_nr', 3 ) );
			if ( 1 === $nr_cols && ( 1600 < $grid_width || true === $rb_fullwidth ) ) {
				$thumbnail_size = '-full';
			} elseif ( 1 === $nr_cols ) {
				$thumbnail_size = '-max';
			} elseif ( 2 === $nr_cols && ( 1400 < $grid_width || true === $rb_fullwidth ) ) {
				$thumbnail_size = '-mid';
			} else {
				$thumbnail_size = '-min';
			}

			$post_count = 0;
		} else {
			$nr_cols = intval( get_theme_mod( 'rovenblog_home_header_hero4_slide_nr', 1 ) );
			if ( 1 === $nr_cols && ( 1600 < $grid_width || true === $rb_fullwidth ) ) {
				$thumbnail_size = '-full';
			} elseif ( 1 === $nr_cols ) {
				$thumbnail_size = '-max';
			} else {
				$thumbnail_size = '-mid';
			}
		}

		if ( 'masonry' === $structure_type ) {
			$thumbnail_type = 'masonry';
		}

		global  $woocommerce;

		// Prepare post template arguments.
		$args = array(
			'price_enable'    => $price_enable,
			'thumbnail_type'  => $thumbnail_type,
			'thumbnail_limit' => $thumbnail_size,
			'headings'        => 'h3',
			'currency'        => get_woocommerce_currency_symbol(),
		);

		$sections  = get_theme_mod( 'rovenblog_home_layout', array( 'hero', 'featured', 'main' ) );
		$col_count = 0;
		?>
		<div id="rb-post-block-2" class="rb-section<?php echo ( get_theme_mod( 'rovenblog_home_header_hero4_fullwidth', false ) ) ? ' rb-stretch-fullwidth' : ''; ?>">

			<?php
			if ( '' !== $section_title ) {
				$title_style = get_theme_mod( 'rovenblog_section_title_style', 'none' );

				if ( 'none' !== $title_style ) {
					$title_class = ' rb-section-title-' . $title_style;
				} else {
					$title_class = '';
				}
				?>
				<div class="rb-section-title<?php echo esc_attr( $title_class ); ?>">
					<?php
					if ( 'style-6' === $title_style ) {
						echo '<h2>' . esc_html( $section_title ) . '</h2>';
						?>
						<svg width="33" height="6" xmlns="https://www.w3.org/2000/svg">
									<path d="M0,4c1.505,0,2.195-0.809,3.069-1.832C3.936,1.152,4.919,0,6.743,0c1.899,0,3.008,1.29,3.898,2.326
									C11.534,3.364,12.131,4,13.128,4c1.505,0,2.195-0.809,3.069-1.832C17.064,1.152,18.047,0,19.871,0c1.9,0,3.009,1.29,3.899,2.326
									C24.662,3.364,25.26,4,26.257,4c1.506,0,2.195-0.809,3.069-1.832C30.193,1.152,31.177,0,33,0v3.528
									c-0.859,0-1.354,0.327-2.152,0.906C29.884,5.131,28.685,6,26.257,6c-1.966,0-3.096-0.812-4.003-1.465
									c-0.869-0.624-1.448-1.007-2.383-1.007c-0.859,0-1.354,0.327-2.152,0.906C16.755,5.131,15.556,6,13.128,6
									c-1.966,0-3.096-0.812-4.004-1.465C8.256,3.91,7.677,3.528,6.743,3.528c-0.859,0-1.354,0.327-2.152,0.906C3.626,5.131,2.428,6,0,6V4
									z"/>
								</svg>
						<?php
					} elseif ( 'style-7' === $title_style || 'style-8' === $title_style || 'style-10' === $title_style ) {
						echo '<h2><span>' . esc_html( $section_title ) . '</span></h2>';
					} else {
						echo '<h2>' . esc_html( $section_title ) . '</h2>';
					}
					?>
				</div>
			<?php } ?>

			<div class="rb-section-content">

				<?php
				if ( 'masonry' === $structure_type ) {
					// Posts masonry layout.
					?>
					<div id="rb-posts-list" class="rb-masonry-grid cols-<?php echo $nr_cols; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>">

						<?php while ( $query_posts->have_posts() ) { ?>
							<div class="rb-masonry-grid-item">
								<?php
								$query_posts->the_post();

								// Don't add lozad lazy loading to the elements above the fold.
								if ( 'hero4' === $sections[0] ) {
									if ( ( $nr_cols + $nr_cols ) > $col_count ) {
										$args['lazy'] = 0;
									} else {
										$args['lazy'] = 1;
									}
									$col_count++;
								}

								// Post template based on style.
								get_template_part( 'template-parts/product-cards/product', $style_type, $args );
								?>
							</div>
						<?php } ?>

					</div><!-- end .rb-masonry-grid -->
					<?php
				} elseif ( 'grid' === $structure_type ) {
					// Header hero section grid mode.
					?>
					<div class="rb-grid mixed cols-<?php echo $nr_cols; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>">

						<?php while ( $query_posts->have_posts() ) { ?>
							<div class="rb-grid-item">
								<?php
								$query_posts->the_post();
								// Post template based on style.

								/**
								 * Assign a larger thumbnail for every 3rd post since it will be almost full width when browser
								 * width is smaller than 992px, unlike the other posts which will be split in 2 columns.
								 */
								if ( 3 === $nr_cols ) {
									$post_count++;
									if ( 3 === $post_count ) {
										$post_count = 0;

										$args['thumbnail_limit'] = '-mid';
									}
								}

								// Don't add lozad lazy loading to the elements above the fold.
								if ( 'hero4' === $sections[0] ) {
									if ( ( $nr_cols + $nr_cols ) > $col_count ) {
										$args['lazy'] = 0;
									} else {
										$args['lazy'] = 1;
									}
									$col_count++;
								}

								get_template_part( 'template-parts/product-cards/product', $style_type, $args );
								?>
							</div>
						<?php } ?>

					</div><!-- end .rb-grid -->
					<?php
				} else {
					// Header hero section slider mode.
					?>
					<div class="rb-slider cols-<?php echo $nr_cols; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>">	

						<div class="rb-slider-content">

							<ul class="rb-slider-slides" data-slick='{"autoplay": false, "infinite": true }'>
								<?php while ( $query_posts->have_posts() ) { ?>
									<li>
										<?php
										$query_posts->the_post();

										// Don't add lozad lazy loading to the elements above the fold.
										if ( 'hero4' === $sections[0] ) {
											$args['lazy'] = 0;
										}

										// Post template based on style.
										get_template_part( 'template-parts/product-cards/product', $style_type, $args );
										?>
									</li>
								<?php } ?>
							</ul>

							<div class="rb-slider-arrows"></div>

						</div><!-- end .rb-slider-content -->

						<div class="rb-slider-pager"></div>

					</div><!-- end .rb-slider -->
				<?php } ?>		
			</div><!-- end .rb-section-content -->

			<?php
			if ( get_theme_mod( 'rovenblog_home_header_hero4_rm_toggle', false ) ) {
				?>
				<div class="rb-section-readmore rb-text-align-<?php echo esc_attr( get_theme_mod( 'rovenblog_home_header_hero4_rm_align', 'center' ) ); ?>">

					<a href="<?php echo esc_url( get_theme_mod( 'rovenblog_home_header_hero4_rm_link', '#' ) ); ?>"<?php echo ( get_theme_mod( 'rovenblog_home_header_hero4_rm_tab', false ) ) ? ' target="_blank"' : ''; ?>>
						<?php echo esc_html( get_theme_mod( 'rovenblog_home_header_hero4_rm_text', esc_html__( 'View all posts', 'roven-blog' ) ) ); ?>
					</a>

				</div><!-- end .rb-section-title -->
				<?php
			}
			?>
		</div><!-- end #rb-header-hero -->
		<?php
		wp_reset_postdata();
	}
}
