<?php
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/../../includes/db.php';
$active_page = basename($_SERVER['PHP_SELF']);

// Count unread messages
$unread_count = 0;
try {
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM messages WHERE is_read = 0");
    $stmt->execute();
    $unread_count = $stmt->fetchColumn();
} catch (Exception $e) {
    $unread_count = 0;
}
?>
<div class="sidebar">
    <div class="sidebar-header">
        <h2>NAMORA NEXA</h2>
        <p>Admin Panel</p>
    </div>
    
    <ul class="sidebar-menu">
        <li>
            <a href="index.php" class="sidebar-link <?php echo $active_page == 'index.php' ? 'active' : ''; ?>">
                <i class="fas fa-chart-line"></i> Dashboard
            </a>
        </li>
        <li>
            <a href="products.php" class="sidebar-link <?php echo in_array($active_page, ['products.php', 'products_add.php', 'products_edit.php']) ? 'active' : ''; ?>">
                <i class="fas fa-boxes"></i> Produk Batu Alam
            </a>
        </li>
        <li>
            <a href="categories.php" class="sidebar-link <?php echo $active_page == 'categories.php' ? 'active' : ''; ?>">
                <i class="fas fa-tags"></i> Kategori Produk
            </a>
        </li>
        <li>
            <a href="articles.php" class="sidebar-link <?php echo in_array($active_page, ['articles.php', 'articles_add.php', 'articles_edit.php']) ? 'active' : ''; ?>">
                <i class="fas fa-newspaper"></i> Artikel / Blog
            </a>
        </li>
        <li>
            <a href="gallery.php" class="sidebar-link <?php echo $active_page == 'gallery.php' ? 'active' : ''; ?>">
                <i class="fas fa-images"></i> Galeri Proyek
            </a>
        </li>
        <li>
            <a href="settings.php" class="sidebar-link <?php echo $active_page == 'settings.php' ? 'active' : ''; ?>">
                <i class="fas fa-cogs"></i> Pengaturan Web
            </a>
        </li>
        <li>
            <a href="messages.php" class="sidebar-link <?php echo $active_page == 'messages.php' ? 'active' : ''; ?>">
                <i class="fas fa-envelope"></i> Pesan Masuk
                <?php if ($unread_count > 0): ?>
                    <span class="badge badge-error" style="margin-left: auto; border-radius: 10px; padding: 2px 8px;"><?php echo $unread_count; ?></span>
                <?php endif; ?>
            </a>
        </li>
    </ul>
    
    <div class="sidebar-footer">
        <p>&copy; <?php echo date('Y'); ?> Namora Nexa</p>
        <a href="logout.php" style="color: var(--error); text-decoration: none; font-weight: 700; margin-top: 10px; display: inline-block;">
            <i class="fas fa-sign-out-alt"></i> Keluar
        </a>
    </div>
</div>
