<?php


$limit = 6;
$featured_items = array();

// QUERY ACTUALITES et AJOUT DES POSTS AU TABLEAU $featured_items
switch_to_blog(1);
$actualites = new WP_Query(array(
	'post_type'      => 'actualites',
	'post_status'    => 'publish',
	'posts_per_page' => $limit,
	'orderby'        => 'date',
	'order'          => 'DESC',
));
if ($actualites->have_posts()) {
	while ($actualites->have_posts()) {
		$actualites->the_post();
		$featured_items[] = array(
			'blog_id'   => get_current_blog_id(),
			'id'        => get_the_ID(),
			'title'     => get_the_title(),
			'excerpt'   => get_the_excerpt(),
			'permalink' => get_permalink(),
			'timestamp' => (int) get_post_time('U', true),
			'type'      => 'post',
		);
	}
	wp_reset_postdata();
}
restore_current_blog();

// QUERY REVUES et AJOUT DES POSTS AU TABLEAU $featured_items
switch_to_blog(2);
$revues = new WP_Query(array(
	'post_type'      => 'revues',
	'post_status'    => 'publish',
	'posts_per_page' => $limit,
	'orderby'        => 'date',
	'order'          => 'DESC',
));
if ($revues->have_posts()) {
	while ($revues->have_posts()) {
		$revues->the_post();
		$featured_items[] = array(
			'blog_id'   => get_current_blog_id(),
			'id'        => get_the_ID(),
			'title'     => get_the_title(),
			'excerpt'   => get_the_excerpt(),
			'permalink' => get_permalink(),
			'timestamp' => (int) get_post_time('U', true),
			'type'      => 'revues',
		);
	}
	wp_reset_postdata();
}
restore_current_blog();

// Tri global par date DESC et limitation à $limit
\usort($featured_items, function ($a, $b) {
	return $b['timestamp'] <=> $a['timestamp'];
});
$featured_items = \array_slice($featured_items, 0, $limit);
?>

<section class="featured-news alignfull has-background" <?php echo get_block_wrapper_attributes(); ?>>
	<div class="featured-news__inner">

		<div class="featured-news__header">
			<h2 class="block-title">À la une</h2>
			<h3 class="block-subtitle">Le fil d’actualité du carhop</h3>
		</div>
		<div class="featured-news__slider-content">
			<div class="swiper featured-news-swiper">
				<div class="swiper-wrapper">

					<?php if (!empty($featured_items)) : ?>
						<?php foreach ($featured_items as $item) : ?>


							<?php
							$current_blog_id = get_current_blog_id();
							$did_switch = false;
							if ($item['blog_id'] !== $current_blog_id) {
								switch_to_blog($item['blog_id']);
								$did_switch = true;
							}

			

							get_template_part('template-parts/components/cards/slider-card', null, array(
								'ID' => $item['id'],
								'isSwiperSlide' => true,
								'showCover' => true,
								'showTags' => false,
							));

							if ($did_switch) {
								restore_current_blog();
							}
							?>
						<?php endforeach; ?>
					<?php endif; ?>

				</div>

				<div class="swiper-pagination-fraction"></div>
				<div class="swiper-pagination"></div>

				<div class="swiper-controls">
					<div class="swiper-button-prev">
						<?php
						$previous_arrow_icon = get_template_directory() . '/resources/img/elements/carhop-slider-previous.svg';
						if (file_exists($previous_arrow_icon)) {
							echo file_get_contents($previous_arrow_icon);
						}
						?>

					</div>
					<div class="swiper-button-next">
						<?php
						$next_arrow_icon = get_template_directory() . '/resources/img/elements/carhop-slider-next.svg';
						if (file_exists($next_arrow_icon)) {
							echo file_get_contents($next_arrow_icon);
						}
						?>
					</div>
				</div>
			</div>
		</div>

	</div>

</section>