<?php
/**
 * Admin Header - PREMIUM VERSION
 * Digital Marketplace
 */

if (!isset($page_title)) {
    $page_title = 'Admin Panel';
}

$theme_colors = get_theme_colors($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 sanitize($page_title); ?> | <?php echo sanitize($theme_colors['site_name'] ?? 'Admin Panel'); ?></title>
    
    <!-- Favicon -->
    <link rel="icon" type="image/x-icon" href="<?php echo $theme_colors['favicon'] ? UPLOAD_URL . '/' . $theme_colors['favicon'] : ASSETS_URL . '/images/favicon.ico'; ?>">
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- Custom CSS -->
    <link rel="stylesheet" href="<?php echo ASSETS_URL; ?>/css/admin.css">
    
    <style>
    :root {
        --primary: <?php echo $theme_colors['primary_color'] ?? '#8b5cf6'; ?>;
        --secondary: <?php echo $theme_colors['secondary_color'] ?? '#6366f1'; ?>;
        --accent: <?php echo $theme_colors['accent_color'] ?? '#f59e0b'; ?>;
        --success: <?php echo $theme_colors['success_color'] ?? '#10b981'; ?>;
        --info: <?php echo $theme_colors['info_color'] ?? '#3b82f6'; ?>;
        --warning: <?php echo $theme_colors['warning_color'] ?? '#f59e0b'; ?>;
        --danger: <?php echo $theme_colors['danger_color'] ?? '#ef4444'; ?>;
    }
    </style>
</head>
<body class="admin-body">
    <!-- Sidebar -->
    <aside class="admin-sidebar">
        <div class="sidebar-header">
            <a href="<?php echo ADMIN_URL; ?>/index.php" class="sidebar-brand">
                <?php if (!empty($theme_colors['logo'])): ?>
                    <img src="<?php echo UPLOAD_URL . '/' . $theme_colors['logo']; ?>" alt="Logo" class="sidebar-logo">
                <?php else: ?>
                    <i class="fas fa-store"></i>
                <?php endif; ?>
                <span><?php echo sanitize($theme_colors['site_name'] ?? 'Marketplace'); ?></span>
            </a>
            <button class="sidebar-toggle d-lg-none">
                <i class="fas fa-times"></i>
            </button>
        </div>
        
        <nav class="sidebar-nav">
            <ul class="nav flex-column">
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/index.php" class="nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'index.php' ? 'active' : ''; ?>">
                        <i class="fas fa-tachometer-alt"></i>
                        <span>Dashboard</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/products.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'product') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-box"></i>
                        <span>Products</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/categories.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'categor') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-tags"></i>
                        <span>Categories</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/orders.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'order') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-shopping-cart"></i>
                        <span>Orders</span>
                        <?php 
                        $pending_orders = $stats['pending_orders'] ?? 0;
                        if ($pending_orders > 0): 
                        ?>
                        <span class="badge bg-danger ms-auto"><?php echo $pending_orders; ?></span>
                        <?php endif; ?>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/users.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'user') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-users"></i>
                        <span>Users</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/negotiations.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'negotiation') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-handshake"></i>
                        <span>Negotiations</span>
                        <?php 
                        $pending_negotiations = $stats['pending_negotiations'] ?? 0;
                        if ($pending_negotiations > 0): 
                        ?>
                        <span class="badge bg-warning ms-auto"><?php echo $pending_negotiations; ?></span>
                        <?php endif; ?>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/affiliates.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'affiliate') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-user-friends"></i>
                        <span>Affiliates</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/coupons.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'coupon') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-ticket-alt"></i>
                        <span>Coupons</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/reviews.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'review') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-star"></i>
                        <span>Reviews</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/pages.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'page') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-file-alt"></i>
                        <span>Pages</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/faqs.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'faq') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-question-circle"></i>
                        <span>FAQs</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/blog.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'blog') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-blog"></i>
                        <span>Blog</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/subscribers.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'subscriber') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-envelope"></i>
                        <span>Subscribers</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/email-settings.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'email') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-cog"></i>
                        <span>Email Settings</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/payments.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'payment') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-credit-card"></i>
                        <span>Payments</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo ADMIN_URL; ?>/settings.php" class="nav-link <?php echo strpos($_SERVER['PHP_SELF'], 'settings') !== false ? 'active' : ''; ?>">
                        <i class="fas fa-cogs"></i>
                        <span>Settings</span>
                    </a>
                </li>
                
                <li class="nav-item mt-auto">
                    <a href="<?php echo BASE_URL; ?>/index.php" class="nav-link" target="_blank">
                        <i class="fas fa-external-link-alt"></i>
                        <span>View Site</span>
                    </a>
                </li>
                
                <li class="nav-item">
                    <a href="<?php echo BASE_URL; ?>/logout.php" class="nav-link text-danger">
                        <i class="fas fa-sign-out-alt"></i>
                        <span>Logout</span>
                    </a>
                </li>
            </ul>
        </nav>
    </aside>
    
    <!-- Main Content -->
    <main class="admin-main">
        <!-- Top Header -->
        <header class="admin-topbar">
            <button class="sidebar-toggle d-lg-none">
                <i class="fas fa-bars"></i>
            </button>
            
            <div class="topbar-right">
                <div class="dropdown">
                    <button class="btn btn-link dropdown-toggle" type="button" data-bs-toggle="dropdown">
                        <i class="fas fa-user-circle fa-lg"></i>
                        <span class="ms-2 d-none d-md-inline"><?php echo sanitize($_SESSION['user_name'] ?? 'Admin'); ?></span>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item" href="<?php echo BASE_URL; ?>/account/profile.php"><i class="fas fa-user me-2"></i>Profile</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="<?php echo BASE_URL; ?>/logout.php"><i class="fas fa-sign-out-alt me-2"></i>Logout</a></li>
                    </ul>
                </div>
            </div>
        </header>
        
        <!-- Page Content -->
        <div class="admin-content-wrapper">
            <?php echo get_flash_message(); ?>
