<?php
// kr/common/navi.php

// [경로 수정] __DIR__ 을 사용하여 navi.php가 위치한 폴더 기준에서 로더를 찾도록 변경
// 이렇게 하면 view.php에서 include될 때 경로 문제가 발생하지 않습니다.
if (!isset($menu_tree)) {
    require_once __DIR__ . '/menu_data_loader.php'; 
}

// URL 파라미터에서 현재 메뉴 ID를 가져옴
$current_page_id = $_GET['menu_id'] ?? 0;

if (!function_exists('getMenuLink')) {
    function getMenuLink($menu) {
        $url = "../contents/view.php?menu_id=" . $menu['id'];
        if (!empty($menu['extra_params'])) {
            $url .= '&' . ltrim($menu['extra_params'], '?&');
        }
        return $url;
    }
}
?>
<header id="cms-gnb" class="main-header">
	<div class="header-wrap">
		<h1 class="main-logo">
            <!-- 로고 클릭시 홈으로 (리소스 경로 주의) -->
            <a class="font-blind" href="../main/">정원엔시스<img src="../../resources/images/logo-main.png" alt="로고"></a>
        </h1>
		<nav class="gnb main-gnb">
		<ul class="gnb-depth1">
        <?php 
        if (!empty($menu_tree)) {
            foreach ($menu_tree as $depth1): 
                $isActive = ($depth1['id'] == $current_page_id);
                if (!$isActive && !empty($depth1['children'])) {
                    foreach ($depth1['children'] as $child) {
                        if ($child['id'] == $current_page_id) { $isActive = true; break; }
                        if (!empty($child['children'])) {
                            foreach ($child['children'] as $grandchild) {
                                if ($grandchild['id'] == $current_page_id) { $isActive = true; break 2; }
                            }
                        }
                    }
                }
                $active_class = $isActive ? ' on' : '';
        ?>
			<li class="<?= $active_class ?>">
                <a href="<?= getMenuLink($depth1) ?>"><?= htmlspecialchars($depth1['title']) ?></a>
                
                <?php if (!empty($depth1['children'])): ?>
                    <ul class="gnb-depth2">
                        <?php foreach ($depth1['children'] as $depth2): ?>
                            <li><a href="<?= getMenuLink($depth2) ?>"><?= htmlspecialchars($depth2['title']) ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
			</li>
        <?php 
            endforeach; 
        }
        ?>
		</ul>
		</nav>

        <!-- 모바일 메뉴 버튼 -->
        <button type="button" class="btn-m-menu" onclick="toggleMobileMenu(true)">
            <i class="fa-solid fa-bars"></i>
        </button>
	</div>
	<div class="gnb-bg"></div>

    <!-- 모바일 네비게이션 레이어 -->
    <div class="m-gnb-overlay" onclick="toggleMobileMenu(false)"></div>
    <div class="m-gnb-wrap">
        <div class="m-gnb-header">
            <img src="../../resources/images/logo-main.png" alt="로고" style="height:30px;">
            <button type="button" class="btn-m-close" onclick="toggleMobileMenu(false)">
                <i class="fa-solid fa-xmark"></i>
            </button>
        </div>
        <div class="m-gnb-body">
            <ul class="m-depth1">
                <?php if (!empty($menu_tree)): ?>
                    <?php foreach ($menu_tree as $depth1): ?>
                        <li>
                            <a href="<?= getMenuLink($depth1) ?>" class="m-link-d1">
                                <?= htmlspecialchars($depth1['title']) ?>
                                <?php if (!empty($depth1['children'])): ?>
                                    <i class="fa-solid fa-chevron-down"></i>
                                <?php endif; ?>
                            </a>
                            <?php if (!empty($depth1['children'])): ?>
                                <ul class="m-depth2">
                                    <?php foreach ($depth1['children'] as $depth2): ?>
                                        <li><a href="<?= getMenuLink($depth2) ?>"><?= htmlspecialchars($depth2['title']) ?></a></li>
                                    <?php endforeach; ?>
                                </ul>
                            <?php endif; ?>
                        </li>
                    <?php endforeach; ?>
                <?php endif; ?>
            </ul>
        </div>
    </div>
</header>