<!-- top nav area -->
<div class="topbg-color py-2">
    <div class="container-fluid">
        <div class="row py-1">

            <div class="col-xs-6 col-sm-6 col-md-12 col-xl-12 col-xxl-12">
                <div class="d-flex justify-content-left content_area">
                    <div class="t-icon"><i class="t-icon bi bi-telephone-outbound"></i> <?= h($contacts->phone1) ?>
                    </div>
                    &nbsp; &nbsp; | &nbsp; &nbsp;
                    <div class="t-icon"><i class="t-icon bi bi-envelope-fill"></i> <?= h($contacts->email1) ?> </div>
                </div>
            </div>

        </div>
    </div>
</div>

<div class="navbar-wrap">

    <nav id="navbar_top" class="navbar navbar-expand-lg bg-light navbar-fixed-top">
        <div class="container">
            <!-- logo area  -->
            <div class="logo-aspect-div">
                <?php echo $this->Html->link(
                    $this->Html->image($appSettings['SITE-LOGO']),
                    ['controller' => 'home'],
                    ['escape' => false, 'class' => "navbar-brands"]
                ); ?>
            </div>

            <!-- toggler on mobile view -->
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown"
                aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse d-md-flex justify-content-end me-3 align-items-end"
                id="navbarNavDropdown">

                <ul class="navbar-nav ">
                    <?php
                    // Get request parameters
                    $urlParams = $this->getRequest()->getAttribute('params');

                    // Determine menu slug based on parameters
                    $menuSlug = $urlParams['section'] ?? strtolower($urlParams['controller'] ?? '');

                    // Adjust menu slug if action is 'subMenu'
                    if ($urlParams['action'] === 'subMenu' && isset($urlParams['controller'])) {
                        $menuSlug = strtolower($urlParams['controller']);
                    }
                    ?>

                    <?php foreach ($menus as $menu):
                        // Determine if this menu item is active
                        $isActive = ($menu->slug === $menuSlug);

                        // Define the link class based on active status
                        $linkClass = $isActive ? 'nav-link activeLink' : 'nav-link';
                        ?>
                        <!-- check if menu button is not enable -->
                        <?php if ($menu->show_button !== 1): ?>

                            <!-- Menu without submenu -->
                            <?php if (!$menu->has('child_menus') || sizeof($menu->child_menus) <= 0) { ?>
                                <li class="nav-item">
                                    <?= $this->Html->link($menu->name, ['controller' => $menu->slug, 'action' => 'index'], ['class' => $linkClass]) ?>
                                </li>
                            <?php } else {
                                // Extract parts from the matched route to determine the parent menu slug
                                $urlParts = explode('/', $urlParams['_matchedRoute']);
                                $pMenuSlug = $urlParts[1];

                                // Check if the current menu is active
                                $isActive = ($menu->slug === $pMenuSlug);

                                // Combine link classes for active/inactive states
                                $linkClasses = $linkClass . ($isActive ? ' active activeLink' : '');
                                ?>

                                <!-- menu with submenu -->
                                <li class="nav-item dropdown">
                                    <a class="nav-link dropdown-toggle <?= $linkClasses ?>" href="<?= $this->Url->build(['controller' => $menu->slug, 'action' => 'index'])?>" role="button" data-bs-toggle="dropdown"
                                        aria-expanded="false">
                                        <?= h($menu->name) ?>
                                    </a>

                                    <!-- Drop down menus -->
                                    <ul class="dropdown-menu">
                                        <?php foreach ($menu->child_menus as $sub): ?>
                                            <li>
                                                <?= $this->Html->link($sub->name, ['controller' => $menu->slug, 'action' => $sub->slug], ['class' => 'dropdown-item']) ?>
                                            </li>
                                        <?php endforeach; ?>
                                    </ul>
                                </li>

                            <?php } ?>
                        <?php endif; ?>
                    <?php endforeach; ?>

                    <!-- menus in a button form -->
                    <?php foreach ($menus as $menu): ?>
                        <?php if ($menu->show_button == 1): ?>
                            <li class="nav-item">
                                <button class="btn btn-danger my-2 my-sm-0 mx-2" type="button">
                                    <?= $this->Html->link($menu->name, ['controller' => $menu->slug, 'action' => 'index'], ['class' => 'nav-links']) ?>
                                </button>
                            </li>
                        <?php endif ?>
                    <?php endforeach ?>

                </ul>
            </div>
        </div>
    </nav>
</div>