<?php 
require_once __DIR__ . '/config/auth.php';
cek_role('gembala'); 

if($_SESSION['role'] == 'bendahara'){
    header('Location: pages/dashboard_bendahara.php');
    exit;
}

$role = $_SESSION['role'];
$role_text = $role == 'admin_dpw' ? 'Admin BPW Keerom' : 'Gembala';
$nama = $_SESSION['nama'];
$foto = $_SESSION['foto'] ?: 'default.png';

// OTOMATIS DETECT DOMAIN - NGGAK PERLU EDIT MANUAL
$BASE_URL = 'http://simjebpw.infinityfreeapp.com';
?>
<!DOCTYPE html>
<html lang="id">
<head> 
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SIMJEBPW - Dashboard</title>
    
    <!-- PAKE URL LENGKAP - TANPA BASE TAG -->
    <link href="<?= $BASE_URL ?>/assets/vendor/fontawesome-free/css/all.min.css" rel="stylesheet">
    <link href="<?= $BASE_URL ?>/assets/css/sb-admin-2.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
    
    <script>const BASE_URL = '<?= $BASE_URL ?>';</script>
</head>
<body id="page-top">
    <div id="wrapper">
        <ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">
            <a class="sidebar-brand d-flex align-items-center justify-content-center" href="#">
                <div class="sidebar-brand-icon"><i class="fas fa-church"></i></div>
                <div class="sidebar-brand-text mx-3">
                    SIMJEBPW
                    <div style="font-size:10px;font-weight:400;margin-top:-3px"><?= $role_text ?></div>
                </div>
            </a>
            <hr class="sidebar-divider my-0">
            <li class="nav-item active">
                <a class="nav-link" href="#" data-page="home"><i class="fas fa-fw fa-tachometer-alt"></i><span>Dashboard</span></a>
            </li>
            <hr class="sidebar-divider">
            
            <?php if($role == 'admin_dpw'): ?>
                <div class="sidebar-heading">MANAJEMEN BPW</div>
                <li class="nav-item"><a class="nav-link" href="#" data-page="gereja"><i class="fas fa-fw fa-church"></i><span>Data Gereja</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="data_gembala"><i class="fas fa-fw fa-user-tie"></i><span>Data Gembala</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="pengurus_dpw"><i class="fas fa-fw fa-sitemap"></i><span>Pengurus BPW</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="struktur_organisasi"><i class="fas fa-fw fa-project-diagram"></i><span>Struktur Organisasi</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="visi_misi"><i class="fas fa-fw fa-bullseye"></i><span>Visi Misi BPW</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="inventaris_dpw"><i class="fas fa-fw fa-box"></i><span>Inventaris BPW</span></a></li>
                <hr class="sidebar-divider">
                <div class="sidebar-heading">LAPORAN BPW</div>
                <li class="nav-item"><a class="nav-link" href="#" data-page="laporan_dpw"><i class="fas fa-fw fa-file-alt"></i><span>Laporan BPW</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="peta"><i class="fas fa-fw fa-map-marked-alt"></i><span>Peta Gereja</span></a></li>
            <?php else: ?>
                <div class="sidebar-heading">MANAJEMEN GEREJA</div>
                <li class="nav-item"><a class="nav-link" href="#" data-page="profil"><i class="fas fa-fw fa-church"></i><span>Profil Gereja</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="jemaat"><i class="fas fa-fw fa-user"></i><span>Data Jemaat</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="pelayan"><i class="fas fa-fw fa-hands-helping"></i><span>Pelayan</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="inventaris"><i class="fas fa-fw fa-box"></i><span>Inventaris Gereja</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="absensi"><i class="fas fa-fw fa-wallet"></i><span>Absensi</span></a></li>
                <hr class="sidebar-divider">
                <li class="nav-item"><a class="nav-link" href="#" data-page="pejabat"><i class="fas fa-fw fa-user-tie"></i><span>Pejabat</span></a></li>
                <li class="nav-item"><a class="nav-link" href="#" data-page="kelola_bendahara"><i class="fas fa-fw fa-users-cog"></i><span>Kelola Bendahara</span></a></li>
                <hr class="sidebar-divider">
                <div class="sidebar-heading">LAPORAN GEREJA</div>
                <li class="nav-item"><a class="nav-link" href="#" data-page="laporan"><i class="fas fa-fw fa-file-pdf"></i><span>Laporan Gereja</span></a></li>
            <?php endif; ?>
            
            <div class="text-center d-none d-md-inline">
                <button class="rounded-circle border-0" id="sidebarToggle"></button>
            </div>
        </ul>

        <div id="content-wrapper" class="d-flex flex-column">
            <div id="content">
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>
                    <ul class="navbar-nav ml-auto">
                        <li class="nav-item dropdown no-arrow">
                            <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-toggle="dropdown">
                                <span class="mr-2 d-none d-lg-inline text-gray-600 small">
                                    <?= htmlspecialchars($nama)?><br>
                                    <small class="text-primary"><?= $role_text ?></small>
                                </span>
                                <img class="img-profile rounded-circle" 
                                     src="<?= $BASE_URL ?>/assets/img/users/<?= $foto?>" 
                                     onerror="this.src='<?= $BASE_URL ?>/assets/img/users/default.png'"
                                     width="32" height="32">
                            </a>
                            <div class="dropdown-menu dropdown-menu-right shadow">
                                <div class="dropdown-item text-center">
                                    <strong><?= htmlspecialchars($nama)?></strong><br>
                                    <small class="text-muted"><?= $role_text ?></small>
                                </div>
                                <div class="dropdown-divider"></div>
                                <a class="dropdown-item" href="logout.php"><i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i> Logout</a>
                            </div>
                        </li>
                    </ul>
                </nav>
                <div class="container-fluid" id="main-content"></div>
            </div>
        </div>
    </div>

<!-- TARUH SEBELUM </body> -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/startbootstrap-sb-admin-2@4.1.4/js/sb-admin-2.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>

<script>
$(document).ready(()=> loadPage('home'));

$(document).on('click', '.nav-link', function(e){
    e.preventDefault();
    let page = $(this).data('page');
    if(!page) return;
    $('.nav-item').removeClass('active');
    $(this).parent().addClass('active');
    loadPage(page);
});

function loadPage(page){
    $('#main-content').html('<div class="text-center py-5"><div class="spinner-border text-primary"></div></div>');
    $('#main-content').load('pages/'+page+'.php', function(res, status){
        if(status == "error"){
            $('#main-content').html('<div class="alert alert-danger">Gagal load halaman: '+page+'</div>');
        }
    });
}

$("#sidebarToggleTop").on('click', function(e) {
    e.preventDefault();
    $("body").toggleClass("sidebar-toggled");
    $(".sidebar").toggleClass("toggled");
});

// FIX DROPDOWN LOGOUT - INI YANG PENTING
$('.dropdown-toggle').dropdown();
</script>

</body>
</html>
</html>