<?php
get_header();
?>


<?php if (have_posts()) : ?>
	<?php
	while (have_posts()) :
		the_post();
		$currentThematique = get_the_terms(get_the_ID(), 'thematiques')[0];
		$mainThematique = getMainThematique($currentThematique);
		$thematiqueColorSlug = $mainThematique->slug;

		$args = array(
			'post_type' => 'conseils',
			'tax_query' => array(
				array(
					'taxonomy' => 'thematiques',
					'field' => 'slug',
					'terms' => $mainThematique->slug,
				)
			)
		);
		$relatedConseils = new WP_Query($args);
		$relatedConseilsUrl = get_permalink($relatedConseils->posts[0]->ID) ?? null;

	?>
		<div class="post-question-page-container <?php echo $thematiqueColorSlug ? "post-question-page--" . $thematiqueColorSlug : "" ?>">

			<?php get_template_part('template-components/post-questions/chapter-header', null, array(
				'postID' => get_the_ID(),
			)); ?>

			<article class="container my-8 mx-auto entry-content single-editor-content <?php echo $thematiqueColorSlug ? "entry-content--" . $thematiqueColorSlug : "" ?>">


				<?php the_content() ?>
			</article>

			<?php get_template_part('template-components/post-conseils/chapter-header', null, array(
				'postID' => get_the_ID(),
				'subtitle' => $mainThematique->name,
				'title' =>   __("Les conseillères et conseillers Homegrade vous guident", "homegrade-theme__texte-fonctionnel"),
				'hasInfoCapsule' => false,
				'ctaUrl' => $relatedConseilsUrl,
				'ctaTitle' => __("Parcourir nos conseils ", "homegrade-theme__texte-fonctionnel"),
				'ctaTarget' => "_self",

			)); ?>

			<!-- PUBLCIATIONS SUGGÉRÉS -->
			<div class="card-large-content-container">
				<?php
				$args = array(
					'post_type' => 'brochures',
					'posts_per_page' => 5,
					'orderby' => 'rand',
					'post_status' => 'publish',
					'meta_query' => array(
						array(
							'key' => 'brochure_pdf',
							'compare' => 'EXISTS'
						)
					),
				);
				$suggestedPublications = new WP_Query($args);
				$pagePublicationsUrl = get_page_by_template('template-publications.php') ? get_permalink(get_page_by_template('template-publications.php')[0]->ID) :  null;
				?>
				<div class="card-large-content">
					<div class="section_titling">
						<h2 class="section_titling__title"><?php echo __("Publications suggérées", "homegrade-theme__texte-fonctionnel") ?></h2>
						<p class="section_titling__subtitle"><?php echo __("Consultez notre documentation pour en savoir plus", "homegrade-theme__texte-fonctionnel") ?></p>
					</div>
					<ul id="brochures-rows" class="publications-grid">
						<?php
						get_template_part(
							'template-components/archives/brochure-rows',
							null,
							array(
								'brochuresPosts' => $suggestedPublications,
							)
						)
						?>
					</ul>
					<a class="cta cta--outline cta--button mx-auto" href="<?php echo $pagePublicationsUrl ?>"><?php echo __("Consulter toutes les publications", "homegrade-theme__texte-fonctionel") ?></a>
				</div>
			</div>
		</div>
	<?php endwhile; ?>
<?php endif; ?>


<?php
get_footer();
