<?php
// includes/sidebar-ketua.php
// Sidebar untuk area ketua

// Cek apakah user sudah login dan role-nya ketua
if (!isset($_SESSION['user_id']) || !isset($_SESSION['role']) || $_SESSION['role'] != 'ketua') {
    // Redirect ke halaman login jika belum login atau bukan ketua
    setFlash('login_required', 'Anda harus login sebagai ketua yayasan untuk mengakses halaman ini', 'danger');
    header('Location: ' . SITE_URL . '/auth/login.php');
    exit;
}

// Tentukan halaman aktif
$current_page = basename($_SERVER['PHP_SELF']);
$current_folder = basename(dirname($_SERVER['PHP_SELF']));

function isActive($page, $folder = null) {
    global $current_page, $current_folder;
    
    if ($folder !== null) {
        return ($current_folder == $folder) ? 'active' : '';
    } else {
        return ($current_page == $page) ? 'active' : '';
    }
}

// Cek apakah ada kampanye yang menunggu persetujuan
$db = dbConnect();
$db->query("SELECT COUNT(*) as count FROM kampanye WHERE status = 'menunggu_persetujuan'");
$kampanye_menunggu = $db->single();
$kampanye_menunggu_value = $kampanye_menunggu->count ?? 0;

// Cek apakah ada laporan keuangan yang menunggu persetujuan
$db->query("SELECT COUNT(*) as count FROM financial_reports WHERE approved_by IS NULL");
$laporan_menunggu = $db->single();
$laporan_menunggu_value = $laporan_menunggu->count ?? 0;

// Cek notifikasi
$db->query("SELECT COUNT(*) as count FROM notifications WHERE user_id = :user_id AND is_read = 0");
$db->bind(':user_id', $_SESSION['user_id']);
$notifikasi = $db->single();
$notifikasi_value = $notifikasi->count ?? 0;
?>

<!-- Sidebar Ketua -->
<nav id="sidebar" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
    <div class="position-sticky pt-3">
        <ul class="nav flex-column">
            <li class="nav-item">
                <a class="nav-link <?= isActive('dashboard.php') ?>" href="<?= SITE_URL ?>/ketua/dashboard.php">
                    <i class="fas fa-tachometer-alt me-2"></i>
                    Dashboard
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= isActive('index.php', 'kampanye') ?>" href="<?= SITE_URL ?>/ketua/kampanye/index.php">
                    <i class="fas fa-hand-holding-heart me-2"></i>
                    Persetujuan Kampanye
                    <?php if ($kampanye_menunggu_value > 0): ?>
                        <span class="badge rounded-pill bg-danger ms-1"><?= $kampanye_menunggu_value ?></span>
                    <?php endif; ?>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= isActive('index.php', 'laporan') ?>" href="<?= SITE_URL ?>/ketua/laporan/index.php">
                    <i class="fas fa-chart-line me-2"></i>
                    Laporan Keuangan
                    <?php if ($laporan_menunggu_value > 0): ?>
                        <span class="badge rounded-pill bg-danger ms-1"><?= $laporan_menunggu_value ?></span>
                    <?php endif; ?>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= isActive('index.php', 'kegiatan') ?>" href="<?= SITE_URL ?>/ketua/kegiatan/index.php">
                    <i class="fas fa-newspaper me-2"></i>
                    Kegiatan Yayasan
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= isActive('index.php', 'staff') ?>" href="<?= SITE_URL ?>/ketua/staff/index.php">
                    <i class="fas fa-users-cog me-2"></i>
                    Manajemen Staff
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= isActive('index.php', 'profil') ?>" href="<?= SITE_URL ?>/ketua/profil/index.php">
                    <i class="fas fa-user-circle me-2"></i>
                    Profil Saya
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="<?= SITE_URL ?>/ketua/notifikasi.php">
                    <i class="fas fa-bell me-2"></i>
                    Notifikasi
                    <?php if ($notifikasi_value > 0): ?>
                        <span class="badge rounded-pill bg-danger ms-1"><?= $notifikasi_value ?></span>
                    <?php endif; ?>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="<?= SITE_URL ?>/auth/logout.php">
                    <i class="fas fa-sign-out-alt me-2"></i>
                    Logout
                </a>
            </li>
        </ul>
        
        <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
            <span>Pintasan</span>
        </h6>
        <ul class="nav flex-column mb-2">
            <li class="nav-item">
                <a class="nav-link" href="<?= SITE_URL ?>" target="_blank">
                    <i class="fas fa-external-link-alt me-2"></i>
                    Lihat Website
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="<?= SITE_URL ?>/ketua/logs/index.php">
                    <i class="fas fa-history me-2"></i>
                    Log Aktivitas
                </a>
            </li>
        </ul>
        
        <div class="mt-4 px-3">
            <div class="d-flex align-items-center">
                <div class="flex-shrink-0">
                    <div class="avatar-sm bg-primary rounded-circle text-white d-flex align-items-center justify-content-center">
                        <i class="fas fa-user"></i>
                    </div>
                </div>
                <div class="flex-grow-1 ms-3">
                    <div class="fw-bold"><?= $_SESSION['nama_lengkap'] ?></div>
                    <div class="small text-muted"><?= getRoleName($_SESSION['role']) ?></div>
                </div>
            </div>
        </div>
    </div>
</nav>

<style>
.avatar-sm {
    width: 32px;
    height: 32px;
    font-size: 0.875rem;
}
</style>