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

$carousel_content_ids  = array();
$carousel_content_type = get_theme_mod( 'headline_news_posts_carousel_content_type', 'post' );

if ( $carousel_content_type === 'post' ) {
	for ( $i = 1; $i <= 4; $i++ ) {
		$carousel_content_ids[] = get_theme_mod( 'headline_news_posts_carousel_content_post_' . $i );
	}
	$carousel_args = array(
		'post_type'           => 'post',
		'posts_per_page'      => absint( 4 ),
		'ignore_sticky_posts' => true,
	);
	if ( ! empty( array_filter( $carousel_content_ids ) ) ) {
		$carousel_args['post__in'] = array_filter( $carousel_content_ids );
		$carousel_args['orderby']  = 'post__in';
	} else {
		$carousel_args['orderby'] = 'date';
	}
} else {
	$cat_content_id = get_theme_mod( 'headline_news_posts_carousel_content_category' );
	$carousel_args  = array(
		'cat'            => $cat_content_id,
		'posts_per_page' => absint( 4 ),
	);
}
$carousel_args = apply_filters( 'headline_news_posts_carousel_section_args', $carousel_args );

headline_news_render_posts_carousel_section( $carousel_args );

/**
 * Render Posts Carousel Section.
 */
function headline_news_render_posts_carousel_section( $carousel_args ) {

	$query = new WP_Query( $carousel_args );
	if ( $query->have_posts() ) {
		$section_title = get_theme_mod( 'headline_news_posts_carousel_title', __( 'Posts Carousel', 'headline-news' ) );
		?>
		<section id="headline_news_posts_carousel_section" class="post-carousel section-splitter" data-aos="fade-up" data-aos-duration="1000">
			<?php
			if ( is_customize_preview() ) :
				headline_news_section_link( 'headline_news_posts_carousel_section' );
			endif;
			?>
			<div class="section-wrapper">
				<div class="main-container-wrap">
					<div class="title-heading">
						<?php if ( ! empty( $section_title ) ) : ?>
							<h3 class="section-title">
								<?php echo esc_html( $section_title ); ?>
							</h3>
						<?php endif; ?>
						<div class="carousel-navigation"></div>
					</div>
					<div class="post-carousel-wrapper">
						<div class="post-slider slick-button">
							<?php
							while ( $query->have_posts() ) :
								$query->the_post();
								?>
								<div class="blog-post-container tile-layout">
									<div class="blog-post-inner">
										<?php if ( has_post_thumbnail() ) { ?>
											<div class="blog-post-image">
												<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'post-thumbnail' ); ?></a>
											</div>
										<?php } ?>
										<div class="blog-post-detail">
											<?php headline_news_categories_list(); ?>
											<div class="post-meta">
												<?php headline_news_posted_by(); ?>
												<?php headline_news_posted_on(); ?>
											</div>
											<h2 class="entry-title">
												<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
											</h2>
											<?php if ( ! has_post_thumbnail() ) { ?>
												<p class="post-excerpt">
													<?php echo wp_kses_post( wp_trim_words( get_the_content(), 20 ) ); ?>
												</p>
											<?php } ?>
										</div>
									</div>
								</div>
								<?php
							endwhile;
							wp_reset_postdata();
							?>
						</div>
					</div>
				</div>
			</div>
		</section>
		<?php
	}
}
