<?php
require_once 'config.php';
require_once 'db.php';
require_once 'functions.php';

// Get categories for navigation
$categories = getCategories($pdo);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="<?php echo SITE_URL; ?>/assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <header>
        <div class="container">
            <div class="logo">
                <h1><a href="<?php echo SITE_URL; ?>/index.php"><?php echo SITE_NAME; ?></a></h1>
            </div>
            
            <nav>
                <ul>
                    <li><a href="<?php echo SITE_URL; ?>/index.php">Home</a></li>
                    <li class="dropdown">
                        <a href="#">Categories <i class="fas fa-chevron-down"></i></a>
                        <ul class="dropdown-menu">
                            <?php foreach ($categories as $category): ?>
                                <li><a href="<?php echo SITE_URL; ?>/products/index.php?category=<?php echo urlencode($category['category']); ?>"><?php echo htmlspecialchars($category['category']); ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    </li>
                    <li><a href="<?php echo SITE_URL; ?>/products/index.php">Products</a></li>
                    <?php if (isLoggedIn()): ?>
                        <li><a href="<?php echo SITE_URL; ?>/favorites.php">Favorites</a></li>
                        <li><a href="<?php echo SITE_URL; ?>/tickets/my_tickets.php">Support</a></li>
                    <?php endif; ?>
                </ul>
            </nav>
            
            <div class="header-actions">
                <?php if (isLoggedIn()): ?>
                    <a href="<?php echo SITE_URL; ?>/auth/login.php?action=logout" class="btn btn-outline">Logout</a>
                    <?php if (isAdmin()): ?>
                        <a href="<?php echo SITE_URL; ?>/admin/index.php" class="btn btn-primary">Admin Panel</a>
                    <?php endif; ?>
                <?php else: ?>
                    <a href="<?php echo SITE_URL; ?>/auth/login.php" class="btn btn-outline">Login</a>
                    <a href="<?php echo SITE_URL; ?>/auth/register.php" class="btn btn-primary">Register</a>
                <?php endif; ?>
                
                <a href="<?php echo SITE_URL; ?>/cart/index.php" class="cart-icon">
                    <i class="fas fa-shopping-cart"></i>
                    <span class="cart-badge" id="cart-count">0</span>
                </a>
            </div>
        </div>
    </header>
    
    <script>
        // Pass user data to JavaScript
        window.userId = <?php echo json_encode(isLoggedIn() ? $_SESSION['user_id'] : null); ?>;
        window.siteUrl = '<?php echo SITE_URL; ?>';
    </script>
    
    <main>