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

if (isset($_GET['lang'])) {
    set_lang($_GET['lang']);
    $uri = $_SERVER['REQUEST_URI'];
    $url_parts = parse_url($uri);
    $path = $url_parts['path'];
    $query = [];
    if (isset($url_parts['query'])) {
        parse_str($url_parts['query'], $query);
        unset($query['lang']);
    }
    $new_url = $path;
    if (!empty($query)) {
        $new_url .= '?' . http_build_query($query);
    }
    header("Location: " . $new_url);
    exit;
}

$user_role = get_user_role();
$current_lang = get_lang();
?>
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
    <div class="container-fluid">
        <a class="navbar-brand" href="index.php">
            <img src="assets/pasted-20251209-065617-6bf1b4e6.png" alt="Logo" style="height: 40px;">
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav ms-auto mb-2 mb-lg-0 align-items-center">
                <li class="nav-item">
                    <a class="nav-link" href="index.php"><?= t('menu_catalog') ?></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="cart.php">
                        <i class="bi bi-cart"></i> <?= t('title_cart') ?>
                        <span class="badge bg-primary rounded-pill"><?= count($_SESSION['cart'] ?? []) ?></span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="orders.php"><?= t('menu_orders') ?></a>
                </li>
                <?php if ($user_role === 'admin'): ?>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/products.php">Admin</a>
                    </li>
                <?php endif; ?>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="bi bi-person-circle"></i> <?= t('header_welcome') ?>, <?= isset($_SESSION['username']) ? htmlspecialchars($_SESSION['username']) : '' ?>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item p-2" href="profile.php"><?= t('menu_profile') ?></a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item p-2" href="logout.php"><?= t('menu_logout') ?></a></li>
                    </ul>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="langDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="bi bi-globe"></i> <?= strtoupper($current_lang) ?>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="langDropdown">
                        <?php 
    $uri = $_SERVER['REQUEST_URI'];
    $url_parts = parse_url($uri);
    $path = $url_parts['path'];
    
    $query_params_pl = $query_params_en = [];
    if (isset($url_parts['query'])) {
        parse_str($url_parts['query'], $query_params_pl);
        $query_params_en = $query_params_pl;
    }
    
    $query_params_pl['lang'] = 'pl';
    $query_params_en['lang'] = 'en';
    
    $url_pl = $path . '?' . http_build_query($query_params_pl);
    $url_en = $path . '?' . http_build_query($query_params_en);
?>
                        <li><a class="dropdown-item<?php if ($current_lang === 'pl') echo ' active'; ?>" href="<?= $url_pl ?>">Polski (PL)</a></li>
                        <li><a class="dropdown-item<?php if ($current_lang === 'en') echo ' active'; ?>" href="<?= $url_en ?>">English (EN)</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>