<?php
// header.php
// FILE INI HARUS DISIMPAN SEBAGAI header.php

// === TAMBAHKAN LINE INI UNTUK MENGATASI ERROR "HEADERS ALREADY SENT" ===
ob_start();

// Logika PHP Awal
// GANTI BARIS session_start() LAMA DENGAN PENGECUALIAN INI:
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
// -----------------------------------------------------------------------

include_once 'config/koneksi.php';

// Mendapatkan nama file yang sedang dibuka
$current_page = basename($_SERVER['PHP_SELF']);

// Fungsi bantuan untuk mendapatkan ID Tahun Aktif
if (!function_exists('getTahunAktifId')) {
    function getTahunAktifId($conn) {
        $sql = "SELECT id FROM tahun_aktif WHERE is_aktif = 1 LIMIT 1";
        $result = $conn->query($sql);
        if ($result && $result->num_rows > 0) {
            return $result->fetch_assoc()['id'];
        }
        return null;
    }
}

if (!isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}

// Inisialisasi variabel sesi
$nama_user = $_SESSION['nama'] ?? 'Pengguna';
$is_admin = $_SESSION['is_admin'] ?? 0;


// =======================================================
// LOGIKA QUOTE DINAMIS (GOTU)
// =======================================================
$quotes = [
    "Perencanaan yang baik adalah setengah dari pekerjaan.",
    "Data adalah minyak baru; jangan biarkan bocor.",
    "Hari ini harus lebih baik dari kemarin, terutama dalam akurasi data.",
    "Fokus pada output, bukan hanya pada input.",
    "Setiap angka memiliki cerita. Pastikan cerita Anda benar.",
    "Kualitas data adalah tanggung jawab kita semua.",
    "Semangat, jadikan kegiatan ini bermakna!",
    "Langkah kecil hari ini adalah kemajuan besar esok hari.",
    "Integrasi data dimulai dari koordinasi yang solid."
];

// Pilih quote secara acak
$random_quote_key = array_rand($quotes);
$dynamic_quote = $quotes[$random_quote_key];

// Tentukan judul halaman berdasarkan nama file (untuk header biru)
$page_title = [
    'alokasi_mitra.php' => 'Alokasi Mitra & Honor',
    'dashboard.php' => 'Dashboard Utama',
    'aktivitas_kerja.php' => 'Manajemen Aktivitas Kerja',
    'mitra.php' => 'Manajemen Mitra Statistik',
    'input_master_aktivitas.php' => 'Master Aktivitas',
    'program_list.php' => 'Daftar Program',
    'kegiatan_list.php' => 'Daftar Kegiatan',
    'subkegiatan_list.php' => 'Daftar Subkegiatan',
    'komponen_list.php' => 'Daftar Komponen',
    'subkomponen_list.php' => 'Daftar Subkomponen',
    'detail_kegiatan_list.php' => 'Daftar Detail Kegiatan',
    'import_hierarki.php' => 'Impor Kegiatan (Admin)',
    'cetak_hierarki.php' => 'Cetak Kegiatan',
    'rekap_mitra.php' => 'Rekapitulasi Kegiatan',
    'manajemen_user.php' => 'Manajemen User (Admin)',
    'manajemen_tahun.php' => 'Manajemen Tahun Aktif (Admin)',
];
$title_display = $page_title[$current_page] ?? 'Halaman Tidak Dikenal';

// === HITUNG TINGGI HEADER KONTEN UTAMA (digunakan untuk nilai TOP) ===
$blue_header_height = '60px'; 
$quote_header_top = $blue_header_height; 

// =======================================================
// LOGIKA MENU BARU (Mitra & Aktivitas)
// =======================================================
$mitra_aktivitas_pages = ['aktivitas_kerja.php', 'alokasi_mitra.php', 'mitra.php'];
$is_mitra_aktivitas_active = in_array($current_page, $mitra_aktivitas_pages);
$mitra_aktivitas_collapse = $is_mitra_aktivitas_active ? 'show' : '';
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manajemen Kegiatan MAMI KOS</title>
    
    <link rel="icon" type="image/png" href="assets/img/favicon.png">
    <link rel="shortcut icon" type="image/x-icon" href="assets/img/favicon.ico">
    
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    
    <link rel="stylesheet" href="assets/css/print.css" media="print">
    
    <style>
        /* Perbaikan Layout Utama */
        .content {
            padding: 0;
            min-height: 100vh;
            margin-left: 16.6667%;
            width: 83.3333%;
        }
        
        /* Gaya Sidebar */
        .sidebar { 
            min-height: 100vh; 
            background-color: #3f51b5; 
            padding-top: 20px; 
            color: white; 
            position: fixed;
            top: 0;
            left: 0;
            z-index: 1030;
            width: 16.6667%;
        }
        
        .sidebar-scrollable-area {
            height: calc(100vh - 120px); 
            overflow-y: auto;
            padding-bottom: 20px;
        }
        
        .sidebar a { color: white; text-decoration: none; padding: 10px 15px; display: block; }
        .sidebar a:hover { background-color: #303f9f; }
        .sidebar .nav-link { color: white; }
        .sidebar .nav-link:hover { background-color: #303f9f; }
        .card-info { border-left: 4px solid #3f51b5; }
        
        .sidebar-header {
            padding: 0 15px 15px 15px;
            text-align: center;
            position: sticky;
            top: 0;
            background-color: #3f51b5;
            z-index: 10;
        }
        .sidebar-header img {
            max-width: 60px;
            height: auto;
            margin-bottom: 10px;
        }
        .sidebar-header h4 {
            margin-bottom: 5px;
        }
        .sidebar-header small {
            font-size: 0.75rem;
            opacity: 0.8;
            display: block;
        }
        
        .sidebar a.active-menu,
        .sidebar .nav-link.active-menu {
            background-color: #303f9f;
            border-left: 3px solid #ffeb3b;
            padding-left: 12px;
        }
        
        .content-page-header {
            background-color: #3f51b5;
            color: white;
            padding: 15px 20px;
            margin: 0; 
            border-bottom: 5px solid #ffeb3b;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            z-index: 1000;
            position: sticky;
            top: 0;
        }
        .content-page-header h1 {
            margin: 0;
            font-size: 1.5rem;
            font-weight: 600;
        }
        
        .top-header {
            background-color: #ffffff;
            border-bottom: 1px solid #e0e0e0;
            padding: 10px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            z-index: 999; 
            position: sticky;
            top: <?php echo $quote_header_top; ?>; 
            margin: 0; 
        }
        .quote-text {
            font-style: italic;
            color: #555;
            font-size: 0.9rem;
        }
        
        .page-content-wrapper {
            padding: 20px; 
        }

    </style>
</head>
<body>
<div> 
    <div class="sidebar"> 
        
        <div class="sidebar-header">
            <img src="assets/img/bps.png" alt="Logo BPS" class="img-fluid">
            <h4 class="mb-1">MAMIKOS</h4>
            <small>Manajemen Alokasi Mitra, Kinerja Organisasi Satker</small>
        </div>
        <hr class="mx-3 my-2" style="border-top: 1px solid rgba(255,255,255,0.2);">

        <div class="sidebar-scrollable-area">
        
            <nav class="nav flex-column">
                <a href="dashboard.php" class="nav-link py-1 <?php echo ($current_page == 'dashboard.php') ? 'active-menu' : ''; ?>">
                    <i class="bi bi-house-door"></i> Dashboard
                </a>
                
                <a href="#mitraAktivitasMenu" data-bs-toggle="collapse" class="nav-link py-1 d-flex justify-content-between align-items-center <?php echo $is_mitra_aktivitas_active ? 'active-menu' : ''; ?>" role="button">
                    <span class="d-flex align-items-center">
                        <i class="bi bi-person-workspace me-2"></i> Mitra & Aktivitas
                    </span>
                    <i class="bi bi-chevron-down small"></i>
                </a>
                
                <div class="collapse <?php echo $mitra_aktivitas_collapse; ?>" id="mitraAktivitasMenu">
                    <ul class="nav flex-column ps-3">
                        <li class="nav-item">
                            <a href="aktivitas_kerja.php" class="nav-link py-1 <?php echo ($current_page == 'aktivitas_kerja.php') ? 'active-menu' : ''; ?>">
                                <i class="bi bi-dot"></i> Manajemen Aktivitas
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="alokasi_mitra.php" class="nav-link py-1 <?php echo ($current_page == 'alokasi_mitra.php') ? 'active-menu' : ''; ?>">
                                <i class="bi bi-dot"></i> Alokasi Mitra
                            </a>
                        </li>
                        <li class="nav-item">
                            <a href="mitra.php" class="nav-link py-1 <?php echo ($current_page == 'mitra.php') ? 'active-menu' : ''; ?>">
                                <i class="bi bi-dot"></i> Manajemen Mitra
                            </a>
                        </li>
                    </ul>
                </div>
                
                <a href="input_master_aktivitas.php" class="nav-link py-1 <?php echo ($current_page == 'input_master_aktivitas.php') ? 'active-menu' : ''; ?>">
                    <i class="bi bi-puzzle"></i> Master Aktivitas
                </a>
                
                <?php
                    $kegiatan_pages = [
                        'program_list.php', 'kegiatan_list.php', 'subkegiatan_list.php',
                        'komponen_list.php', 'subkomponen_list.php', 'detail_kegiatan_list.php',
                        'cetak_hierarki.php', 'import_hierarki.php'
                    ];
                    $is_kegiatan_active = in_array($current_page, $kegiatan_pages);
                    $kegiatan_collapse = $is_kegiatan_active ? 'show' : '';
                ?>
                <a href="#kegiatanMenu" data-bs-toggle="collapse" class="nav-link py-1 d-flex justify-content-between align-items-center <?php echo $is_kegiatan_active ? 'active-menu' : ''; ?>" role="button">
                    <span class="d-flex align-items-center">
                        <i class="bi bi-calendar-event me-2"></i> Kegiatan BPS
                    </span>
                    <i class="bi bi-chevron-down small"></i>
                </a>
                
                <div class="collapse <?php echo $kegiatan_collapse; ?>" id="kegiatanMenu">
                    <ul class="nav flex-column ps-3">
                        <li class="nav-item"><a class="nav-link py-1 <?php echo ($current_page == 'program_list.php') ? 'active-menu' : ''; ?>" href="program_list.php"><i class="bi bi-dot"></i> Program</a></li>
                        
                        <?php if ($is_admin): ?>
                        <li class="nav-item"><a class="nav-link py-1 <?php echo ($current_page == 'import_hierarki.php') ? 'active-menu' : ''; ?>" href="import_hierarki.php"><i class="bi bi-cloud-arrow-up"></i> <strong>Impor Kegiatan</strong></a></li>
                        <?php endif; ?>
                        
                        <li class="nav-item"><a class="nav-link py-1 text-warning <?php echo ($current_page == 'cetak_hierarki.php') ? 'active-menu' : ''; ?>" href="cetak_hierarki.php" target="_blank"><i class="bi bi-printer"></i> Cetak Kegiatan Lengkap</a></li>
                    </ul>
                </div>
                
                <a href="rekap_mitra.php" class="nav-link py-1 <?php echo ($current_page == 'rekap_mitra.php') ? 'active-menu' : ''; ?>">
                    <i class="bi bi-bar-chart-line"></i> Rekap Kegiatan
                </a>

                <!-- MENU GENERATE SPK SAMAR -->
                <a href="#" class="nav-link py-1 text-white-50" style="pointer-events: none; opacity:0.4;">
                    <i class="bi bi-file-earmark-text"></i> Generate SPK <span class="small">(coming soon)</span>
                </a>

                <?php
                    $manajemen_pages = ['manajemen_user.php', 'manajemen_tahun.php'];
                    $is_manajemen_active = in_array($current_page, $manajemen_pages);
                    $manajemen_collapse = $is_manajemen_active ? 'show' : '';
                ?>
                <?php if ($is_admin): ?>
                <a href="#manajemenSystemMenu" data-bs-toggle="collapse" class="nav-link py-1 d-flex justify-content-between align-items-center <?php echo $is_manajemen_active ? 'active-menu' : ''; ?>" role="button">
                    <span class="d-flex align-items-center">
                        <i class="bi bi-gear me-2"></i> Manajemen Sistem
                    </span>
                    <i class="bi bi-chevron-down small"></i>
                </a>
                    
                    <div class="collapse <?php echo $manajemen_collapse; ?>" id="manajemenSystemMenu">
                        <ul class="nav flex-column ps-3">
                            <li class="nav-item"><a class="nav-link py-1 <?php echo ($current_page == 'manajemen_user.php') ? 'active-menu' : ''; ?>" href="manajemen_user.php"><i class="bi bi-person-gear"></i> Manajemen User</a></li>
                            <li class="nav-item"><a class="nav-link py-1 <?php echo ($current_page == 'manajemen_tahun.php') ? 'active-menu' : ''; ?>" href="manajemen_tahun.php"><i class="bi bi-calendar-check"></i> Manajemen Tahun</a></li>
                        </ul>
                    </div>
                <?php endif; ?>
                
                <a href="logout.php" class="nav-link py-1 mt-5"><i class="bi bi-box-arrow-right"></i> Logout</a>
            </nav>
        
        </div> </div> <div class="content"> 
        
        <div class="content-page-header">
            <h1>
                <span class="me-2">BPS Malra</span> - <?php echo htmlspecialchars($title_display); ?>
            </h1>
        </div>

        <div class="top-header">
            <div class="quote-text">
                <i class="bi bi-chat-quote-fill me-2 text-primary"></i>
                <?php echo htmlspecialchars($dynamic_quote); ?>
            </div>
            <div class="user-info">
                <i class="bi bi-person-circle me-1"></i> <?php echo htmlspecialchars($nama_user); ?>
                <span class="badge bg-secondary"><?php echo $is_admin ? 'Admin' : 'User'; ?></span>
            </div>
        </div>
        
        <div class="page-content-wrapper">
    
</body>
</html>
