<?php

$queryArgs = array(
	"post_type" => "post",
	"status" => "publish",
	"posts_per_page" => 4,
	"tax_query" => array(
		array(
			"taxonomy" => "news_type",
			"field" => "slug",
			"terms" => "communiques-de-presse",
		),
	),
);
$pressPosts = new WP_Query($queryArgs);

$args = array(
	'meta_key' => '_wp_page_template',
	'meta_value' => "template-archive-news.php"
);
$newsTemplatePage = get_pages($args) ? get_pages($args)[0] : null;

?>

<section <?php echo get_block_wrapper_attributes(['class' => 'homegrade-blocks-communiques-presse-feed']); ?>>
	<?php echo $content; ?>
	<div class="card-grid-container">

		<?php foreach ($pressPosts->posts as $post) : ?>
			<?php
			$post_thumbnail = get_the_post_thumbnail($post->ID, 'full', array('class' => 'card-news__thumbnail card-post__thumbnail')) ?? null;
			$news_type = get_the_terms($post->ID, "news_type") ?? "";
			$thematique = get_the_terms($post->ID, "thematiques") ? get_the_terms($post->ID, "thematiques")[0] : null;
			$post_date = get_the_date('j.m.Y', $post->ID) ?? null;
			get_template_part(
				'template-components/cards/card-news',
				null,
				array(
					'card_variant' => $news_type[0]->name,
					'post_ID' => $post->ID,
					'post_title' => get_the_title($post->ID),
					'post_thumbnail' => $post_thumbnail,
					'thematique' => $thematique,
					'post_date' => $post_date,
				)
			);

			?>
		<?php endforeach; ?>
		<?php if ($pressPosts->found_posts === 0) : ?>
			<p class="no-posts-found"> <?php echo __("Pas de communiqué de presse actuellement", "homegrade-blocks")  ?> </p>
		<?php endif; ?>
	</div>
	<?php if ($pressPosts->max_num_pages > 1) : ?>
		<a class="cta cta--outline cta--button mx-auto read-more-communiques" href="<?php echo get_permalink($newsTemplatePage) ?>">
			<?php echo __("Lire les autres communiqués", "homegrade-blocks")  ?>
		</a>
	<?php endif; ?>
</section>