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

$mindful_blog_slider_content_ids  = array();
$mindful_blog_slider_content_type = get_theme_mod( 'mindful_blog_banner_slider_content_type', 'post' );

if ( $mindful_blog_slider_content_type === 'post' ) {
	for ( $mindful_blog_i = 1; $mindful_blog_i <= 3; $mindful_blog_i++ ) {
		$mindful_blog_slider_content_ids[] = get_theme_mod( 'mindful_blog_banner_slider_content_post_' . $mindful_blog_i );
	}
	$mindful_blog_banner_slider_args = array(
		'post_type'           => 'post',
		'post__in'            => array_filter( $mindful_blog_slider_content_ids ),
		'orderby'             => 'post__in',
		'posts_per_page'      => absint( 3 ),
		'ignore_sticky_posts' => true,
	);

	if ( ! empty( array_filter( $mindful_blog_slider_content_ids ) ) ) {
		$mindful_blog_banner_slider_args['post__in'] = array_filter( $mindful_blog_slider_content_ids );
		$mindful_blog_banner_slider_args['orderby']  = 'post__in';
	} else {
		$mindful_blog_banner_slider_args['orderby'] = 'date';
	}
	
} else {
	$mindful_blog_cat_content_id     = get_theme_mod( 'mindful_blog_banner_slider_content_category' );
	$mindful_blog_banner_slider_args = array(
		'cat'            => $mindful_blog_cat_content_id,
		'posts_per_page' => absint( 3 ),
	);
}
$mindful_blog_banner_slider_args = apply_filters( 'mindful_blog_banner_section_args', $mindful_blog_banner_slider_args );

mindful_blog_render_banner_section( $mindful_blog_banner_slider_args );

/**
 * Render Banner Section.
 */
function mindful_blog_render_banner_section( $mindful_blog_banner_slider_args ) {
	?>

	<section id="mindful_blog_banner_section" class="banner-section banner-style-2">
		<?php
		if ( is_customize_preview() ) :
			mindful_blog_section_link( 'mindful_blog_banner_section' );
		endif;
		?>
		<div class="ascendoor-wrapper">
			<div class="banner-section-wrapper ascen-body-wrap">
				<?php
				$mindful_blog_query = new WP_Query( $mindful_blog_banner_slider_args );
				if ( $mindful_blog_query->have_posts() ) :
					?>
					<div class="mindful-blog-slider-wrapper banner-slider mindful-blog-carousel-slider-navigation">
						<?php
						while ( $mindful_blog_query->have_posts() ) :
							$mindful_blog_query->the_post();
							?>
							<div class="carousel-item">
								<div class="mag-post-single has-image tile-design">
									<div class="mag-post-img">
										<a href="<?php the_permalink(); ?>">
											<?php the_post_thumbnail( 'full' ); ?>
										</a>
									</div>
									<div class="mag-post-detail">
										<div class="mag-post-category with-background">
											<?php mindful_blog_categories_list(); ?>
										</div>
										<h3 class="mag-post-title">
											<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
										</h3>
										<div class="mag-post-meta">
											<span class="post-author">
												<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><i class="fas fa-user"></i><?php echo esc_html( get_the_author() ); ?></a>
											</span>
											<span class="post-date">
												<a href="<?php the_permalink(); ?>"><i class="far fa-clock"></i><?php echo esc_html( get_the_date() ); ?></a>
											</span>
										</div>
									</div>
								</div>
							</div>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
					<?php
				endif;
				?>
			</div>
		</div>
	</section>

	<?php
}
