<?php
// admin/header.php
// Sertakan file "otak" kita. Ini sudah termasuk session, DB, dan $id_tahun_aktif
include_once 'init.php';

// 3. Cek Autentikasi
// Perbaikan: Mengizinkan role 'admin' DAN 'operator' agar tidak tertendang ke login.php
if (!isset($_SESSION['user_id']) || !in_array($_SESSION['role'], ['admin', 'operator'])) {
    header('Location: ../login.php');
    exit();
}

// 4. Siapkan judul halaman
$page_title = $page_title ?? 'Dashboard';
$page_icon = $page_icon ?? 'bi-speedometer2';

?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($page_title); ?> - Admin PILKASIM</title>

    <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/admin-layout.css">
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2-bootstrap-5-theme@1.3.0/dist/select2-bootstrap-5-theme.min.css" />
</head>
<body>
<div class="d-flex" id="wrapper">

    <?php include 'sidebar.php'; ?>

    <div id="page-content-wrapper">
        <nav class="topbar">
            <div class="topbar-left d-flex align-items-center">
                <button class="btn btn-link fs-4 me-2" id="sidebarToggle">
                    <i class="bi bi-list"></i>
                </button>
                <h4 class="mb-0">
                    <i class="bi <?= htmlspecialchars($page_icon); ?> me-2"></i>
                    <?= htmlspecialchars($page_title) ?>
                </h4>
            </div>

            <div class="dropdown">
                <a href="#" class="d-flex align-items-center text-decoration-none" data-bs-toggle="dropdown">
                    <div class="text-end me-3 d-none d-sm-block">
                        <div class="fw-bold"><?= htmlspecialchars($_SESSION['nama_lengkap']); ?></div>
                        <small class="text-muted"><?= ucfirst(htmlspecialchars($_SESSION['role'])); ?></small>
                    </div>
                    <div class="user-avatar">
                        <?= strtoupper(substr($_SESSION['nama_lengkap'], 0, 1)); ?>
                    </div>
                </a>
                <ul class="dropdown-menu dropdown-menu-end mt-2">
                    <li>
                        <a class="dropdown-item text-danger" href="../logout.php">
                            <i class="bi bi-box-arrow-left me-2"></i>Logout
                        </a>
                    </li>
                </ul>
            </div>
        </nav>

        <main class="container-fluid p-md-4 p-3">