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

$aster_ebooks_slider_content_ids  = array();
$aster_ebooks_slider_content_type = get_theme_mod( 'aster_ebooks_banner_slider_content_type', 'post' );

for ( $aster_ebooks_i = 1; $aster_ebooks_i <= 3; $aster_ebooks_i++ ) {
	$aster_ebooks_slider_content_ids[] = get_theme_mod( 'aster_ebooks_banner_slider_content_' . $aster_ebooks_slider_content_type . '_' . $aster_ebooks_i );
}
$aster_ebooks_banner_slider_args = array(
	'post_type'           => $aster_ebooks_slider_content_type,
	'post__in'            => array_filter( $aster_ebooks_slider_content_ids ),
	'orderby'             => 'post__in',
	'posts_per_page'      => absint( 3 ),
	'ignore_sticky_posts' => true,
);
$aster_ebooks_banner_slider_args = apply_filters( 'aster_ebooks_banner_section_args', $aster_ebooks_banner_slider_args );

aster_ebooks_render_banner_section( $aster_ebooks_banner_slider_args );

/**
 * Render Banner Section.
 */
function aster_ebooks_render_banner_section( $aster_ebooks_banner_slider_args ) {     ?>

	<section id="aster_ebooks_banner_section" class="banner-section banner-style-1">
		<?php
		if ( is_customize_preview() ) :
			aster_ebooks_section_link( 'aster_ebooks_banner_section' );
		endif;
		?>
		<div class="banner-section-wrapper">
			<?php
			$aster_ebooks_query = new WP_Query( $aster_ebooks_banner_slider_args );
			if ( $aster_ebooks_query->have_posts() ) :
				?>
				<div class="asterthemes-banner-wrapper banner-slider aster-ebooks-carousel-navigation" data-slick='{"autoplay": false }'>
					<?php
					$aster_ebooks_i = 1;
					while ( $aster_ebooks_query->have_posts() ) :
						$aster_ebooks_query->the_post();
						$aster_ebooks_button_label = get_theme_mod( 'aster_ebooks_banner_button_label_' . $aster_ebooks_i);
						$aster_ebooks_button_link  = get_theme_mod( 'aster_ebooks_banner_button_link_' . $aster_ebooks_i);
						$aster_ebooks_button_link  = ! empty( $aster_ebooks_button_link ) ? $aster_ebooks_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" alt="Default Image" />
									<?php endif; ?>
								</div>
								<div class="banner-caption">
									<div class="asterthemes-wrapper">
										<div class="banner-catption-wrapper wow bounceInUp delay-1000" data-wow-duration="2s">
											<h1 class="banner-caption-title">
												<?php
												$title = get_the_title();
												$aster_ebooks_title_parts = explode(' ', $title);
												$aster_ebooks_last_word = array_pop($aster_ebooks_title_parts);
												$aster_ebooks_title_without_last_word = implode(' ', $aster_ebooks_title_parts);
												echo $aster_ebooks_title_without_last_word . ' <span class="yellow-text">' . $aster_ebooks_last_word . '</span>';
												?>
											</h1>
											<?php if ( ! empty( $aster_ebooks_button_label ) ) { ?>
												<div class="banner-slider-btn">
													<a href="<?php echo esc_url( $aster_ebooks_button_link ); ?>" class="asterthemes-button"><?php echo esc_html( $aster_ebooks_button_label ); ?></a>
												</div>
											<?php } ?>
										</div>

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

	<?php
}