<?php
$displayType = $attributes['displayType'] ?? 'grid';
$variantLocation = $attributes['variantLocation'] ?? 'carhop';
$showTableOfContents = $attributes['showTableOfContents'] ?? true;
$backgroundColor = $attributes['backgroundColor'] ?? null;
$backgroundColorHex = get_color_hex_from_slug($backgroundColor) ?? null;

$hasLightBackground = is_color_light($backgroundColorHex);
$backgroundColorClass = $hasLightBackground ? 'block-dernieres-dynamiques--has-light-bg' : 'block-dernieres-dynamiques--has-dark-bg';


$current_blog_id = get_current_blog_id();
switch_to_blog(2);

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

$dynamiques = new WP_Query($args);

$last_issue = $dynamiques->posts[0];
if (!$last_issue) return;
$issueNumber = get_field('issue_number', $last_issue->ID);

$issue_related_articles =  get_field('articles', $last_issue->ID);
?>


<section <?php echo get_block_wrapper_attributes(array('class' => 'block-dernieres-dynamiques content-section block-dernieres-dynamiques--' . $variantLocation . ' alignfull ' . $backgroundColorClass)); ?>>
	<div class="block-dernieres-dynamiques__inner">
		<div class="block-dernieres-dynamiques__header">
			<h2 class="block-title">
				<span>Dernier numéro</span>
			</h2>
			<a href="<?php echo get_the_permalink($last_issue->ID); ?>" class="header-link">
				<span>Lire toute la revue</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>
			<p class="content-meta__revue-issue <?php echo $variantLocation === 'carhop' ? 'content-meta__revue-issue--green' : 'content-meta__revue-issue--white'; ?>">
				<span class="revue-issue-number revue-meta__label sr-only">Numéro</span>
				<?php echo $issueNumber; ?>
			</p>
		</div>

		<div class="block-dernieres-dynamiques__issue-content-wrapper">
			<div class="block-dernieres-dynamiques__issue-content">
				<?php if ($showTableOfContents) : ?>
					<h3 class="block-dernieres-dynamiques__issue-title">
						<?php echo $last_issue->post_title; ?>
					</h3>
				<?php else : ?>
					<h3 class="block-dernieres-dynamiques__issue-title">
						<a href="<?php echo get_the_permalink($last_issue->ID); ?>"><?php echo $last_issue->post_title; ?></a>
					</h3>
				<?php endif; ?>
				<div class="block-dernieres-dynamiques__issue-parution">
					<h4 class="block-dernieres-dynamiques__issue-date title">Parution</h4>
					<p class="block-dernieres-dynamiques__issue-date-number">
						<?php echo get_the_date('d F Y', $last_issue->ID); ?>
					</p>
				</div>

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

			<div class="block-dernieres-dynamiques__issue-thumbnail-wrapper">
				<div class="block-dernieres-dynamiques__issue-thumbnail">
					<?php
					$focal_position = safe_get_thumbnail_focal_point_css($last_issue->ID);
					echo get_the_post_thumbnail($last_issue->ID, 'full', [
						'style' => 'object-fit: cover; object-position: ' . esc_attr($focal_position) . ';'
					]);
					?>
				</div>
				<div class="card-background"></div>
			</div>
		</div>

		<?php if ($showTableOfContents) : ?>

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


				<div class="article-caroussel article-caroussel--<?php echo $displayType; ?>">

					<?php if ($displayType === 'grid') : ?>

						<?php foreach ($issue_related_articles as $article) : ?>
							<?php
							get_template_part('template-parts/dynamiques/article-card', null, array(
								'ID' => $article->ID,

							));
							?>
						<?php endforeach; ?>
						<!-- <button class="related-articles__show-all">
						Tout afficher
					</button> -->
					<?php else : ?>

						<div class="article-caroussel__slider">
							<?php if ($issue_related_articles && is_array($issue_related_articles)) : ?>


								<div class="swiper dernieres-dynamiques-swiper">
									<div class="swiper-wrapper">
										<?php foreach ($issue_related_articles as $article) : ?>

											<?php get_template_part('template-parts/dynamiques/article-card', null, array(
												'ID' => $article->ID,
												'isSwiperSlide' => true,
												'showCover' => true,
											)) ?>

										<?php endforeach; ?>
									</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>


							<?php endif; ?>
						</div>
					<?php endif; ?>
				</div>



			</div>
		<?php endif; ?>

	</div>
</section>
<?php

restore_current_blog();



?>
<?php
