<?php
$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 menu-renovateur--footer">

	<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) :
				// write_log("item");
				// write_log($item);
				$link = $item->url;
				$title = $item->title;

				// $pageIcon = get_field('page_icon', $item->ID);

				// echo '<pre>';
				// print_r($item);
				// print_r($pageIcon);
				// echo '</pre>';
				// item does not have a parent so menu_item_parent equals 0 (false)
			?>

				<?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'] ?>" />
								<?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'] ?>" />
								<?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
									// #### IF IS TAXONOMY SUB-ITEM
									if ($item->type === "taxonomy") :

										$term = get_term_by('id', $item->object_id, 'thematiques');
										$cover = get_field('taxonomy_pictures', "thematiques_" . $item->object_id);

									?>
										<a href="<?php echo get_term_link($term) ?>">
											<img class="sub-menu__navlist__thematique-icon" src="<?php echo $cover['icon']['url'] ?>" alt="">
											<div class="details">
												<h6><?php echo $title; ?></h6>
												<p class="sub-menu__navlist__description"><?php echo $term->description ?></p>
											</div>
										</a>
									<?php else : ?>

										<a href="<?php echo $link; ?>" class="title"><?php echo $title; ?></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>