<?php

if ( ! get_theme_mod( 'construktly_enable_banner_section', false ) ) {
	return;
}

$content_ids = $btn_label = $btn_link = $section_content = array();

$content_count = 3;
$content_type  = get_theme_mod( 'construktly_banner_content', 'page' );

if ( ! in_array( $content_type, array( 'post', 'page' ) ) ) {
	return;
}

for ( $i = 1; $i <= $content_count; $i++ ) {
	$item_id               = get_theme_mod( 'construktly_banner_content_' . $content_type . '_' . $i );
	$content_ids[]         = $item_id;
	$btn_label[ $item_id ] = get_theme_mod( 'construktly_banner_button_label' . $i, __( 'Read More', 'construktly' ) );
	$btn_link[ $item_id ]  = get_theme_mod( 'construktly_banner_button_link' . $i );
}

$args = array(
	'post_type'           => $content_type,
	'post__in'            => (array) $content_ids,
	'orderby'             => 'post__in',
	'posts_per_page'      => absint( $content_count ),
	'ignore_sticky_posts' => true,
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :
	while ( $query->have_posts() ) :
		$query->the_post();
		$section_content[] = array(
			'id'            => get_the_ID(),
			'title'         => get_the_title(),
			'excerpt'       => get_the_excerpt(),
			'permalink'     => get_the_permalink(),
			'thumbnail_url' => get_the_post_thumbnail_url( get_the_ID(), 'full' ),
		);
	endwhile;
	wp_reset_postdata();

	$section_content = apply_filters( 'construktly_banner_section_content', $section_content );

	construktly_render_banner_section( $section_content, $btn_label, $btn_link );
endif;

/**
 * Render Banner Section
 */
function construktly_render_banner_section( $section_content, $btn_label, $btn_link ) {
	if ( empty( $section_content ) ) {
		return;
	}
	?>
	<section id="construktly_banner_section" class="construktly-main-banner-section banner-style-2">
		<?php
		if ( is_customize_preview() ) :
			construktly_section_link( 'construktly_banner_section' );
		endif;
		?>
		<div class="main-slider">
			<?php
			foreach ( $section_content as $content ) {
				$item_id              = $content['id'];
				$btn_link[ $item_id ] = ! empty( $btn_link[ $item_id ] ) ? $btn_link[ $item_id ] : $content['permalink'];
				?>
				<div class="construktly-banner-slider-single">
					<div class="construktly-banner-slider-img">
						<img src="<?php echo esc_url( $content['thumbnail_url'] ); ?>" alt="<?php echo esc_attr( $content['title'] ); ?>">
					</div>
					<div class="construktly-banner-slider-detail">
						<div class="ascendoor-wrapper">
							<div class="construktly-banner-slider-detail-inside">
								<h3 class="construktly-banner-head-title"><?php echo esc_html( $content['title'] ); ?></h3>
								<p><?php echo esc_html( $content['excerpt'] ); ?></p>
								<?php if ( $btn_label[ $item_id ] ) { ?>
									<div class="construktly-button banner-slider-btn construktly-button-secondary-alternate">
										<a href="<?php echo esc_url( $btn_link[ $item_id ] ); ?>"><?php echo esc_html( $btn_label[ $item_id ] ); ?></a>
									</div>
								<?php } ?>
							</div>
						</div>
					</div>
				</div>
				<?php
			}
			?>
		</div>
	</section>
	<?php
}
