<?php

use Jet\MVC;
use Jet\MVC_View;
use Jet\MVC_Page;
use Jet\UI;

/**
 * @var MVC_View $this
 * @var MVC_Page[] $pages
 */
$pages = $this->getRaw( 'page_tree_current' );

if( !MVC::getPage()->accessAllowed() ) {
	return;
}


$show_menu = function( $pages, $level = 0 ) use ( &$show_menu ) {
	/**
	 * @var MVC_Page[] $pages
	 */

	if( !$pages ) {
		return;
	}

	$current_page_key = MVC::getPage()->getKey();
	$padding = 10 + ($level * 25);

	foreach( $pages as $page ):
		if(
			!$page->getIsActive() ||
			!$page->getIsSecret() ||
			!$page->accessAllowed() ||
			$page->getParameter('do_not_display_in_menu', false)
		) {
			continue;
		}

		$pd = $padding;
		$icon = '';
		if( $page->getIcon() ) {
			$icon = UI::icon( $page->getIcon() ) . ' ';
		} else {
			$pd += 10;
		}

		?>
		<?php if( $current_page_key == $page->getKey() ): ?>
		<li class="list-group-item active" style="padding-left: <?= $pd; ?>px;">
			<?= $icon ?><?= $page->getMenuTitle(); ?>
		</li>
	<?php else: ?>
		<li class="list-group-item" style="padding-left: <?= $pd; ?>px;">
			<?= $icon ?><a href="<?= $page->getURL(); ?>"><?= $page->getMenuTitle(); ?></a>
		</li>
	<?php endif; ?>
		<?php $show_menu( $page->getChildren(), $level + 1 ); ?>
	<?php
	endforeach;

};
?>


<div class="card bg-danger text-white">
	<div class="card-body">
		<ul class="list-group list-group-flush">
			<?php $show_menu( $pages ); ?>
		</ul>
	</div>
</div>
