<?php
session_start();
require_once __DIR__ . '/config/config.php';

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

$nama_lengkap = $_SESSION['nama_lengkap'] ?? 'Admin Dinas';
$role = $_SESSION['role'] ?? '-';

/* =====================
   TOTAL ANGGARAN
===================== */
$qAnggaran = mysqli_query($conn,"SELECT SUM(total) AS total_anggaran FROM penganggaran");
$total_anggaran = mysqli_fetch_assoc($qAnggaran)['total_anggaran'] ?? 0;

/* =====================
   TOTAL PENGELUARAN
===================== */
$qPengeluaran = mysqli_query($conn,"SELECT SUM(pengeluaran) AS total_pengeluaran FROM kwitansi");
$total_pengeluaran = mysqli_fetch_assoc($qPengeluaran)['total_pengeluaran'] ?? 0;

/* =====================
   SALDO BANK
===================== */
$qBank = mysqli_query($conn,"SELECT SUM(saldo_akhir) AS saldo_bank FROM rekening_koran");
$saldo_bank = mysqli_fetch_assoc($qBank)['saldo_bank'] ?? 0;

/* =====================
   SALDO TUNAI
===================== */
$saldo_tunai = $total_anggaran - $total_pengeluaran - $saldo_bank;

/* =====================
   NOTIFIKASI TERPISAH (AMAN)
===================== */
$qNotif = mysqli_query($conn,"
    SELECT
        SUM(CASE 
            WHEN status_verifikasi = 'menunggu' THEN 1 
            ELSE 0 
        END) AS menunggu,

        SUM(CASE 
            WHEN status_verifikasi = 'diajukan_ulang' THEN 1 
            ELSE 0 
        END) AS diajukan_ulang
    FROM upload_laporan
");

$notif = mysqli_fetch_assoc($qNotif);

$notif_menunggu      = $notif['menunggu'] ?? 0;
$notif_ajukan_ulang  = $notif['diajukan_ulang'] ?? 0;
?>


?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Dashboard e-BOS</title>
    <!-- Favicon (logo di tab browser) -->
    <link rel="icon" href="assets/images/logomu.ico">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link rel="icon" href="assets/images/logomu.ico">
    
     <style>
        body {
            margin: 0;
            overflow-x: hidden;
            background-color: #f4f6f9;
        }

        /* NAVBAR */
        .navbar {
            height: 56px;
        }

        /* SIDEBAR */
        .sidebar {
            position: fixed;
            top: 56px;
            left: 0;
            width: 240px;
            height: calc(100vh - 56px);
            background: #0d6efd; /* biru seperti app guru */
            color: #fff;
            overflow-y: auto;
        }

        .sidebar .title {
            font-weight: bold;
            padding: 15px;
            font-size: 15px;
            background: #0b5ed7;
            text-align: center;
        }

        .sidebar ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .sidebar ul li a {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 12px 18px;
            color: #eaeaea;
            text-decoration: none;
            font-size: 14px;
            transition: 0.2s;
        }

        .sidebar ul li a:hover {
            background: rgba(255,255,255,0.15);
            color: #fff;
        }

		.sidebar ul li a.active {
			background: transparent;
			color: #ffffff;
			font-weight: 700;
		}

       /* CONTENT */
	
.content {
    margin-left: 240px;
    margin-top: 66px;        /* navbar + jarak aman */
    padding: 20px 25px;
}

.dashboard-divider {
    border-bottom: 1px solid #dee2e6; /* garis tipis abu bootstrap */
    margin-top: 10px;
    margin-bottom: 10px;
}
/* HEADER DASHBOARD – RAPAT */
.dashboard-header h4 {
    margin-bottom: 2px;   /* rapatkan judul ke subjudul */
    line-height: 1.2;
}

.dashboard-header p {
    margin-bottom: 0;    /* hilangkan jarak bawah */
    line-height: 1.3;
}
.sidebar ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.sidebar ul li {
    margin: 2px 0; /* 🔽 jarak antar menu dirapatkan */
}

.sidebar ul li a {
    display: block;
    padding: 8px 12px; /* 🔽 sebelumnya biasanya 12–15px */
    font-size: 14px;
    line-height: 1.3;
    color: #fff;
    text-decoration: none;
    border-radius: 6px;
}

.sidebar ul li a:hover {
    background: rgba(255,255,255,0.15);
}

.sidebar ul li a.active {
    background: #0d6efd;
}
/* =========================
   DASHBOARD CARD RAPAT & LURUS
========================= */
.dashboard-cards {
    margin-left: -6px;   /* 🔽 tarik sedikit ke kiri */
    margin-right: -6px;  /* 🔽 samakan kanan */
}

.dashboard-cards .card {
    height: 100%;
    border-radius: 10px;
}

.dashboard-cards .card-body {
    min-height: 95px; /* 🔒 samakan tinggi semua card */
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 14px;
}

.dashboard-cards h6 {
    font-size: 14px;
    margin-bottom: 6px;
    white-space: nowrap;
}

.dashboard-cards h4 {
    font-weight: 700;
    margin-bottom: 0;
}
/* =========================
   HOVER EFFECT DASHBOARD CARD
========================= */
.dashboard-cards .card {
    transition: 
        transform 0.25s ease,
        box-shadow 0.25s ease,
        border-color 0.25s ease;
    border: 1px solid #e5e7eb;
    cursor: pointer;
}

.dashboard-cards .card::before {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: 10px;
    border-top: 4px solid transparent;
    transition: border-color 0.25s ease;
}

.dashboard-cards .card:hover {
    transform: translateY(-4px);
    box-shadow: 0 12px 25px rgba(0,0,0,0.12);
}

/* WARNA AKSEN SAAT HOVER */
.dashboard-cards .card:hover::before {
    border-top-color: #0d6efd; /* biru bootstrap */
}

/* WARNA TEKS SEDIKIT HIDUP */
.dashboard-cards .card:hover h4 {
    color: #0d6efd !important;
}
.dashboard-cards .card {
    position: relative;
}
/* RUNNING TEXT */
.running-text{
    position:fixed;
    bottom:0;
    left:240px; /* menyesuaikan sidebar */
    right:0;
    background:#ffffff;        /* putih mengikuti background */
    color:#000000;             /* teks hitam */
    font-size:13px;
    padding:8px 0;
    overflow:hidden;
    border-top:1px solid #ddd; /* garis tipis biar rapi */
    z-index:999;
}

.running-text span{
    display:inline-block;
    padding-left:100%;
    white-space:nowrap;
    animation: scrollText 36s linear infinite;
}

@keyframes scrollText{
    0%   { transform: translateX(0); }
    100% { transform: translateX(-100%); }
}
.menu-upload {
    font-weight: 700;
}
.menu-upload {
    font-weight: 700;
    color: #ffffff;
}
.sidebar li a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 15px;
    color: #333;
    text-decoration: none;
    border-radius: 8px;
    transition: 0.2s;
}

.sidebar li a:hover {
    background: #f0f4ff;
    color: #0d6efd;
}

.sidebar li a i {
    width: 18px;
    text-align: center;
}
.navbar-brand img {
    object-fit: cover;
}

.sidebar .title {
    font-weight: 600;
    padding: 15px;
    font-size: 14px;
}
.menu-logout {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    background: #0d6efd;
    color: #fff !important;
    padding: 10px 14px;
    border-radius: 8px;
    text-decoration: none;
    transition: 0.2s;
}

    </style>
</head>
<body>

<!-- NAVBAR -->
<nav class="navbar navbar-dark bg-primary fixed-top">
    <div class="container-fluid">

        <span class="navbar-brand d-flex align-items-center gap-2">
            <img src="assets/images/logomu.ico" alt="Logomu" width="30" height="30" style="border-radius:6px;">
            Aplikasi e-BOS
        </span>

        <div class="text-white d-flex align-items-center gap-2">
            <i class="fa fa-user-circle"></i>
            <?= htmlspecialchars($nama_lengkap) ?>
        </div>

    </div>
</nav>

<!-- SIDEBAR -->
<div class="sidebar">
    <div class="title d-flex align-items-center gap-2">
        <img src="assets/images/logomu.ico" width="28" style="border-radius:6px;">
        APP-e-BOS 2026 V.1
    </div>

    <ul>

        <!-- MENU UMUM (ADMIN & USER) -->
        <li>
            <a href="dashboard_admin.php" class="<?= basename($_SERVER['PHP_SELF'])=='dashboard_admin.php'?'active':'' ?>">
                <i class="fas fa-house"></i> Dashboard
            </a>
        </li>

            <?php if ($role === 'admin') { ?>
                <!-- MENU KHUSUS ADMIN -->
                <li><a href="users.php"><i class="fa fa-user"></i> Users</a></li>
                <li><a href="list_rekening_map.php"><i class="fa fa-file-invoice"></i> Input Rekening Belanja</a></li>
                <li><a href="list_sekolah.php"><i class="fa fa-school"></i> Profil Sekolah</a></li>
                <li><a href="list_penganggaran.php"><i class="fa fa-chart-bar"></i> Penganggaran</a></li>
                <li><a href="list_kwitansi.php"><i class="fa fa-print"></i> Cetak Kwitansi</a></li>
                <li><a href="rekap_bku_proses.php"><i class="fa fa-book"></i> Rekap BKU</a></li>
                <li><a href="list_rekap_kib.php"><i class="fa fa-tags"></i> Rekap KIB</a></li>
                <li><a href="list_rekap_bhp.php"><i class="fa fa-boxes"></i> Rekap BHP</a></li>
                <li><a href="list_rekening_koran.php"><i class="fa fa-university"></i> Upload Rekening Koran</a></li>
                <li><a href="list_stokopname.php"><i class="fa fa-clipboard-check"></i> Stok Opname</a></li>
                <li><a href="list_laporan.php" class="menu-upload"><i class="fa fa-upload"></i> Upload Laporan</a></li>
            <?php } else { ?>
                <!-- MENU KHUSUS USER -->
                <li><a href="list_sekolah.php"><i class="fa fa-school"></i> Profil Sekolah</a></li>
                <li><a href="list_penganggaran.php"><i class="fa fa-chart-bar"></i> Penganggaran</a></li>
                <li><a href="list_kwitansi.php"><i class="fa fa-print"></i> Cetak Kwitansi</a></li>
                <li><a href="rekap_bku_proses.php"><i class="fa fa-book"></i> Rekap BKU</a></li>
                <li><a href="list_rekap_kib.php"><i class="fa fa-tags"></i> Rekap KIB</a></li>
                <li><a href="list_rekap_bhp.php"><i class="fa fa-boxes"></i> Rekap BHP</a></li>
                <li><a href="list_rekening_koran.php"><i class="fa fa-university"></i> Upload Rekening Koran</a></li>
                <li><a href="list_stokopname.php"><i class="fa fa-clipboard-check"></i> Stok Opname</a></li>
                <li><a href="list_laporan.php" class="menu-upload"><i class="fa fa-upload"></i> Upload Laporan</a></li>
            <?php } ?>

         <!-- LOGOUT -->
        <li class="mt-3">
            <a href="logout.php" class="menu-logout">
                <i class="fa fa-sign-out-alt"></i>
                Logout
            </a>
        </li>

    </ul>
</div>

<!-- CONTENT -->
<div class="content">
    <div class="dashboard-header">
    <h4>Selamat Datang di e-BOS Dinas Pendidikan Kab. Situbondo</h4>
    <p class="text-muted mb-1">Sistem Pengelolaan Dana BOS</p>
    <div class="dashboard-divider"></div>
</div>

<?php if($notif_ajukan_ulang > 0): ?>
<div class="alert alert-warning d-flex justify-content-between align-items-center mt-3">
    <div>
        🔁 <b><?= $notif_ajukan_ulang ?></b> sekolah <b>mengajukan ulang laporan</b>.
    </div>
    <a href="list_laporan.php?status=diajukan_ulang"
       class="btn btn-sm btn-warning fw-bold">
        Verifikasi Ulang
    </a>
</div>
<?php endif; ?>

<?php if($notif_menunggu > 0): ?>
<div class="alert alert-info d-flex justify-content-between align-items-center mt-2">
    <div>
        📤 <b><?= $notif_menunggu ?></b> laporan <b>baru menunggu verifikasi</b>.
    </div>
    <a href="list_laporan.php?status=menunggu"
       class="btn btn-sm btn-info fw-bold text-white">
        Verifikasi
    </a>
</div>
<?php endif; ?>

	<div class="row g-3 mt-3 dashboard-cards">
    <div class="col-md-3">
        <div class="card shadow-sm text-center">
            <div class="card-body">
                <h6>Total Anggaran</h6>
                <h4 class="text-success">
                    Rp <?= number_format($total_anggaran,0,',','.') ?>
                </h4>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card shadow-sm text-center">
            <div class="card-body">
                <h6>Total Pengeluaran</h6>
                <h4 class="text-danger">
                    Rp <?= number_format($total_pengeluaran,0,',','.') ?>
                </h4>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card shadow-sm text-center">
            <div class="card-body">
                <h6>Saldo Bank</h6>
                <h4 class="text-primary">
                    Rp <?= number_format($saldo_bank,0,',','.') ?>
                </h4>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card shadow-sm text-center">
            <div class="card-body">
                <h6>Saldo Tunai</h6>
                <h4 class="<?= $saldo_tunai < 0 ? 'text-danger' : 'text-dark' ?>">
                    Rp <?= number_format($saldo_tunai,0,',','.') ?>
                </h4>
            </div>
        </div>
    </div>

</div>
	
	<?php
		require_once __DIR__ . '/config/config.php';

		// ambil total dari tabel penganggaran
		$qTotal = mysqli_query($conn, "
			SELECT
				SUM(honor) AS total_honor,
				SUM(habis_pakai) AS total_habis_pakai,
				SUM(barjas) AS total_barjas,
				SUM(modal_mesin + modal_lainnya) AS total_modal
			FROM penganggaran
		");

		$total = mysqli_fetch_assoc($qTotal);

		$total_honor       = $total['total_honor'] ?? 0;
		$total_habis_pakai = $total['total_habis_pakai'] ?? 0;
		$total_barjas      = $total['total_barjas'] ?? 0;
		$total_modal       = $total['total_modal'] ?? 0;
		?>
    
	<div class="row g-3 mt-3 dashboard-cards">
    <div class="col-md-3">
        <div class="card shadow-sm text-center">
            <div class="card-body">
                <h6>Total Honor</h6>
                <h4 class="text-success">
                    Rp <?= number_format($total_honor,0,',','.') ?>
                </h4>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card shadow-sm text-center">
            <div class="card-body">
                <h6>Total Habis Pakai</h6>
                <h4 class="text-danger">
                    Rp <?= number_format($total_habis_pakai,0,',','.') ?>
                </h4>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card shadow-sm text-center">
            <div class="card-body">
                <h6>Total Barjas</h6>
                <h4 class="text-primary">
                    Rp <?= number_format($total_barjas,0,',','.') ?>
                </h4>
            </div>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card shadow-sm text-center">
            <div class="card-body">
                <h6>Total Modal</h6>
                <h4 class="text-dark">
                    Rp <?= number_format($total_modal,0,',','.') ?>
                </h4>
            </div>
        </div>
    </div>

</div>

    <div class="alert alert-success mt-4">
        ✅ Terima Kasih telah menggunakan Aplikasi ini, dan ini hanya alat untuk membantu agar dapat mempermudah Rekap Laporan BOS Sekolah.
    </div>

</div>

<!-- RUNNING TEXT -->
<div class="running-text">
    <span>
		<?php if($role == 'admin'){ ?>
			📢 Terima Kasih Semoga Sehat Selalu, Salam satu data untuk semua ARKAS untuk Sekolah Indonesia.
		<?php }else{ ?>
			📢 Informasi dari Admin: Silakan lengkapi Laporan BOS Sekolah anda sesuai dengan aplikasi yang ada di ARKAS.
		<?php } ?>
</span>
</div>
</body>
</html>
