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

$charity_elite_content_id   = array();
$charity_elite_content_type = get_theme_mod( 'charity_elite_banner_slider_content_type', 'post' );

if ( 'post' === $charity_elite_content_type ) {
	$charity_elite_content_id[] = get_theme_mod( 'charity_elite_banner_slider_content_post' );
} else {
	$charity_elite_content_id[] = get_theme_mod( 'charity_elite_banner_slider_content_page' );
}
$charity_elite_args = array(
	'post_type'           => $charity_elite_content_type,
	'posts_per_page'      => absint( 1 ),
	'ignore_sticky_posts' => true,
);

if ( ! empty( array_filter( $charity_elite_content_id ) ) ) {
	$charity_elite_args['post__in'] = array_filter( $charity_elite_content_id );
	$charity_elite_args['orderby']  = 'post__in';
} else {
	$charity_elite_args['orderby'] = 'date';
}

$charity_elite_args = apply_filters( 'charity_elite_banner_section_args', $charity_elite_args );

charity_elite_render_banner_section( $charity_elite_args );

/**
 * Render Banner Section.
 */
function charity_elite_render_banner_section( $charity_elite_args ) {

	$charity_elite_query = new WP_Query( $charity_elite_args );
	if ( $charity_elite_query->have_posts() ) :
		$charity_elite_banner_bg_image = get_theme_mod( 'charity_elite_banner_background_image', '' );

		?>
		<section id="charity_elite_banner_section" class="banner-section banner-style-2 image-right" style='background-image:url("<?php echo esc_url( $charity_elite_banner_bg_image ); ?>")'>
			<?php
			if ( is_customize_preview() ) :
				charity_elite_section_link( 'charity_elite_banner_section' );
			endif;
			?>
			<div class="banner-section-wrapper">
				<div class="ascendoor-banner-wrapper banner-slider charity-elite-carousel-navigation">
					<?php
					while ( $charity_elite_query->have_posts() ) :
						$charity_elite_query->the_post();
						$charity_elite_subtitle     = get_theme_mod( 'charity_elite_banner_subtitle', '' );
						$charity_elite_button_label = get_theme_mod( 'charity_elite_banner_button_label', __( 'Read More', 'charity-elite' ) );
						$charity_elite_button_link  = get_theme_mod( 'charity_elite_banner_button_link', '' );
						$charity_elite_button_link  = ! empty( $charity_elite_button_link ) ? $charity_elite_button_link : get_the_permalink();
						?>
						<div class="banner-single-outer">
							<div class="ascendoor-wrapper">
								<div class="banner-single">
									<div class="banner-img">
										<?php the_post_thumbnail( 'post-thumbnail' ); ?>
									</div>
									<div class="banner-caption">
										<div class="banner-catption-wrapper">
											<?php if ( ! empty( $charity_elite_subtitle ) ) { ?>
												<h4 class="caption-subtitle"><?php echo esc_html( $charity_elite_subtitle ); ?></h4>
											<?php } ?>
											<h3 class="banner-caption-title">
												<?php the_title(); ?>
											</h3>
											<div class="caption-description">
												<p>
													<?php echo wp_kses_post( wp_trim_words( get_the_content(), 10 ) ); ?>
												</p>
											</div>
											<?php if ( ! empty( $charity_elite_button_label ) ) { ?>
												<div class="banner-slider-btn">
													<a href="<?php echo esc_url( $charity_elite_button_link ); ?>" class="ascendoor-button"><?php echo esc_html( $charity_elite_button_label ); ?></a>
												</div>
											<?php } ?>
										</div>
									</div>
								</div>
							</div>
						</div>
						<?php
					endwhile;
					wp_reset_postdata();
					?>
				</div>
			</div>
		</section>
		<?php
	endif;

}
