<?php

require_once __DIR__ . '/../db/config.php';

$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="<?php echo BASE_URL; ?>index.php">
            <img src="<?php echo BASE_URL; ?>assets/pasted-20260109-102314-972f2719.jpg" alt="Logo" style="height: 40px;">
        </a>

        <div class="position-absolute start-50 translate-middle-x">
            <div class="d-flex flex-column align-items-center">
                <?php
                $company_name = 'ExTrading e-commerce';
                $client_info = null;
                if (isset($_SESSION['user_id']) && $_SESSION['user_role'] !== 'admin' && isset($_SESSION['client_id'])) {
                    $client_id = $_SESSION['client_id'];
                    $stmt = db()->prepare("SELECT name, credit_limit, credit_balance FROM clients WHERE id = ?");
                    $stmt->execute([$client_id]);
                    $client_info = $stmt->fetch(PDO::FETCH_ASSOC);
                    if ($client_info) {
                        $company_name = $client_info['name'];
                    }
                }
                ?>
                <span style="font-family: 'Montserrat', sans-serif; font-size: 20px; color: #333; white-space: nowrap;"><?php echo htmlspecialchars($company_name); ?></span>
                <?php
                if ($client_info) {
                    echo "<span class='text-muted' style='font-size: 12px;'>";
                    echo t('credit_limit') . ": " . format_money($client_info['credit_limit'], $current_lang, db());
                    echo " | " . t('credit_balance') . ": " . format_money($client_info['credit_balance'], $current_lang, db());
                    echo "</span>";
                }
                ?>
            </div>
        </div>

        <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="<?php echo BASE_URL; ?>index.php"><?= t('menu_catalog') ?></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="<?php echo BASE_URL; ?>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="<?php echo BASE_URL; ?>orders.php"><?= t('menu_orders') ?></a>
                </li>
                <?php if ($user_role === 'admin'): ?>
                    <li class="nav-item">
                        <a class="nav-link" href="<?php echo BASE_URL; ?>admin/products.php">Admin</a>
                    </li>
                <?php endif; ?>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="profileDropdown" 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" aria-labelledby="profileDropdown">
                        <li><a class="dropdown-item p-2" href="<?php echo BASE_URL; ?>profile.php"><?= t('menu_profile') ?></a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item p-2" href="<?php echo BASE_URL; ?>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 p-2<?php if ($current_lang === 'pl') echo ' active'; ?>" href="<?= $url_pl ?>">Polski (PL)</a></li>
                        <li><a class="dropdown-item p-2<?php if ($current_lang === 'en') echo ' active'; ?>" href="<?= $url_en ?>">English (EN)</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>