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

$bike_workshop_slider_content_ids  = array();
$bike_workshop_slider_content_type = get_theme_mod( 'bike_workshop_banner_slider_content_type', 'post' );

for ( $bike_workshop_i = 1; $bike_workshop_i <= 3; $bike_workshop_i++ ) {
	$bike_workshop_slider_content_ids[] = get_theme_mod( 'bike_workshop_banner_slider_content_' . $bike_workshop_slider_content_type . '_' . $bike_workshop_i );
}

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

// Modify query to fetch posts from a specific category
$bike_workshop_banner_slider_args = array(
    'post_type'           => $bike_workshop_slider_content_type,
    'post__in'            => array_filter( $bike_workshop_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' === $bike_workshop_slider_content_type && ! empty( $bike_workshop_banner_slider_category ) ) {
    $bike_workshop_banner_slider_args['category_name'] = $bike_workshop_banner_slider_category;
}

$bike_workshop_banner_slider_args = apply_filters( 'bike_workshop_banner_section_args', $bike_workshop_banner_slider_args );

bike_workshop_render_banner_section( $bike_workshop_banner_slider_args );

/**
 * Render Banner Section.
 */
function bike_workshop_render_banner_section( $bike_workshop_banner_slider_args ) {     ?>

	<section id="bike_workshop_banner_section" class="banner-section banner-style-1">
		<?php
		if ( is_customize_preview() ) :
			bike_workshop_section_link( 'bike_workshop_banner_section' );
		endif;
		?>
		<div class="banner-section-wrapper">
			<?php
			$bike_workshop_query = new WP_Query( $bike_workshop_banner_slider_args );
			if ( $bike_workshop_query->have_posts() ) :
				?>
				<div class="asterthemes-banner-wrapper banner-slider bike-workshop-carousel-navigation" data-slick='{"autoplay": false }'>
					<?php
					$bike_workshop_i = 1;
					while ( $bike_workshop_query->have_posts() ) :
						$bike_workshop_query->the_post();
						$bike_workshop_button_label = get_theme_mod( 'bike_workshop_banner_button_label_' . $bike_workshop_i, '' );
						$bike_workshop_button_link  = get_theme_mod( 'bike_workshop_banner_button_link_' . $bike_workshop_i, '' );
						$bike_workshop_button_link  = ! empty( $bike_workshop_button_link ) ? $bike_workshop_button_link : get_the_permalink();
						?>
						<div class="banner-single-outer">
							<div class="banner-single">
								<div class="banner-img">
                                    <?php if ( has_post_thumbnail() ) : ?>
                                        <?php the_post_thumbnail( 'full' ); ?>
                                    <?php else : ?>
                                        <img src="<?php echo esc_url( get_template_directory_uri() ); ?>/resource/img/default.png" />
                                    <?php endif; ?>
									<div class="banner-overlay"></div>
                                </div>
								<div class="banner-caption">
								    <div class="asterthemes-wrapper">
								        <div class="banner-catption-wrapper wow fadeInDown" data-wow-duration="1.5s">
								        	<!-- New Heading from Customizer -->
								            <h2 class="custom-banner-heading">
										    <?php echo esc_html( get_theme_mod( 'bike_workshop_banner_heading') ); ?>
											</h2>

											<a href="<?php the_permalink(); ?>"> <h1 class="banner-caption-title"><?php the_title(); ?></h1> </a>
								            <div class="mag-post-excerpt">
								                <?php the_excerpt(); ?>
								            </div>
								            <?php if ( ! empty( $bike_workshop_button_label ) ) { ?>
								                <div class="banner-slider-btn">
								                    <a href="<?php echo esc_url( $bike_workshop_button_link ); ?>" class="asterthemes-button"><?php echo esc_html( $bike_workshop_button_label ); ?></a>
								                </div>
								            <?php } ?>
								            
								        </div>
								    </div>
								</div>

							</div>
						</div>
						<?php
						$bike_workshop_i++;
					endwhile;
					wp_reset_postdata();
					?>
				</div>
				<?php
			endif;
			?>
		</div>
	</section>

	<?php
}