<?php
$current_blog_id = get_current_blog_id();
switch_to_blog(2);


$args = array(
	'post_type' => 'revues',
	'posts_per_page' => 1,
	'orderby' => 'date',
	'order' => 'DESC',
);




$dynamiques = new WP_Query($args);

$last_issue = $dynamiques->posts[0];

$issue_related_articles =  new WP_Query(array(
	'post_type' => 'articles',
	'posts_per_page' => 3,
	'orderby' => 'date',
	'order' => 'DESC',
	'meta_query' => array(
		array(
			'key' => 'related_revue',
			'value' => $last_issue->ID,
			'compare' => '=',
		),
	),
));

?>

<section class="block-dernieres-dynamiques">
	<div class="block-dernieres-dynamiques__inner">
		<div class="block-dernieres-dynamiques__header">
			<h2 class="block-title">
				<span>Dernières dynamiques</span>
			</h2>
			<a href="<?php echo get_the_permalink($last_issue->ID); ?>" class="header-link">
				<span>Tout lire</span>
				<?php
				$svg_path = get_template_directory() . '/resources/img/carhop-fleche-lien-externe-full.svg';
				if (file_exists($svg_path)) {
					echo file_get_contents($svg_path);
				}
				?>
			</a>

		</div>
		<div class="content-meta">
			<span class="content-meta__type content-meta__type--revue">Revue</span>
		</div>

		<div class="block-dernieres-dynamiques__issue-content-wrapper">
			<div class="block-dernieres-dynamiques__issue-content">

				<h3 class="block-dernieres-dynamiques__issue-title">
					<?php echo $last_issue->post_title; ?>
				</h3>
				<h4 class="block-dernieres-dynamiques__issue-description-title">Édito</h4>
				<p class="block-dernieres-dynamiques__issue-description">
					<?php echo $last_issue->post_excerpt; ?>
				</p>

			</div>

			<div class="block-dernieres-dynamiques__issue-thumbnail-wrapper">
				<div class="block-dernieres-dynamiques__issue-thumbnail">
					<?php echo get_the_post_thumbnail($last_issue->ID, 'full'); ?>
				</div>
				<div class="card-background"></div>
			</div>
		</div>

		<div class="related-articles">
			<h3 class="related-articles__title">
				Table des matières
			</h3>


			<div class="article-caroussel">


				<?php foreach ($issue_related_articles->posts as $article) : ?>

					<div class="article-card">
						<div class="content-meta">
							<span class="content-meta__type content-meta__type--article">Article</span>
							<!-- <span class="content-meta__reading-time"><?php echo calculate_reading_time($article->post_content); ?> minutes de lecture</span> -->
						</div>


						<a href="<?php echo get_the_permalink($article->ID); ?>" class="article-card__link">

							<h4 class="article-card__title"><?php echo $article->post_title; ?></h4>
						</a>

						<ul class="article-card__tags">
							<?php
							$terms = get_the_terms($article->ID, 'etiquettes');
							if ($terms): ?>
								<?php foreach ($terms as $term): ?>
									<li class="article-card__tag article-tag"><?php echo esc_html($term->name); ?></li>
								<?php endforeach; ?>
							<?php endif; ?>

						</ul>
						<a class="article-card__link-button" href="<?php echo get_the_permalink($article->ID); ?>">

							<?php
							$svg_path = get_template_directory() . '/resources/img/carhop-fleche-full.svg';
							if (file_exists($svg_path)) {
								echo file_get_contents($svg_path);
							}
							?>
						</a>


					</div>

				<?php endforeach; ?>
			</div>

			<button class="related-articles__show-all">
				Tout afficher
			</button>

		</div>


	</div>
</section>
<?php

restore_current_blog();



?>
<?php

$args = array(
	'post_type' => 'post',
	'posts_per_page' => 8,
);
$query = new WP_Query($args);

$query =  new WP_Query(array(
	'post_type' => 'articles',
	'posts_per_page' => 3,
	'orderby' => 'date',
	'order' => 'DESC',
	'meta_query' => array(
		array(
			'key' => 'related_revue',
			'value' => $last_issue->ID,
			'compare' => '=',
		),
	),
));


// Enqueue Swiper CSS and JS
// wp_enqueue_style('swiper-css', 'https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css');
// wp_enqueue_script('swiper-js', 'https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js', array(), null, true);

?>
<!-- <section <?php echo get_block_wrapper_attributes(); ?>>
	<h2>lalala actualités</h2>
	<?php if ($query->have_posts()) : ?>
		<div class="swiper latest-news-swiper">
			<div class="swiper-wrapper">
				<?php while ($query->have_posts()) : $query->the_post(); ?>
					<div class="swiper-slide">
						<article class="news-card">
							<?php if (has_post_thumbnail()) : ?>
								<div class="news-image">
									<?php the_post_thumbnail('medium'); ?>
								</div>
							<?php endif; ?>
							<div class="news-content">
								<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
								<div class="news-excerpt">
									<?php the_excerpt(); ?>
								</div>
							</div>
						</article>
					</div>


				<?php endwhile; ?>
			</div>
			<div class="swiper-pagination"></div>
			<div class="swiper-button-next"></div>
			<div class="swiper-button-prev"></div>
		</div>


	<?php endif; ?>
</section> -->