<?php
if ( ! get_theme_mod( 'florist_bouquet_enable_banner_section', true ) ) {
	return;
}

$florist_bouquet_slider_content_ids  = array();
$florist_bouquet_slider_content_type = get_theme_mod( 'florist_bouquet_banner_slider_content_type', 'post' );

for ( $i = 1; $i <= 3; $i++ ) {
	$florist_bouquet_slider_content_ids[] = get_theme_mod( 'florist_bouquet_banner_slider_content_' . $florist_bouquet_slider_content_type . '_' . $i );
}

// Get the category for the banner slider from theme mods or a default category
$florist_bouquet_banner_slider_category = get_theme_mod('florist_bouquet_banner_slider_category', 'slider');

// Modify query to fetch posts from a specific category
$florist_bouquet_banner_slider_args = array(
	'post_type'           => $florist_bouquet_slider_content_type,
	'post__in'            => array_filter( $florist_bouquet_slider_content_ids ),
	'orderby'             => 'post__in',
	'posts_per_page'      => absint(3),
	'ignore_sticky_posts' => true,
);

// Apply category filter only if content type is 'post'
if ( 'post' === $florist_bouquet_slider_content_type && ! empty( $florist_bouquet_banner_slider_category ) ) {
	$florist_bouquet_banner_slider_args['category_name'] = $florist_bouquet_banner_slider_category;
}

$florist_bouquet_banner_slider_args = apply_filters( 'florist_bouquet_banner_section_args', $florist_bouquet_banner_slider_args );

florist_bouquet_render_banner_section( $florist_bouquet_banner_slider_args );

/**
 * Render Banner Section.
 */
function florist_bouquet_render_banner_section( $florist_bouquet_banner_slider_args ) {     ?>

	<section id="florist_bouquet_banner_section" class="banner-section banner-style-1">
		<?php
		if ( is_customize_preview() ) :
			florist_bouquet_section_link( 'florist_bouquet_banner_section' );
		endif;
		?>
		<div class="banner-section-wrapper">
			<?php
			$florist_bouquet_query = new WP_Query( $florist_bouquet_banner_slider_args );
			if ( $florist_bouquet_query->have_posts() ) :
				?>
				<div class="asterthemes-banner-wrapper banner-slider florist-bouquet-carousel-navigation" data-slick='{"autoplay": false }'>
					<?php
					$i = 1;
					while ( $florist_bouquet_query->have_posts() ) :
						$florist_bouquet_query->the_post();
						$florist_bouquet_button_label = get_theme_mod( 'florist_bouquet_banner_button_label_' . $i );
						$florist_bouquet_button_link  = get_theme_mod( 'florist_bouquet_banner_button_link_' . $i );
						$florist_bouquet_button_link  = ! empty( $florist_bouquet_button_link ) ? $florist_bouquet_button_link : get_the_permalink();
						?>
						<div class="banner-single-outer">
							<div class="banner-single">
								<div class="banner-img">
									<?php the_post_thumbnail( 'full' ); ?>
								</div>
								<div class="banner-caption">
									<div class="asterthemes-wrapper wow bounceInUp delay-1000" data-wow-duration="2s">
										<div class="banner-catption-wrapper">
											<h1 class="banner-caption-title">
											<a href="<?php the_permalink(); ?>">
												<?php the_title(); ?>
											</a>
											</h1>
											<div class="caption-description">
												<p>
													<?php echo wp_kses_post( wp_trim_words( get_the_content(), 25 ) ); ?>
												</p>
											</div>
											<?php if ( ! empty( $florist_bouquet_button_label ) ) { ?>
												<div class="banner-slider-btn">
													<a href="<?php echo esc_url( $florist_bouquet_button_link ); ?>" class="asterthemes-button"><?php echo esc_html( $florist_bouquet_button_label ); ?></a>
												</div>
											<?php } ?>
										</div>
									</div>
								</div>
							</div>
						</div>
						<?php
						$i++;
					endwhile;
					wp_reset_postdata();
					?>
				</div>
				<?php
			endif;
			?>
		</div>
	</section>

	<?php
}