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

$role = $_SESSION['role'] ?? 'user';
$npsn = $_SESSION['npsn'] ?? '';

$status = $_GET['status'] ?? '';

$where = [];

// BATASI SEKOLAH
if ($role !== 'admin') {
    $where[] = "npsn = '" . mysqli_real_escape_string($conn, $npsn) . "'";
}

// FILTER STATUS
if (in_array($status, ['menunggu','diajukan_ulang','disetujui','ditolak'])) {
    $where[] = "status_verifikasi = '" . mysqli_real_escape_string($conn, $status) . "'";
}

$whereSql = !empty($where) ? "WHERE " . implode(" AND ", $where) : "";

$data = mysqli_query($conn,"
    SELECT *
    FROM upload_laporan
    $whereSql
    ORDER BY tgl_lapor DESC
");
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Daftar Laporan</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">
<style>
body{background:#f8f9fa;}
.card{border-radius:12px;}
.table th{font-size:.8rem;text-transform:uppercase;}
</style>
</head>

<body>

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

<a href="<?= $role=='admin'?'dashboard_admin.php':'dashboard.php' ?>"
   class="btn btn-outline-secondary btn-sm mb-2">← Dashboard</a>

<div class="card shadow-sm">
<div class="card-header bg-primary text-white d-flex justify-content-between">
    <h5 class="mb-0">📄 Daftar Laporan BOS</h5>
    <?php if($role!='admin'){ ?>
        <a href="tambah_laporan.php" class="btn btn-light btn-sm">➕ Tambah</a>
    <?php } ?>
</div>

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

<thead class="table-light text-center">
<tr>
    <th>No</th>
    <th>NPSN</th>
    <th>Nama Sekolah</th>
    <th>Tanggal</th>
    <th>Bulan</th>
    <th>Tahun</th>
    <th>Rekening Koran</th>
    <th>File Laporan</th>
    <th>Status & Catatan</th>
    <th width="240">Aksi</th>
</tr>
</thead>

<tbody>
<?php
$no = 1;
while ($r = mysqli_fetch_assoc($data)) {
?>
<tr>
<td class="text-center"><?= $no++ ?></td>
<td><?= htmlspecialchars($r['npsn']) ?></td>
<td><?= htmlspecialchars($r['nama_sekolah']) ?></td>
<td class="text-center"><?= date('d-m-Y', strtotime($r['tgl_lapor'])) ?></td>
<td class="text-center"><?= htmlspecialchars($r['bulan']) ?></td>
<td class="text-center"><?= htmlspecialchars($r['tahun']) ?></td>

<!-- REKENING KORAN -->
<td class="text-center">
<?php if($role=='admin' || $r['status_verifikasi']=='disetujui'){ ?>
    <?php if(!empty($r['upload_rekening_koran'])){ ?>
        <a href="upload/<?= htmlspecialchars($r['upload_rekening_koran']) ?>"
           target="_blank"
           class="btn btn-info btn-sm">📑 Lihat</a>
    <?php } else { ?>
        <span class="text-muted">-</span>
    <?php } ?>
<?php } else { ?>
    <span class="badge bg-secondary">⏳ Diproses</span>
<?php } ?>
</td>

<!-- FILE LAPORAN -->
<td class="text-center">
<?php if($role=='admin' || $r['status_verifikasi']=='disetujui'){ ?>
    <a href="upload/<?= htmlspecialchars($r['upload_laporan']) ?>"
       target="_blank"
       class="btn btn-success btn-sm">📂 Lihat</a>
<?php } else { ?>
    <span class="badge bg-secondary">⏳ Diproses</span>
<?php } ?>
</td>

<!-- STATUS -->
<td>
<?php
$badge = [
    'menunggu'         => 'warning',
    'diajukan_ulang'   => 'info',
    'disetujui'        => 'success',
    'ditolak'          => 'danger',
    'minta_buka_kunci' => 'warning'
];

$status = $r['status_verifikasi'];

$warna = $badge[$status] ?? 'secondary';

echo '<span class="badge bg-'.$warna.'">'
     . ucfirst(str_replace('_',' ',$status))
     . '</span>';
?>
</td>

<!-- AKSI -->
<td class="text-center">
<div class="d-flex justify-content-center gap-1 flex-wrap">

<?php
$status = $r['status_verifikasi'];
$id     = $r['id_laporan'];
$isAdmin = ($role === 'admin');
$isOwner = ($r['npsn'] === $npsn);
?>

<!-- ================= ADMIN ================= -->

<?php if($isAdmin): ?>

    <?php if(in_array($status, ['menunggu','diajukan_ulang'])): ?>
        <a href="verifikasi_laporan.php?id=<?= $id ?>"
           class="btn btn-success btn-sm">🔍 Verifikasi</a>

    <?php elseif($status === 'minta_buka_kunci'): ?>
        <a href="buka_kunci_laporan.php?id=<?= $id ?>"
           class="btn btn-warning btn-sm">🔓 Buka Kunci</a>

    <?php elseif($status === 'disetujui'): ?>
        <a href="buka_kunci_laporan.php?id=<?= $id ?>"
           class="btn btn-outline-warning btn-sm">🔓 Buka Kunci</a>

    <?php elseif($status === 'ditolak'): ?>
        <span class="badge bg-danger">Ditolak</span>

    <?php endif; ?>

<!-- ================= SEKOLAH ================= -->

<?php else: ?>

    <?php if($status === 'ditolak' && $isOwner): ?>
        <a href="edit_laporan.php?id=<?= $id ?>"
           class="btn btn-warning btn-sm">✏️</a>
        <a href="hapus_laporan.php?id=<?= $id ?>"
           onclick="return confirm('Hapus laporan?')"
           class="btn btn-danger btn-sm">🗑️</a>

    <?php elseif($status === 'disetujui'): ?>
        <?php if(!empty($r['file_rekomendasi']) && file_exists(__DIR__.'/upload/rekomendasi/'.$r['file_rekomendasi'])): ?>
            <a href="upload/rekomendasi/<?= rawurlencode($r['file_rekomendasi']) ?>"
               target="_blank"
               class="btn btn-primary btn-sm">⬇️ Rekom</a>
        <?php endif; ?>

        <a href="alasan_buka_kunci.php?id=<?= $id ?>"
           class="btn btn-outline-warning btn-sm">🔓 Buka Kunci</a>

    <?php elseif($status === 'minta_buka_kunci'): ?>
        <span class="badge bg-warning text-dark">Menunggu Admin</span>

    <?php else: ?>
        <span class="badge bg-secondary">-</span>

    <?php endif; ?>

<?php endif; ?>

</div>
</td>
</tr>
<?php } ?>

<?php if(mysqli_num_rows($data)==0){ ?>
<tr>
<td colspan="10" class="text-center text-muted">
    Belum ada data laporan
</td>
</tr>
<?php } ?>

</tbody>
</table>
</div>
</div>
</div>

</body>
</html>
