<?php
// Ensure we have database connection available
if (!isset($GLOBALS['pdo']) || $GLOBALS['pdo'] === null) {
    if (!function_exists('getDBConnection')) {
        require_once __DIR__ . '/../config/database.php';
    }
    try {
        $GLOBALS['pdo'] = getDBConnection();
    } catch (Exception $e) {
        $GLOBALS['pdo'] = null;
    }
}

$currentPage = basename($_SERVER['PHP_SELF']);
$menuItems = [
    [
        'icon' => '📊',
        'title' => 'Dashboard',
        'url' => 'dashboard.php',
        'badge' => null
    ],
    [
        'icon' => '⚙️',
        'title' => 'Services',
        'url' => 'services.php',
        'badge' => null
    ],
    [
        'icon' => '📊',
        'title' => 'Statistics',
        'url' => 'statistics.php',
        'badge' => null
    ],
    [
        'icon' => '🔧',
        'title' => 'Technology',
        'url' => 'technology.php',
        'badge' => null
    ],
    [
        'icon' => '🎥',
        'title' => 'Videos',
        'url' => 'videos.php',
        'badge' => null
    ],
    [
        'icon' => '🖼️',
        'title' => 'Gallery',
        'url' => 'gallery.php',
        'badge' => null
    ],
    [
        'icon' => '📁',
        'title' => 'Projects',
        'url' => 'projects.php',
        'badge' => null
    ],
    [
        'icon' => '💬',
        'title' => 'Testimonials',
        'url' => 'testimonials.php',
        'badge' => null
    ],
    [
        'icon' => '📧',
        'title' => 'Contacts',
        'url' => 'contacts.php',
        'badge' => function() {
            try {
                if (!isset($GLOBALS['pdo']) || $GLOBALS['pdo'] === null) {
                    require_once __DIR__ . '/../config/database.php';
                    $GLOBALS['pdo'] = getDBConnection();
                }
                return $GLOBALS['pdo']->query("SELECT COUNT(*) FROM contact_submissions WHERE status = 'new'")->fetchColumn();
            } catch (Exception $e) {
                return 0;
            }
        }
    ],
    [
        'icon' => 'ℹ️',
        'title' => 'About Content',
        'url' => 'about.php',
        'badge' => null
    ],
    [
        'icon' => '❓',
        'title' => 'FAQs',
        'url' => 'faqs.php',
        'badge' => null
    ],
    [
        'icon' => '📚',
        'title' => 'Resources',
        'url' => 'resources.php',
        'badge' => null
    ],
    [
        'icon' => '👥',
        'title' => 'Team',
        'url' => 'team.php',
        'badge' => null
    ],
    [
        'icon' => '📬',
        'title' => 'Newsletter',
        'url' => 'newsletter.php',
        'badge' => null
    ],
    [
        'icon' => '⚙️',
        'title' => 'Settings',
        'url' => 'settings.php',
        'badge' => null
    ]
];
?>
<aside class="admin-sidebar" id="adminSidebar">
    <div class="sidebar-header">
        <div class="sidebar-logo">
            <div class="logo-icon">H</div>
            <div class="logo-text">
                <h3>Horizon Admin</h3>
                <span>Control Panel</span>
            </div>
        </div>
        <button class="sidebar-toggle" id="sidebarToggle" aria-label="Toggle sidebar">
            <svg class="toggle-icon-expand" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M3 12h18M3 6h18M3 18h18"/>
            </svg>
            <svg class="toggle-icon-collapse" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M18 6L6 18M6 6l12 12"/>
            </svg>
        </button>
    </div>
    
    <nav class="sidebar-nav">
        <?php foreach ($menuItems as $item): 
            $isActive = ($currentPage === $item['url']);
            try {
                $badge = is_callable($item['badge']) ? $item['badge']() : $item['badge'];
            } catch (Exception $e) {
                $badge = null;
            }
        ?>
        <a href="<?php echo htmlspecialchars($item['url']); ?>" 
           class="nav-item <?php echo $isActive ? 'active' : ''; ?>"
           data-tooltip="<?php echo htmlspecialchars($item['title']); ?>">
            <span class="nav-icon"><?php echo $item['icon']; ?></span>
            <span class="nav-title"><?php echo htmlspecialchars($item['title']); ?></span>
            <?php if ($badge && $badge > 0): ?>
                <span class="nav-badge"><?php echo $badge; ?></span>
            <?php endif; ?>
        </a>
        <?php endforeach; ?>
    </nav>
    
    <div class="sidebar-footer">
        <div class="user-info">
            <div class="user-avatar">
                <?php echo strtoupper(substr($_SESSION['admin_name'] ?? 'A', 0, 1)); ?>
            </div>
            <div class="user-details">
                <div class="user-name"><?php echo htmlspecialchars($_SESSION['admin_name'] ?? 'Admin'); ?></div>
                <div class="user-role"><?php echo ucfirst($_SESSION['admin_role'] ?? 'admin'); ?></div>
            </div>
        </div>
        <a href="logout.php" class="logout-btn">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4M16 17l5-5-5-5M21 12H9"/>
            </svg>
            Logout
        </a>
    </div>
</aside>

<div class="sidebar-overlay" id="sidebarOverlay"></div>

