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

/* =========================
   PROTEKSI LOGIN
========================= */
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
    die("Akses ditolak!");
}

if (!isset($_SESSION['role'])) {
    die("Role tidak ditemukan, silakan login ulang.");
}

$role     = $_SESSION['role'];
$username = $_SESSION['username']; // NPSN sekolah

/* =========================
   QUERY SESUAI ROLE
   + NAMA SEKOLAH
========================= */
if ($role === 'admin') {
    $stmt = $conn->prepare("
        SELECT 
            p.*,
            s.nama_sekolah
        FROM penganggaran p
        LEFT JOIN sekolah s ON p.npsn = s.npsn
        ORDER BY p.tahun DESC
    ");
} else {
    $stmt = $conn->prepare("
        SELECT 
            p.*,
            s.nama_sekolah
        FROM penganggaran p
        LEFT JOIN sekolah s ON p.npsn = s.npsn
        WHERE p.npsn = ?
        ORDER BY p.tahun DESC
    ");
    $stmt->bind_param("s", $username);
}

$stmt->execute();
$q = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Data Penganggaran</title>
<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 href="https://cdn.jsdelivr.net/npm/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet">

<style>
body { font-family: "Segoe UI", Arial; }
.table th {
    text-align: center;
    vertical-align: middle;
    white-space: nowrap;
}
.table td {
    vertical-align: middle;
}
.table {
    font-size: 13px;
    white-space: nowrap;
}
.btn-aksi {
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
}
</style>
</head>

<body>

<div class="container-fluid mt-4">

<!-- HEADER -->
<div class="d-flex justify-content-between align-items-center mb-3">
    <div>
        <a href="<?= $role==='admin'?'dashboard_admin.php':'dashboard.php' ?>"
           class="text-decoration-none text-muted">
            ← Kembali ke Dashboard
        </a>
        <h5 class="mt-2 fw-bold">📊 Data Penganggaran BOS</h5>
    </div>

    <a href="penganggaran_form.php" class="btn btn-primary btn-sm">
        ➕ Tambah Penganggaran
    </a>
</div>

<!-- CARD -->
<div class="card">
<div class="card-body p-0">

<div class="table-responsive">
<table class="table table-bordered table-hover align-middle">

<thead class="table-light">
<tr>
    <th>No</th>
    <th>NPSN</th>
    <th>Nama Sekolah</th>
    <th>Tahun</th>
    <th>Barjas</th>
    <th>Habis Pakai</th>
    <th>Modal Mesin</th>
    <th>Modal Lainnya</th>
    <th>Total</th>
    <th>Aksi</th>
</tr>
</thead>

<tbody>
<?php $no=1; while($d = $q->fetch_assoc()): ?>
<tr>
    <td class="text-center"><?= $no++ ?></td>

    <td class="text-center">
        <span class="badge bg-info"><?= htmlspecialchars($d['npsn']) ?></span>
    </td>

    <td><?= htmlspecialchars($d['nama_sekolah'] ?? '-') ?></td>

    <td class="text-center">
        <span class="badge bg-secondary"><?= htmlspecialchars($d['tahun']) ?></span>
    </td>

    <td class="text-end">Rp <?= number_format($d['barjas'],0,',','.') ?></td>
    <td class="text-end">Rp <?= number_format($d['habis_pakai'],0,',','.') ?></td>
    <td class="text-end">Rp <?= number_format($d['modal_mesin'],0,',','.') ?></td>
    <td class="text-end">Rp <?= number_format($d['modal_lainnya'],0,',','.') ?></td>

    <td class="text-end fw-bold">
        Rp <?= number_format($d['total'],0,',','.') ?>
    </td>

    <td class="text-center">
    <a href="penganggaran_form.php?id=<?= $d['id_anggaran'] ?>"
       class="btn btn-warning btn-sm btn-aksi">
       <i class="bi bi-pencil"></i>
    </a>

    <a href="cetak_penganggaran.php?id=<?= $d['id_anggaran'] ?>"
       target="_blank"
       class="btn btn-success btn-sm btn-aksi">
       <i class="bi bi-file-earmark-pdf"></i>
    </a>

    <?php if($role === 'admin'): ?>
    <button class="btn btn-danger btn-sm btn-aksi"
        data-bs-toggle="modal"
        data-bs-target="#modalHapus"
        data-id="<?= $d['id_anggaran'] ?>"
        data-tahun="<?= htmlspecialchars($d['tahun']) ?>">
        <i class="bi bi-trash"></i>
    </button>
    <?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>

</table>
</div>

</div>
</div>

</div>

<!-- MODAL HAPUS -->
<div class="modal fade" id="modalHapus" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header bg-danger text-white">
        <h5 class="modal-title">⚠️ Konfirmasi Hapus</h5>
        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body text-center">
        <p>Yakin ingin menghapus data penganggaran tahun:</p>
        <strong id="tahunAnggaran" class="text-danger fs-5"></strong>
      </div>
      <div class="modal-footer justify-content-center">
        <button class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
        <a href="#" id="btnHapus" class="btn btn-danger">Ya, Hapus</a>
      </div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.getElementById('modalHapus').addEventListener('show.bs.modal', function (e) {
    const btn = e.relatedTarget;
    document.getElementById('tahunAnggaran').textContent = btn.dataset.tahun;
    document.getElementById('btnHapus').href =
        'penganggaran_hapus.php?id=' + btn.dataset.id;
});
</script>

</body>
</html>
