<?php
get_header();

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

		<div class="post-conseil-page-container <?php echo $thematiqueColorSlug ? "post-conseil-page--" . $thematiqueColorSlug : "" ?>">
			<nav class=" breadcrumbs_navigation" aria-label="<?php echo __("Vous êtes ici", "homegrade-theme__texte-fonctionnel") ?>">
				<?php
				$archiveConseils = get_page_by_template('template-archive-conseils.php')[0];
				?>
				<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_post_permalink($archiveConseils)  ?>"><?php echo $archiveConseils->post_title ?></a></li>
					<?php if ($mainThematique != $currentThematique) : ?>
						<?php
						$args = array(
							'post_type' => 'conseils', // the post type
							'tax_query' => array(
								array(
									'taxonomy' => 'thematiques', // the custom vocabulary
									'field'    => 'term_id', // term_id, slug or name
									'terms'    => array($mainThematique->term_id),
									'include_children' => false,
								),
							),
						);
						$mainThematiqueRelatedConseil = get_posts($args)[0] ?? null;
						?>
						<li><a href="<?php echo get_post_permalink($mainThematiqueRelatedConseil)  ?>"><?php echo $mainThematique->name ?></a></li>

					<?php endif; ?>
					<li><a href="<?php echo the_permalink()  ?>" aria-current="location" aria-disabled="true"><?php echo $currentThematique->name ?></a></li>

				</ol>
			</nav>


			<?php /* -------
			CHAPTER HEADER	
 			---------------*/ ?>
			<?php
			$blocks = parse_blocks($post->post_content);
			$chapterBlockIndex  = build_page_chapter_index($blocks); // see utilities.php
			$queryArgs = array(
				'post_type'   => 'brochures',
				'posts_per_page' => 1,
				'status' => 'publish',
				'tax_query' => array(
					array(
						'taxonomy' => 'thematiques',
						'field' => 'term_id',
						'terms' => $currentThematique->term_id,
						'include_children' => false,
					)
				)
			);
			$relatedBrochuresPosts = get_posts($queryArgs);
			$relatedBrochure = get_field('brochure_pdf', $relatedBrochuresPosts[0]->ID) ?? null;

			$hasManualDocument = get_field('manual_source_document') ?? null;
			$manualSrcDocument = get_field('manual_document_link');

			$relatedDocumentUrl = ($hasManualDocument && $manualSrcDocument && $manualSrcDocument['source_document']) ? $manualSrcDocument['source_document']['url'] : $relatedBrochure['url'] ?? null;
			$relatedDocumentCtaTitle = ($hasManualDocument && $manualSrcDocument && $manualSrcDocument['button_title']) ? $manualSrcDocument['button_title'] : __("Télécharger la brochure ", "homegrade-theme__texte-fonctionnel");

			get_template_part('template-components/post-conseils/chapter-header', null, array(
				'postID' => get_the_ID(),
				'subtitle' => __("Les conseillers Homegrade vous guident", "homegrade-theme__texte-fonctionnel"),
				'title' => get_the_title(get_the_ID()),
				'ctaUrl' =>  $relatedDocumentUrl,
				'ctaTitle' => $relatedDocumentCtaTitle,
				'hasInfoCapsule' => true,
			));
			?>

			<?php /* -------
				CONSEIL
 			---------------*/ ?>
			<div class=" conseils_wrapper">
				<!-- CHAPTER INDEX -->
				<aside>
					<?php if ($chapterBlockIndex) {
						get_template_part('template-components/post-conseils/chapter-index', null, array(
							'chapterBlockIndex' => $chapterBlockIndex,
						));
					} ?>

					<?php
					if ($mainThematique->term_id == 59 || $mainThematique->term_id == 32) {
						get_template_part('template-components/post-conseils/find-artisan', null, array());
					}
					?>

				</aside>

				<!-- ARTICLE -->
				<article class="entry-content single-editor-content <?php echo $thematiqueColorSlug ? "entry-content--" . $thematiqueColorSlug : "" ?>">
					<?php the_content() ?>
				</article>


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

<?php
get_footer();
