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

$construction_engineer_slider_content_ids  = array();
$construction_engineer_slider_content_type = get_theme_mod( 'construction_engineer_banner_slider_content_type', 'post' );

for ( $construction_engineer_i = 1; $construction_engineer_i <= 3; $construction_engineer_i++ ) {
	$construction_engineer_slider_content_ids[] = get_theme_mod( 'construction_engineer_banner_slider_content_' . $construction_engineer_slider_content_type . '_' . $construction_engineer_i );
}
// Get the category for the banner slider from theme mods or a default category
$construction_engineer_banner_slider_category = get_theme_mod('construction_engineer_banner_slider_category', 'slider');

// Modify query to fetch posts from a specific category
$construction_engineer_banner_slider_args = array(
    'post_type'           => $construction_engineer_slider_content_type,
    'post__in'            => array_filter( $construction_engineer_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' === $construction_engineer_slider_content_type && ! empty( $construction_engineer_banner_slider_category ) ) {
    $construction_engineer_banner_slider_args['category_name'] = $construction_engineer_banner_slider_category;
}
$construction_engineer_banner_slider_args = apply_filters( 'construction_engineer_banner_section_args', $construction_engineer_banner_slider_args );

construction_engineer_render_banner_section( $construction_engineer_banner_slider_args );

/**
 * Render Banner Section.
 */
function construction_engineer_render_banner_section( $construction_engineer_banner_slider_args ) {     ?>

	<section id="construction_engineer_banner_section" class="banner-section banner-style-1">
		<?php
		if ( is_customize_preview() ) :
			construction_engineer_section_link( 'construction_engineer_banner_section' );
		endif;
		?>
		<div class="banner-section-wrapper">
			<?php
			$construction_engineer_query = new WP_Query( $construction_engineer_banner_slider_args );
			if ( $construction_engineer_query->have_posts() ) :
				?>
				<div class="asterthemes-banner-wrapper banner-slider construction-engineer-carousel-navigation" data-slick='{"autoplay": false }'>
					<?php 
					$construction_engineer_i = 1;
					while ( $construction_engineer_query->have_posts() ) :
						$construction_engineer_query->the_post();
						$construction_engineer_button_label = get_theme_mod( 'construction_engineer_banner_button_label_' . $construction_engineer_i);
						$construction_engineer_button_link  = get_theme_mod( 'construction_engineer_banner_button_link_' . $construction_engineer_i);
						$construction_engineer_second_button_label = get_theme_mod( 'construction_engineer_banner_second_button_label_' . $construction_engineer_i);
						$construction_engineer_second_button_link  = get_theme_mod( 'construction_engineer_banner_second_button_link_' . $construction_engineer_i);
						$construction_engineer_banner_short_heading_1 = get_theme_mod( 'construction_engineer_banner_short_heading_1' . $construction_engineer_i);
						$construction_engineer_banner_short_heading_2 = get_theme_mod( 'construction_engineer_banner_short_heading_2' . $construction_engineer_i);
						$construction_engineer_banner_short_heading_3 = get_theme_mod( 'construction_engineer_banner_short_heading_3' . $construction_engineer_i);
						$construction_engineer_button_link  = ! empty( $construction_engineer_button_link ) ? $construction_engineer_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">
										<div class="banner-catption-wrapper wow fadeInDown" data-wow-duration="1.5s">
											<h1 class="banner-caption-title">
												<a href="<?php the_permalink(); ?>">
							                        <?php the_title(); ?>
							                    </a>
											</h1>
											<div class="text-box">
												<?php if ( ! empty( $construction_engineer_banner_short_heading_1 ) ) { ?>
													<span><?php echo esc_html( $construction_engineer_banner_short_heading_1 ); ?></span>
												<?php } ?>
												<?php if ( ! empty( $construction_engineer_banner_short_heading_2 ) ) { ?>
													<span><?php echo esc_html( $construction_engineer_banner_short_heading_2 ); ?></span>
												<?php } ?>
												<?php if ( ! empty( $construction_engineer_banner_short_heading_3 ) ) { ?>
													<span><?php echo esc_html( $construction_engineer_banner_short_heading_3 ); ?></span>
												<?php } ?>
											</div>
											<?php if ( ! empty( $construction_engineer_button_label ) ) { ?>
												<div class="banner-slider-btn">
													<a href="<?php echo esc_url( $construction_engineer_button_link ); ?>" class="asterthemes-button"><?php echo esc_html( $construction_engineer_button_label ); ?></a>
												</div>
											<?php } ?>
											<?php if ( ! empty( $construction_engineer_second_button_label ) ) { ?>
												<div class="banner-slider-btn2">
													<a href="<?php echo esc_url( $construction_engineer_second_button_link ); ?>" class="asterthemes-button"><?php echo esc_html( $construction_engineer_second_button_label ); ?></a>
												</div>
											<?php } ?>
										</div>
									</div>
								</div>
							</div>
						</div>
						<?php
						$construction_engineer_i++;
					endwhile;
					wp_reset_postdata();
					?>
				</div>
				<?php
			endif;
			?>
		</div>
	</section>

	<?php
}