<?php

switch_to_blog(1);
$menu_name = 'renovateur';
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menuitems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC'));
$logoID = get_theme_mod('custom_logo');
$logoSRC = wp_get_attachment_image_src($logoID, 'full');
?>

<div class="menu-renovateur-container">
	<div class="menu-renovateur">
		<div class="website_logo">
			<?php if (has_custom_logo()) { ?>
				<a id="website-logo-link" href="<?php echo home_url() ?>" tabindex="0">
					<img src="<?php echo $logoSRC[0] ?>" alt="<?php echo __("Accueil Homegrade.brussels", "homegrade-theme__texte-fonctionnel") ?>">
				</a>
				<!-- <?php the_custom_logo(); ?> -->
			<?php } else { ?>
				<a href=" <?php echo get_bloginfo('url'); ?>" class="font-extrabold text-lg uppercase">
					<?php echo get_bloginfo('name'); ?>
				</a>
			<?php } ?>
		</div>

		<div id="menu-renovateur-navlist-container" class="menu-renovateur__navlist-container" closed>
			<ul id="menu-menu-renovateur" class="menu-renovateur__navlist mobile-navlist">
				<?php
				$count = 0;
				$submenu = false;

				foreach ($menuitems as $key => $item) :
					$link = $item->url;
					$title = $item->title;
				?>

					<?php
					/* -----------------------------------------------------------
			IF NOT A SUBMENU
			-----------------------------------------------------------*/
					if (!$item->menu_item_parent) : ?>
						<?php
						$parent_id = $item->ID;
						$page_icon = get_field('page_icon', $item->object_id) ?? null;
						?>

						<?php
						/* --------------
						IF HAS A SUBMENU
						----------------*/
						?>
						<?php if (isset($menuitems[$key + 1]->menu_item_parent) && $menuitems[$key + 1]->menu_item_parent  == $item->ID) : ?>
							<li id="menu-item-<?php echo $item->ID ?>" class="menu-item menu-item-has-children">
								<button type="button" class="menu-item__submenu-toggle" aria-expanded="false">
									<?php if ($page_icon) : ?>
										<img class="page_icon" src="<?php echo $page_icon['url'] ?>" alt="" />
									<?php endif; ?>
									<?php echo $title; ?>
								</button>

								<!-- HAS NO SUBMENU -->
							<?php else : ?>
							<li id="menu-item-<?php echo $item->ID ?>" class="menu-item ">

								<a href=" <?php echo $link; ?>" class="title">
									<?php if ($page_icon) : ?>
										<img class="page_icon" src=" <?php echo $page_icon['url'] ?>" alt="" />
									<?php endif; ?>
									<?php echo $title; ?>
								</a>
							<?php endif; ?>


						<?php endif; ?>

						<?php
						/* -----------------------------------------------------------
				IF IS A SUBMENU
				-----------------------------------------------------------*/
						if (isset($parent_id) &&  $parent_id == $item->menu_item_parent) : ?>
							<?php if (!$submenu) : $submenu = true; ?>
								<div class="sub-menu sub-menu--closed" id="submenu-<?php echo $item->menu_item_parent ?>">

									<button class="menu-item__submenu-close" id="submenu-toggle-<?php echo $item->menu_item_parent ?>" aria-label="<?php echo __("Fermer le sous-menu", "homegrade-theme__texte-fonctionnel") ?>">
										<img src="<?php echo get_template_directory_uri() . '/resources/img/graphic-assets/close_submenu_icon.svg' ?>" alt="">
									</button>
									<?php /* NOS CONSEILS — TAXONOMY SUBMENU TITLE */
									if ($item->menu_item_parent === "168") : ?>
										<h5 class="menu-item__submenu-title"><?php echo __('Découvrez nos conseils par thématique', 'homegrade-theme__texte-fonctionnel') ?></h5>
									<?php endif; ?>
									<?php /* AIDES FINANCIERES SUBMENU TITLE */
									if ($item->menu_item_parent === "169") : ?>
										<h5 class="menu-item__submenu-title"><?php echo __('Quelles sont les aides financières disponibles pour votre logement ?', 'homegrade-theme__texte-fonctionnel') ?></h5>

									<?php endif; ?>

									<ul class="sub-menu__navlist">
									<?php endif; ?>

									<li class="submenu-item sub-menu__navlist__item">

										<?php // #### WHEN IS POST TYPE CONSEIL SUB ITEM
										?>

										<?php if ($item->type === "post_type" && $item->type_label === "Conseil") : ?>
											<?php
											$postThematique = get_the_terms($item->object_id, "thematiques")[0];
											$postParentThematique = getMainThematique($postThematique);
											$thematiqueCover = get_field('taxonomy_pictures', "thematiques_" . $postParentThematique->term_id);
											$thematiqueColorslug = getThematiqueFamilySlug($postParentThematique->slug);
											?>
											<?php if ($postThematique) : ?>

												<a href="<?php echo $item->url ?>">
													<img class="sub-menu__navlist__thematique-icon" src="<?php echo $thematiqueCover['icon']['url'] ?>" alt="">
													<div class="details">
														<h6 class="<?php echo $thematiqueColorslug ? "text-" .  $thematiqueColorslug : "" ?>"><?php echo $postThematique->name ?></h6>
														<p class="sub-menu__navlist__description"><?php echo $postParentThematique->description ?></p>
													</div>
												</a>
											<?php endif; ?>

											<?php // WHEN THE SUB-LINK IS A TAXONOMY TERM 
											?>
										<?php elseif ($item->type === "taxonomy") : ?>
											<?php
											$term = get_term($item->object_id, 'thematiques');
											$mainTaxonomyParent = getMainThematique($term);
											$thematiqueColorslug = getThematiqueFamilySlug($term->slug);
											$args = array(
												'post_type' => 'conseils',
												'posts_per_page' => 1,
												'tax_query' => array(
													array(
														'taxonomy' => 'thematiques',
														'field' => 'slug',
														'terms' => $mainTaxonomyParent->slug,
														'include_children' => false,
													),
												),
											);

											$taxonomyRelatedPost = get_posts($args)[0] ?? null;
											$cover = get_field('taxonomy_pictures', "thematiques_" . $mainTaxonomyParent->term_id);
											?>
											<?php if ($taxonomyRelatedPost) : ?>
												<a href="<?php echo get_permalink($taxonomyRelatedPost->ID) ?>">
													<?php if ($cover && $cover['icon']) : ?>
														<img class="sub-menu__navlist__thematique-icon" src="<?php echo $cover['icon']['url'] ?>" alt="">
													<?php endif; ?>
													<div class="details">
														<h6><?php echo $title; ?></h6>
														<p class="sub-menu__navlist__description"><?php echo $mainTaxonomyParent->description ?></p>
													</div>
												</a>
											<?php else : ?>
												<a href="<?php echo "#" ?>">
													<?php if ($cover) : ?>
														<img class="sub-menu__navlist__thematique-icon" src="<?php echo $cover['icon']['url'] ?>" alt="">
													<?php endif; ?>
													<div class="details">
														<h6><?php echo $title; ?></h6>
														<p class="sub-menu__navlist__description"><?php echo $mainTaxonomyParent->description ?></p>
														<p class="!text-red-500 text-xs uppercase">pas de page conseil liée à cette thématique</p>
													</div>
												</a>
											<?php endif; ?>

											<!-- IF CLASSIC SUBMENU -->
										<?php else : ?>
											<a href="<?php echo $link; ?>">
												<p class="title"><?php echo $title; ?></p>
												<p class="excerpt"><?php echo get_the_excerpt($item->object_id) ?></p>
											</a>

										<?php endif; ?>
									</li>

									<?php if ($menuitems[$count + 1]->menu_item_parent != $parent_id && $submenu) : ?>
									</ul>
								</div>
							<?php $submenu = false;
									endif; ?>

						<?php endif; ?>

						<?php if (isset($parent_id) && isset($menuitems[$count + 1]->menu_item_parent) && $menuitems[$count + 1]->menu_item_parent  != $parent_id) : ?>
							</li>
						<?php $submenu = false;
						endif; ?>

					<?php $count++;
				endforeach; ?>

			</ul>
		</div>


		<div class="menu-renovateur__interaction_toggles">
			<button id="search-module-toggle" class="menu-renovateur__search-module-toggle cta cta--circular cta--outline cta--outline-secondary !border border-secondary" aria-expanded="false">
				<img src="<?php echo get_template_directory_uri() . '/resources/img/graphic-assets/search-icon.svg' ?>" alt="<?php echo __("Recherche", "homegrade-theme__texte-fonctionnel") ?>">
			</button>
			<button id="mobile-menu-toggle" class="menu-renovateur__mobile-menu-toggle cta cta--outline cta--button" data-text-open="<?php echo __("Menu", 'homegrade-theme__texte-fonctionnel') ?>" data-text-close="<?php echo __("Fermer", 'homegrade-theme__texte-fonctionnel') ?>" aria-expanded="false">
				<span class="text-content">
					<?php echo __("Menu", 'homegrade-theme__texte-fonctionnel') ?>
				</span>
				<div class="toggle-icon">
					<div class="toggle-icon__bar toggle-icon__bar--top"></div>
					<div class="toggle-icon__bar toggle-icon__bar--middle"></div>
					<div class="toggle-icon__bar toggle-icon__bar--bottom"></div>
				</div>

			</button>
		</div>

		<?php
		//  get_template_part('template-components/header/search-module');
		?>
	</div>
</div>

<?php
restore_current_blog();
?>