<?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;
		$thematique_icon = get_field('taxonomy_pictures', 'thematiques' . '_' . $mainThematique->term_id)['icon'];

		$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 -->
			<article class="container my-8 mx-auto entry-content single-editor-content <?php echo $thematiqueColorSlug ? "entry-content--" . $thematiqueColorSlug : "" ?>">
				<?php the_content() ?>
			</article>

			<!-- PREVIOUS / NEXT ARTICLES -->
			<div class="previous-next-questions">
				<?php
				$args = array(
					'post_type' => 'questions',
					'posts_per_page' => 2,
					'post_status' => 'publish',
					'orderby' => 'rand',
					'post__not_in' => array(get_the_ID()),
					'tax_query' => 	array(
						array(
							'taxonomy' => 'thematiques',
							'field' => 'term_id',
							'terms' => $mainThematique->term_id,
						)
					)
				);
				$otherQuestions = new WP_Query($args);
				$previousPost = $otherQuestions->posts[0];
				$nextPost = $otherQuestions->posts[1];

				?>

				<?php if ($nextPost) : ?>
					<a class="previous-next-questions__next" href="<?php echo get_the_permalink($nextPost->ID) ?>">
						<div class="previous-next-questions__link-content">
							<p class="question_type"><?php echo __(" Question suivante ", "homegrade-theme__texte-fonctionnel") ?></p>
							<p class="question_title"><?php echo $nextPost->post_title ?></p>
						</div>
						<img class="thematique_icon" src="<?php echo $thematique_icon['url'] ?>" alt="">
					</a>
				<?php endif; ?>

				<?php if ($previousPost) : ?>
					<a class="previous-next-questions__previous" href="<?php echo get_the_permalink($previousPost->ID) ?>">
						<img class="thematique_icon" src="<?php echo $thematique_icon['url'] ?>" alt="">
						<div class="previous-next-questions__link-content">
							<p class="question_type"><?php echo __("Question précédente ", "homegrade-theme__texte-fonctionnel") ?></p>
							<p class="question_title"><?php echo $previousPost->post_title ?></p>
						</div>
					</a>
				<?php endif; ?>

			</div>

			<!-- RELATED CONSEILS -->
			<?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",

			)); ?>

			<section class="suggested-publications">
				<!-- PUBLCIATIONS SUGGÉRÉS -->
				<div class="card-large-content-container">
					<?php
					$args = array(
						'post_type' => 'brochures',
						'posts_per_page' => 5,
						'post_status' => 'publish',
						'orderby' => 'rand',
						'meta_key'      => 'brochure_pdf',
						'meta_value' => false,
						'meta_compare' => '!=',
						'tax_query' => 	array(
							array(
								'taxonomy' => 'thematiques',
								'field' => 'term_id',
								'terms' => $mainThematique->term_id,
							)
						)
					);
					$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-fonctionnel") ?></a>
					</div>
				</div>
			</section>

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


<?php
get_footer();
