<?php
/**
 * Frontend Header - COMPLETE FIXED VERSION
 * Digital Product Store
 */

// Start output buffering
ob_start();

// Start session if not started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Include database and functions if not already included
if (!isset($pdo)) {
    require_once __DIR__ . '/config/database.php';
}

// Get theme colors
$theme_colors = get_theme_colors($pdo);

// Get cart count
$cart_count = get_cart_count();

// Get categories for menu
$menu_categories = get_categories();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo isset($page_title) ? $page_title . ' - ' : ''; ?><?php echo get_setting('site_name', 'Digital Store'); ?></title>
    <meta name="description" content="<?php echo get_setting('seo_description', 'Premium digital products at affordable prices'); ?>">
    <meta name="keywords" content="<?php echo get_setting('seo_keywords', 'digital products, software, downloads, ebooks'); ?>">
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <style>
        :root {
            --primary-color: <?php echo $theme_colors['primary_color']; ?>;
            --secondary-color: <?php echo $theme_colors['secondary_color']; ?>;
            --success-color: <?php echo $theme_colors['success_color']; ?>;
            --info-color: <?php echo $theme_colors['info_color']; ?>;
            --warning-color: <?php echo $theme_colors['warning_color']; ?>;
            --danger-color: <?php echo $theme_colors['danger_color']; ?>;
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background-color: #f8f9fa;
        }
        
        .navbar {
            background: #fff;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        }
        
        .navbar-brand {
            font-weight: 700;
            color: var(--primary-color) !important;
        }
        
        .nav-link {
            font-weight: 500;
            color: #333 !important;
        }
        
        .nav-link:hover {
            color: var(--primary-color) !important;
        }
        
        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
        }
        
        .btn-primary:hover {
            background-color: var(--secondary-color);
            border-color: var(--secondary-color);
        }
        
        .cart-badge {
            position: absolute;
            top: -5px;
            right: -5px;
            font-size: 10px;
        }
        
        .notification-banner {
            background-color: <?php echo get_setting('notification_bg_color', '#6366f1'); ?>;
            color: <?php echo get_setting('notification_text_color', '#ffffff'); ?>;
            padding: 10px 0;
            text-align: center;
        }
        
        .footer {
            background: #1a1a2e;
            color: #fff;
            padding: 60px 0 20px;
        }
        
        .footer a {
            color: rgba(255,255,255,0.7);
            text-decoration: none;
        }
        
        .footer a:hover {
            color: #fff;
        }
        
        .product-card {
            transition: transform 0.3s, box-shadow 0.3s;
        }
        
        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .price {
            color: var(--primary-color);
            font-weight: 700;
        }
        
        .sale-price {
            color: var(--danger-color);
        }
        
        .original-price {
            text-decoration: line-through;
            color: #999;
        }
    </style>
</head>
<body>
    <!-- Notification Banner -->
    <?php if (get_setting('notification_enabled', '1') === '1' && get_setting('notification_text')): ?>
    <div class="notification-banner">
        <?php echo htmlspecialchars(get_setting('notification_text')); ?>
    </div>
    <?php endif; ?>
    
    <!-- Navbar -->
    <nav class="navbar navbar-expand-lg navbar-light sticky-top">
        <div class="container">
            <a class="navbar-brand" href="<?php echo BASE_URL; ?>/index.php">
                <i class="fas fa-store me-2"></i><?php echo get_setting('site_name', 'Digital Store'); ?>
            </a>
            
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="<?php echo BASE_URL; ?>/index.php">Home</a>
                    </li>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Categories</a>
                        <ul class="dropdown-menu">
                            <?php foreach ($menu_categories as $cat): ?>
                            <li><a class="dropdown-item" href="<?php echo BASE_URL; ?>/category.php?slug=<?php echo $cat['slug']; ?>"><?php echo htmlspecialchars($cat['name']); ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?php echo BASE_URL; ?>/products.php">Products</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?php echo BASE_URL; ?>/contact.php">Contact</a>
                    </li>
                </ul>
                
                <ul class="navbar-nav align-items-center">
                    <!-- Search -->
                    <li class="nav-item me-3">
                        <form class="d-flex" action="<?php echo BASE_URL; ?>/search.php" method="GET">
                            <div class="input-group">
                                <input type="text" name="q" class="form-control" placeholder="Search...">
                                <button type="submit" class="btn btn-outline-primary">
                                    <i class="fas fa-search"></i>
                                </button>
                            </div>
                        </form>
                    </li>
                    
                    <!-- Cart -->
                    <li class="nav-item me-3">
                        <a class="nav-link position-relative" href="<?php echo BASE_URL; ?>/cart.php">
                            <i class="fas fa-shopping-cart fa-lg"></i>
                            <?php if ($cart_count > 0): ?>
                            <span class="badge bg-danger cart-badge"><?php echo $cart_count; ?></span>
                            <?php endif; ?>
                        </a>
                    </li>
                    
                    <?php if (is_user_logged_in()): ?>
                    <!-- User Menu -->
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">
                            <i class="fas fa-user-circle fa-lg"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><a class="dropdown-item" href="<?php echo BASE_URL; ?>/account/index.php"><i class="fas fa-user me-2"></i>My Account</a></li>
                            <li><a class="dropdown-item" href="<?php echo BASE_URL; ?>/account/orders.php"><i class="fas fa-shopping-bag me-2"></i>My Orders</a></li>
                            <li><a class="dropdown-item" href="<?php echo BASE_URL; ?>/account/downloads.php"><i class="fas fa-download me-2"></i>Downloads</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="<?php echo BASE_URL; ?>/logout.php"><i class="fas fa-sign-out-alt me-2"></i>Logout</a></li>
                        </ul>
                    </li>
                    <?php else: ?>
                    <!-- Login/Register -->
                    <li class="nav-item">
                        <a class="btn btn-primary" href="<?php echo BASE_URL; ?>/login.php">
                            <i class="fas fa-sign-in-alt me-2"></i>Sign In
                        </a>
                    </li>
                    <?php endif; ?>
                </ul>
            </div>
        </div>
    </nav>
    
    <!-- Main Content -->
    <main>
        <?php echo get_flash_message(); ?>
