<?php
// includes/sidebar.php
$user = currentUser();
$role = $user['role'] ?? '';
$unread = getUnreadCount($pdo, $user['id']);

$navItems = [];
if ($role === 'admin') {
    $navItems = [
        ['icon' => 'grid', 'label' => 'لوحة التحكم', 'href' => BASE . 'admin/index.php'],
        ['icon' => 'users', 'label' => 'إدارة المستخدمين', 'href' => BASE . 'admin/users.php'],
        ['icon' => 'briefcase', 'label' => 'مراجعة المحامين', 'href' => BASE . 'admin/lawyers.php'],
        ['icon' => 'user-plus', 'label' => 'إنشاء حساب كاتب', 'href' => BASE . 'admin/create_clerk.php'],
        ['icon' => 'bar-chart-2', 'label' => 'التقارير', 'href' => BASE . 'admin/reports.php'],
        ['icon' => 'settings', 'label' => 'الإعدادات', 'href' => BASE . 'admin/settings.php'],
    ];
} elseif ($role === 'clerk') {
    $navItems = [
        ['icon' => 'grid', 'label' => 'لوحة التحكم', 'href' => BASE . 'clerk/index.php'],
        ['icon' => 'folder', 'label' => 'القضايا', 'href' => BASE . 'clerk/cases/index.php'],
        ['icon' => 'calendar', 'label' => 'الجلسات', 'href' => BASE . 'clerk/sessions/index.php'],
        ['icon' => 'file-text', 'label' => 'المحاضر (PV)', 'href' => BASE . 'clerk/minutes.php'],
        ['icon' => 'bar-chart-2', 'label' => 'التقارير', 'href' => BASE . 'clerk/reports.php'],
    ];
} elseif ($role === 'lawyer') {
    $navItems = [
        ['icon' => 'grid', 'label' => 'لوحة التحكم', 'href' => BASE . 'lawyer/index.php'],
        ['icon' => 'folder', 'label' => 'قضاياي', 'href' => BASE . 'lawyer/cases.php'],
        ['icon' => 'upload', 'label' => 'وثائقي', 'href' => BASE . 'lawyer/upload.php'],
        ['icon' => 'user', 'label' => 'ملفي الشخصي', 'href' => BASE . 'lawyer/profile.php'],
    ];
}

$currentPage = basename($_SERVER['PHP_SELF']);
?>
<aside class="sidebar" id="sidebar">
    <div class="sidebar-brand">
        <div class="brand-icon" style="display:flex;align-items:center;justify-content:center;width:44px;height:44px;background:linear-gradient(135deg,#C9A84C,#E8C97A);border-radius:10px;flex-shrink:0;">
          <svg width="24" height="24" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
            <line x1="16" y1="4" x2="16" y2="28" stroke="#0A1628" stroke-width="2.2" stroke-linecap="round"/>
            <line x1="7" y1="7" x2="25" y2="7" stroke="#0A1628" stroke-width="2.2" stroke-linecap="round"/>
            <line x1="7" y1="7" x2="3.5" y2="15" stroke="#0A1628" stroke-width="1.8" stroke-linecap="round"/>
            <line x1="7" y1="7" x2="10.5" y2="15" stroke="#0A1628" stroke-width="1.8" stroke-linecap="round"/>
            <path d="M3 15 Q7 18 11 15" stroke="#0A1628" stroke-width="1.8" fill="none" stroke-linecap="round"/>
            <line x1="25" y1="7" x2="21.5" y2="15" stroke="#0A1628" stroke-width="1.8" stroke-linecap="round"/>
            <line x1="25" y1="7" x2="28.5" y2="15" stroke="#0A1628" stroke-width="1.8" stroke-linecap="round"/>
            <path d="M21 15 Q25 18 29 15" stroke="#0A1628" stroke-width="1.8" fill="none" stroke-linecap="round"/>
            <line x1="11" y1="28" x2="21" y2="28" stroke="#0A1628" stroke-width="2.2" stroke-linecap="round"/>
          </svg>
        </div>
        <div class="brand-text">
            <span class="brand-title">قضيتي</span>
            <span class="brand-sub">نظام إدارة القضايا</span>
        </div>
    </div>

    <div class="sidebar-user">
        <div class="user-avatar">
            <?= mb_substr($user['full_name'], 0, 1) ?>
        </div>
        <div class="user-info">
            <div class="user-name"><?= clean($user['full_name']) ?></div>
            <div class="user-role"><?= roleLabel($role) ?></div>
        </div>
    </div>

    <nav class="sidebar-nav">
        <?php foreach ($navItems as $item): ?>
        <a href="<?= $item['href'] ?>"
           class="nav-item <?= (basename($item['href']) === $currentPage ? 'active' : '') ?>">
            <i data-feather="<?= $item['icon'] ?>"></i>
            <span><?= $item['label'] ?></span>
        </a>
        <?php endforeach; ?>
    </nav>

    <div class="sidebar-footer">
        <a href="#" class="nav-item" id="notifBtn" onclick="toggleNotifications(event)">
            <i data-feather="bell"></i>
            <span>الإشعارات</span>
            <?php if ($unread > 0): ?>
            <span class="notif-badge"><?= $unread ?></span>
            <?php endif; ?>
        </a>
        <a href="<?= BASE ?>logout.php" class="nav-item nav-logout">
            <i data-feather="log-out"></i>
            <span>تسجيل الخروج</span>
        </a>
    </div>
</aside>

<!-- Notifications Panel -->
<div class="notif-panel" id="notifPanel">
    <div class="notif-header">
        <span>الإشعارات</span>
        <button onclick="markAllRead()" class="btn-link">تحديد الكل كمقروء</button>
    </div>
    <div class="notif-list" id="notifList">
        <div class="notif-loading">جارٍ التحميل...</div>
    </div>
</div>
<div class="notif-overlay" id="notifOverlay" onclick="closeNotifications()"></div>