<?php /* Template Name: News | Archive   */
get_header();

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

$posts_per_page = 12;
$args = array(
	"post_type" => "post",
	"status" => "publish",
	"posts_per_page" => $posts_per_page,
	// "paged" => 1,
);
$posts = new WP_Query($args);


?>
<div class="template-archives template-archives--news">
	<?php /* --------
		BREADCRUMB
 ---------------*/ ?>
	<nav class="breadcrumbs_navigation" aria-label="<?php echo __("Vous êtes ici", "homegrade-theme__texte-fonctionnel") ?>">
		<ol>
			<li>
				<a href="<?php echo home_url() ?>" title="<?php echo __("Accueil", "homegrade-theme__texte-fonctionnel") ?>">
					<img src="<?php echo get_template_directory_uri() . "/resources/img/pictogrammes/icon_house_dark.svg" ?>" alt="">
				</a>
			</li>
			<li><a href="<?php echo get_the_permalink($relatedPageTemplatePage) ?>" aria-disabled="true" aria-current='location'>
					<?php echo __("News", "homegrade-theme__texte-fonctionnel") ?>
				</a></li>
		</ol>
	</nav>


	<?php /* --------
		HEADING BOX
 ---------------*/ ?>
	<?php
	get_template_part("template-components/heading-box", null, array(
		"pageIcon" => $pageIcon,
		"title" => get_the_title(get_queried_object_id()),
		"description" =>  __("Qu'y a-t-il de nouveau chez Homegrade ?", "homegrade-theme__texte-fonctionnel"),
	));
	?>



	<?php /* --------
		FILTERS
 ---------------*/ ?>
	<div class="filters-toolbar filters-toolbar--archive-news" data-active-news-term='all' data-active-page="1" data-posts-per-page="<?php echo $posts_per_page ?>" data-has-more-posts="true">
		<p class="filters-toolbar__posts-results-amount filter-disable" role="status">
			<span class="results-amount">
				<?php echo $posts->found_posts ?>
			</span>
			<span class="results-text">
				<?php echo __("résultat(s)", "homegrade-theme__texte-fonctionnel"); ?>
			</span>

			<span class="results-for">
				<?php echo __("pour", "homegrade-theme__texte-fonctionnel") . " : "; ?>
			</span>
			<span class="results-filter-name">
				<?php echo __("categorie", "homegrade-theme__texte-fonctionnel"); ?>
			</span>

		</p>

		<ul class="news-type-filters-wrapper">
			<?php
			$news_types = get_terms(array(
				'taxonomy' => 'news_type',
				'hide_empty' => false,
			));

			?>
			<li>
				<button class="filters-toolbar__action-button" data-term-id="all" data-term-name="all"> <?php echo __("Tous", "homegrade-theme__texte-fonctionnel") ?></button>
			</li>
			<?php foreach ($news_types as $typeTerm) : ?>
				<?php

				$termIcon = get_field('news_type_icon', "news_type" . '_' . $typeTerm->term_id) ?? null;

				?>
				<li>
					<button class="filters-toolbar__action-button" data-term-id="<?php echo $typeTerm->term_id ?>" data-term-name="<?php echo $typeTerm->name ?>">
						<?php if ($termIcon) : ?>
							<img class="icon" src="<?php echo $termIcon['url'] ?>" alt='' />
						<?php endif; ?>

						<?php echo $typeTerm->name ?>

					</button>
				</li>
			<?php endforeach; ?>
		</ul>
	</div>


	<?php /* -------------
		POSTS CARDS GRID
 --------------------*/ ?>

	<section class="news-container">

		<div class="card-grid-container">

			<?php
			foreach ($posts->posts as $key => $post) {
				$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") ?? null;
				$post_date = get_the_date('j.m.Y', $post->ID) ?? null;

				get_template_part(
					'template-components/cards/card-news',
					null,
					array(
						'card_variant' => 'activite',
						'post_ID' => $post->ID,
						'post_title' => $post->post_title,
						'post_thumbnail' => $post_thumbnail,
						'news_type' => $news_type,
						'post_date' => $post_date,
					)
				);
			}
			?>
		</div>

		<?php if ($posts->max_num_pages > 1) : ?>
			<div class="load-news">
				<button id="load-all-news" class="cta cta--outline cta--button"><?php echo __("Charger Tout", "homegrade-theme__texte-fonctionnel") ?></button>
				<button id="load-more-news" class="cta cta--outline cta--button"><?php echo __("Charger Plus", "homegrade-theme__texte-fonctionnel") ?></button>
			</div>
		<?php endif; ?>
	</section>

</div>



<?php
get_footer();
