<?php
require_once __DIR__ . '/i18n.php';

function gp_render_project_menu(PDO $pdo, int $projectId): void
{
    if ($projectId <= 0) return;
    $currentFile = basename($_SERVER['PHP_SELF']);

    $financialFiles = ['expenses.php','payments.php','employee_expenses.php'];
    $materialFiles  = ['equipment.php','sold_equipment.php'];
    $teamFiles      = ['assign_send.php','partners.php'];
    $mediaFiles     = ['photos.php','map.php'];

    $tabs = [
        [
            'file'  => 'view.php',
            'label' => 'Επισκόπηση',
            'icon'  => '📁',
            'url'   => "/gopools/public/projects/view.php?id=$projectId",
        ],
        [
            'file'  => 'edit.php',
            'label' => gp_tr($pdo, 'edit'),
            'icon'  => '✏️',
            'url'   => "/gopools/public/projects/edit.php?id=$projectId",
        ],
        [
            'file'  => 'tasks.php',
            'label' => gp_tr($pdo, 'tasks'),
            'icon'  => '✅',
            'url'   => "/gopools/public/projects/tasks.php?project_id=$projectId",
        ],
        // ── Financials group ──────────────────────────────────────────────
        [
            'file'  => 'payments.php',
            'label' => 'Οικονομικά',
            'icon'  => '💰',
            'url'   => "/gopools/public/projects/payments.php?project_id=$projectId",
            'groupFiles' => $financialFiles,
            'sub'   => [
                ['file' => 'payments.php',        'label' => 'Πληρωμές',        'icon' => '💳', 'url' => "/gopools/public/projects/payments.php?project_id=$projectId"],
                ['file' => 'expenses.php',         'label' => 'Έξοδα Έργου',    'icon' => '💸', 'url' => "/gopools/public/projects/expenses.php?project_id=$projectId"],
                ['file' => 'employee_expenses.php','label' => 'Έξοδα Υπαλλήλων','icon' => '👤', 'url' => "/gopools/public/projects/employee_expenses.php?project_id=$projectId"],
            ],
        ],
        // ── Materials group ───────────────────────────────────────────────
        [
            'file'  => 'equipment.php',
            'label' => 'Υλικά',
            'icon'  => '🛠️',
            'url'   => "/gopools/public/projects/equipment.php?project_id=$projectId",
            'groupFiles' => $materialFiles,
            'sub'   => [
                ['file' => 'equipment.php',    'label' => 'Υλικά Έργου', 'icon' => '🔩', 'url' => "/gopools/public/projects/equipment.php?project_id=$projectId"],
                ['file' => 'sold_equipment.php','label' => 'Εγγυήσεις',  'icon' => '📜', 'url' => "/gopools/public/projects/sold_equipment.php?project_id=$projectId"],
            ],
        ],
        // ── Team group ────────────────────────────────────────────────────
        [
            'file'  => 'assign_send.php',
            'label' => 'Ομάδα',
            'icon'  => '👥',
            'url'   => "/gopools/public/projects/assign_send.php?project_id=$projectId",
            'groupFiles' => $teamFiles,
            'sub'   => [
                ['file' => 'assign_send.php','label' => 'Ανάθεση & Αποστολή','icon' => '🚀', 'url' => "/gopools/public/projects/assign_send.php?project_id=$projectId"],
                ['file' => 'partners.php',   'label' => 'Συνεργάτες',        'icon' => '🤝', 'url' => "/gopools/public/projects/partners.php?project_id=$projectId"],
            ],
        ],
        // ── Media group ───────────────────────────────────────────────────
        [
            'file'  => 'photos.php',
            'label' => 'Φωτο/Χάρτης',
            'icon'  => '📸',
            'url'   => "/gopools/public/projects/photos.php?project_id=$projectId",
            'groupFiles' => $mediaFiles,
            'sub'   => [
                ['file' => 'photos.php','label' => 'Φωτογραφίες','icon' => '📷', 'url' => "/gopools/public/projects/photos.php?project_id=$projectId"],
                ['file' => 'map.php',   'label' => 'Χάρτης',     'icon' => '📍', 'url' => "/gopools/public/projects/map.php?project_id=$projectId"],
            ],
        ],
    ];

    if (function_exists('gp_can_manage_project_access') && gp_can_manage_project_access($pdo)) {
        $tabs[] = ['file' => 'access.php', 'label' => gp_tr($pdo, 'access'), 'icon' => '🔑', 'url' => "/gopools/public/projects/access.php?project_id=$projectId"];
    }
    ?>
    <style>
    .project-nav-container {
      margin-bottom: 20px;
      position: sticky; top: 56px; z-index: 990;
      background: var(--gp-nav-bg-1);
      border-bottom: 1px solid var(--gp-card-border);
      margin-left: -20px; margin-right: -20px; margin-top: -20px;
      padding: 0 20px;
      backdrop-filter: blur(20px);
      -webkit-backdrop-filter: blur(20px);
    }
    [data-theme='dark'] .project-nav-container { background: rgba(15,23,42,0.85); }

    .project-nav-tabs {
      display: flex; align-items: center; gap: 2px;
      flex-wrap: wrap;
    }

    .nav-item-wrapper { position: relative; flex-shrink: 0; }

    .project-nav-link {
      display: flex; align-items: center; gap: 6px;
      padding: 12px 10px; font-size: 0.82rem; font-weight: 600;
      color: var(--gp-muted); text-decoration: none;
      white-space: nowrap; transition: all 0.18s;
      position: relative; border-radius: 6px 6px 0 0;
    }
    .project-nav-link:hover { color: var(--gp-text); background: rgba(0,0,0,0.02); }
    [data-theme='dark'] .project-nav-link:hover { background: rgba(255,255,255,0.03); }

    .project-nav-link.active { color: var(--gp-primary); }
    .project-nav-link.active::after {
      content: ""; position: absolute; bottom: 0; left: 8px; right: 8px;
      height: 3px; background: var(--gp-primary); border-radius: 3px 3px 0 0;
      box-shadow: 0 -2px 10px rgba(37,99,235,0.3);
    }
    .project-nav-link .nav-icon { font-size: 1rem; opacity: 0.85; }
    .project-nav-link .nav-arrow { font-size: 0.55rem; margin-left: 1px; opacity: 0.45; }

    /* Dropdown */
    .nav-dropdown {
      position: absolute; top: 100%; left: 0;
      min-width: 210px; background: var(--gp-card-bg);
      border: 1px solid var(--gp-card-border); border-radius: 12px;
      box-shadow: 0 15px 35px rgba(0,0,0,0.12);
      padding: 6px; display: none; flex-direction: column; gap: 2px;
      z-index: 1000; animation: navSlideDown 0.18s ease;
      backdrop-filter: blur(24px);
      margin-top: 2px;
    }
    /* Bridge: fills the gap between link and dropdown so hover is not lost */
    .nav-item-wrapper::after {
      content: ''; position: absolute;
      top: 100%; left: 0; right: 0; height: 8px;
      display: none;
    }
    .nav-item-wrapper:hover::after { display: block; }
    @keyframes navSlideDown { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }
    .nav-item-wrapper:hover .nav-dropdown { display: flex; }

    .nav-dropdown-item {
      display: flex; align-items: center; gap: 10px;
      padding: 9px 12px; border-radius: 8px; font-size: 0.83rem; font-weight: 600;
      text-decoration: none; color: var(--gp-text); transition: all 0.18s;
    }
    .nav-dropdown-item:hover { background: rgba(0,0,0,0.04); padding-left: 16px; }
    [data-theme='dark'] .nav-dropdown-item:hover { background: rgba(255,255,255,0.05); }
    .nav-dropdown-item.active { background: rgba(37,99,235,0.08); color: var(--gp-primary); }
    .nav-dropdown-divider { height: 1px; background: var(--gp-card-border); margin: 3px 6px; }

    @media (max-width: 1024px) {
      .project-nav-container { margin-left: -20px; margin-right: -20px; padding: 0 16px; }
      .project-nav-link { padding: 12px 9px; font-size: 0.79rem; }
    }
    </style>

    <div class="project-nav-container">
      <div class="project-nav-tabs">
        <?php foreach ($tabs as $tab):
          $hasSub = !empty($tab['sub']);
          $groupFiles = $tab['groupFiles'] ?? [];
          $isActive = ($currentFile === $tab['file'])
                   || (!empty($groupFiles) && in_array($currentFile, $groupFiles, true));
        ?>
        <div class="nav-item-wrapper">
          <a href="<?php echo h($tab['url']); ?>" class="project-nav-link <?php echo $isActive ? 'active' : ''; ?>">
            <span class="nav-icon"><?php echo $tab['icon']; ?></span>
            <span><?php echo h($tab['label']); ?></span>
            <?php if ($hasSub): ?><span class="nav-arrow">▼</span><?php endif; ?>
          </a>
          <?php if ($hasSub): ?>
            <div class="nav-dropdown">
              <?php foreach ($tab['sub'] as $sub): ?>
                <a href="<?php echo h($sub['url']); ?>" class="nav-dropdown-item <?php echo $currentFile === $sub['file'] ? 'active' : ''; ?>">
                  <span><?php echo $sub['icon']; ?></span>
                  <span><?php echo h($sub['label']); ?></span>
                </a>
              <?php endforeach; ?>
            </div>
          <?php endif; ?>
        </div>
        <?php endforeach; ?>
      </div>
    </div>
    <script>
    // Mobile: tap to toggle dropdowns (hover doesn't work on touch)
    (function(){
      if (window.matchMedia('(max-width: 768px)').matches) {
        document.querySelectorAll('.nav-item-wrapper').forEach(function(wrap) {
          var link = wrap.querySelector('.project-nav-link');
          var dropdown = wrap.querySelector('.nav-dropdown');
          if (!dropdown) return;
          link.addEventListener('click', function(e) {
            e.preventDefault();
            var isOpen = wrap.classList.contains('mobile-open');
            document.querySelectorAll('.nav-item-wrapper.mobile-open').forEach(function(w){ w.classList.remove('mobile-open'); });
            if (!isOpen) wrap.classList.add('mobile-open');
          });
        });
      }
    })();
    </script>
    <?php
}
