<?php
if ( ! get_theme_mod( 'zenith_blog_enable_banner_section', false ) ) {
	return;
}

$slider_content_ids  = array();
$slider_content_type = get_theme_mod( 'zenith_blog_banner_slider_content_type', 'post' );

if ( $slider_content_type === 'post' ) {
	for ( $i = 1; $i <= 5; $i++ ) {
		$slider_content_ids[] = get_theme_mod( 'zenith_blog_banner_slider_content_post_' . $i );
	}
	$banner_slider_args = array(
		'post_type'           => $slider_content_type,
		'posts_per_page'      => absint( 5 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $slider_content_ids ) ) ) {
		$banner_slider_args['post__in'] = array_filter( $slider_content_ids );
		$banner_slider_args['orderby']  = 'post__in';
	} else {
		$banner_slider_args['orderby'] = 'date';
	}
} else {
	$cat_content_id     = get_theme_mod( 'zenith_blog_banner_slider_content_category' );
	$banner_slider_args = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 5 ),
	);
}

$banner_slider_args = apply_filters( 'zenith_blog_banner_section_args', $banner_slider_args );

zenith_blog_render_banner_section( $banner_slider_args );

/**
 * Render Banner Section.
 */
function zenith_blog_render_banner_section( $banner_slider_args ) {
	?>
	<section id="zenith_blog_banner_section" class="banner-section banner-style-2">
		<?php
		if ( is_customize_preview() ) :
			zenith_blog_section_link( 'zenith_blog_banner_section' );
		endif;
		?>
		<div class="ascendoor-wrapper">
			<div class="banner-section-wrapper">
				<?php
				$banner_query = new WP_Query( $banner_slider_args );
				if ( $banner_query->have_posts() ) :
					?>
					<div class="zenith-blog-slider-wrapper banner-slider zenith-blog-carousel-slider-navigation">
						<?php
						while ( $banner_query->have_posts() ) :
							$banner_query->the_post();
							?>
							<div class="carousel-item">
								<div class="blog-post-single tile-design <?php echo esc_attr( has_post_thumbnail() ? 'has-post-thumbnail' : '' ); ?>">
									<a class="post-thumbnail" href="<?php the_permalink(); ?>">
										<?php the_post_thumbnail( 'post-thumbnail' ); ?>
									</a>
									<div class="blog-detail">
										<div class="mag-post-category with-background">
											<?php zenith_blog_categories_list(); ?>
										</div>
										<h3 class="mag-post-title">
											<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
										</h3>
										<div class="mag-post-meta">
											<?php
											zenith_blog_posted_by();
											zenith_blog_posted_on();
											?>
											<span class="post-views">
												<i class="far fa-eye"></i>
												<?php echo getPostViews( get_the_ID() ); ?>
											</span>
										</div>
									</div>
								</div>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
					<?php
				endif;
				?>

				<div class="banner-section-thumb-wrapper">
					<div class="zenith-blog-thumb-slider-wrapper">
						<?php
						while ( $banner_query->have_posts() ) :
							$banner_query->the_post();
							?>
							<div class="carousel-thumb-item">
								<div class="blog-post-single list-design <?php echo esc_attr( has_post_thumbnail() ? 'has-post-thumbnail' : '' ); ?>">
									<?php if ( has_post_thumbnail() ) { ?>
										<div class="blog-img">
											<?php the_post_thumbnail( 'post-thumbnail' ); ?>
										</div>
										<div class="blog-details">
										<?php } ?>
										<h3 class="mag-post-title">
											<?php the_title(); ?>
										</h3>
										<?php if ( has_post_thumbnail() ) { ?>	
										</div>
									<?php } ?>
								</div>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
			</div>
		</div>

	</section>

	<?php
}
