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

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

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

$slider_args = apply_filters( 'cosmic_blog_slider_section_args', $slider_args );

cosmic_blog_render_slider_section( $slider_args );

/**
 * Render Banner Section.
 */
function cosmic_blog_render_slider_section( $slider_args ) {

	$slider_query = new WP_Query( $slider_args );
	if ( $slider_query->have_posts() ) {
		?>
		<section id="cosmic_blog_banner_section" class="banner-section section-splitter banner-style-1">
			<?php
			if ( is_customize_preview() ) :
				cosmic_blog_section_link( 'cosmic_blog_banner_section' );
			endif;
			?>
			<div class="section-wrapper">
				<div class="banner-slider slick-button">
					<?php
					while ( $slider_query->have_posts() ) :
						$slider_query->the_post();
						?>
						<div class="blog-post-container list-layout">
							<div class="blog-post-inner">
									<div class="blog-post-image">
										<a href="<?php the_permalink(); ?>">
											<?php the_post_thumbnail( 'post-thumbnail' ); ?>
										</a>
									</div>
								<div class="blog-post-detail">
									<?php the_category( '', '', get_the_ID() ); ?>
									<h3 class="post-main-title">
										<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									</h3>
									<div class="post-meta-button">
										<div class="post-meta">
											<span class="post-author">
												<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>"><?php echo esc_html( get_the_author() ); ?></a>
											</span>
											<span class="post-date">
												<a href="<?php the_permalink(); ?>"><?php echo esc_html( get_the_date() ); ?></a>
											</span>
										</div>
									</div>
									<?php if ( ! has_post_thumbnail() ) { ?>
										<p class="post-excerpt">
											<?php echo esc_html( wp_trim_words( get_the_excerpt(), 20 ) ); ?>
										</p>
									<?php } ?>
								</div>
							</div>
						</div>
						<?php
					endwhile;
					wp_reset_postdata();
					?>
				</div>
			</div>
		</section>
		<?php
	}

}
