<?php
$current = basename($_SERVER['PHP_SELF']);
$unread  = getUnreadCount($conn, $_SESSION['user_id']);
$av      = $conn->query("SELECT avatar FROM users WHERE id={$_SESSION['user_id']}")->fetch_assoc();
?>
<div class="sidebar" id="sidebar">

    <!-- Logo -->
    <div class="sidebar-logo">
        <div class="sidebar-logo-inner">
            <img src="/lostfound_relink/cyn_logo.png" alt="Cauayan City Hall">
            <div>
                <span class="sidebar-brand">🔗 RELINK</span>
                <span class="sidebar-sub">Cauayan City Hall</span>
            </div>
        </div>
    </div>

    <!-- Navigation -->
    <nav class="sidebar-nav">

        <div class="nav-section">Main</div>
        <a href="/lostfound_relink/admin/dashboard.php" class="nav-item <?= $current=='dashboard.php'?'active':'' ?>">
            <span class="nav-icon">📊</span>
            <span class="nav-label">Dashboard</span>
        </a>

        <div class="nav-section">Management</div>
        <a href="/lostfound_relink/admin/users.php" class="nav-item <?= $current=='users.php'?'active':'' ?>">
            <span class="nav-icon">👥</span>
            <span class="nav-label">Users</span>
        </a>
        <a href="/lostfound_relink/admin/lost_items.php" class="nav-item <?= $current=='lost_items.php'?'active':'' ?>">
            <span class="nav-icon">🔍</span>
            <span class="nav-label">Lost Items</span>
        </a>
        <a href="/lostfound_relink/admin/found_items.php" class="nav-item <?= $current=='found_items.php'?'active':'' ?>">
            <span class="nav-icon">📦</span>
            <span class="nav-label">Found Items</span>
        </a>
        <a href="/lostfound_relink/admin/matches.php" class="nav-item <?= $current=='matches.php'?'active':'' ?>">
            <span class="nav-icon">🤝</span>
            <span class="nav-label">Matches</span>
        </a>
        <a href="/lostfound_relink/admin/archive.php" class="nav-item <?= $current=='archive.php'?'active':'' ?>">
            <span class="nav-icon">🗄️</span>
            <span class="nav-label">Archive</span>
            <?php
            $arc_count = 0;
            $arc_tbl = $conn->query("SHOW TABLES LIKE 'archived_items'");
            if ($arc_tbl && $arc_tbl->num_rows > 0) {
                $arc_col = $conn->query("SHOW COLUMNS FROM archived_items LIKE 'is_restored'");
                if ($arc_col && $arc_col->num_rows > 0) {
                    $arc = $conn->query("SELECT COUNT(*) c FROM archived_items WHERE is_restored=FALSE");
                    $arc_count = ($arc && $r = $arc->fetch_assoc()) ? (int)$r['c'] : 0;
                }
            }
            if ($arc_count > 0): ?><span class="nav-badge" style="background:#7c3aed"><?= $arc_count ?></span><?php endif; ?>
        </a>

        <div class="sidebar-divider"></div>
        <div class="nav-section">Reports & Insights</div>
        <a href="/lostfound_relink/public/browse.php" class="nav-item <?= $current=='browse.php'?'active':'' ?>">
            <span class="nav-icon">🌐</span>
            <span class="nav-label">Public Browse</span>
        </a>
        <a href="/lostfound_relink/admin/messages.php" class="nav-item <?= $current=='messages.php'?'active':'' ?>">
            <span class="nav-icon">💬</span>
            <span class="nav-label">Messages</span>
            <?php
            $amsg = $conn->query("SELECT COUNT(*) as c FROM messages WHERE seen_status='unseen' AND sender_id != {$_SESSION['user_id']}");
            $amsg_count = $amsg ? $amsg->fetch_assoc()['c'] : 0;
            if($amsg_count > 0): ?><span class="nav-badge"><?= $amsg_count ?></span><?php endif; ?>
        </a>
        <a href="/lostfound_relink/admin/notifications.php" class="nav-item <?= $current=='notifications.php'?'active':'' ?>">
            <span class="nav-icon">🔔</span>
            <span class="nav-label">Notifications</span>
            <?php if($unread > 0): ?><span class="nav-badge"><?= $unread ?></span><?php endif; ?>
        </a>
        <a href="/lostfound_relink/admin/activity_log.php" class="nav-item <?= $current=='activity_log.php'?'active':'' ?>">
            <span class="nav-icon">📋</span>
            <span class="nav-label">Activity Log</span>
        </a>
        <a href="/lostfound_relink/admin/analytics.php" class="nav-item <?= $current=='analytics.php'?'active':'' ?>">
            <span class="nav-icon">📈</span>
            <span class="nav-label">Analytics</span>
        </a>
        <a href="/lostfound_relink/admin/locations.php" class="nav-item <?= $current=='locations.php'?'active':'' ?>">
            <span class="nav-icon">📍</span>
            <span class="nav-label">Locations</span>
        </a>

        <div class="sidebar-divider"></div>
        <div class="nav-section">System</div>
        <a href="/lostfound_relink/admin/settings.php" class="nav-item <?= $current=='settings.php'?'active':'' ?>">
            <span class="nav-icon">⚙️</span>
            <span class="nav-label">Settings</span>
        </a>
        <a href="/lostfound_relink/admin/backup.php" class="nav-item <?= $current=='backup.php'?'active':'' ?>">
            <span class="nav-icon">💾</span>
            <span class="nav-label">Backup</span>
        </a>

    </nav>

    <!-- Footer / Profile -->
    <div class="sidebar-footer">
        <div class="sidebar-profile">
            <div class="sidebar-avatar">
                <?php if(!empty($av['avatar'])): ?>
                <img src="/lostfound_relink/<?= htmlspecialchars($av['avatar']) ?>" alt="Avatar">
                <?php else: ?>
                <?= strtoupper(substr($_SESSION['name'], 0, 1)) ?>
                <?php endif; ?>
            </div>
            <div class="sidebar-profile-info">
                <div class="sidebar-profile-name"><?= htmlspecialchars($_SESSION['name']) ?></div>
                <div class="sidebar-profile-role">Administrator</div>
            </div>
        </div>
        <a href="/lostfound_relink/auth/logout.php" class="sidebar-logout">
            <svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/>
                <polyline points="16 17 21 12 16 7"/>
                <line x1="21" y1="12" x2="9" y2="12"/>
            </svg>
            Sign Out
        </a>
    </div>

</div>
