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

/* =====================
   PROTEKSI ADMIN
===================== */
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    die("⛔ Akses ditolak");
}

/* =====================
   VALIDASI ID
===================== */
$id = (int)($_GET['id'] ?? $_POST['id'] ?? 0);
if ($id <= 0) die("ID tidak valid");

/* =====================
   AMBIL DATA LAPORAN
===================== */
$stmt = $conn->prepare("
    SELECT id_laporan, npsn, nama_sekolah, bulan, tahun,
           status_verifikasi, file_rekomendasi, alasan_buka_kunci
    FROM upload_laporan
    WHERE id_laporan=?
");

if (!$stmt) {
    die("Query error: " . $conn->error);
}

$stmt->bind_param("i", $id);
$stmt->execute();
$data = $stmt->get_result()->fetch_assoc();

if (!$data) die("Data tidak ditemukan");

/* =====================
   HANYA JIKA SUDAH DISETUJUI
===================== */
if ($data['status_verifikasi'] !== 'minta_buka_kunci') {
    die("Belum ada permohonan buka kunci dari sekolah");
}

/* =====================
   PROSES SUBMIT
===================== */
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $alasan = trim($_POST['alasan']);

    if ($alasan === '') {
        $error = "Alasan buka kunci wajib diisi";
    } else {

        /* HAPUS FILE REKOMENDASI */
        if (!empty($data['file_rekomendasi'])) {
            $file = "upload/rekomendasi/" . $data['file_rekomendasi'];
            if (file_exists($file)) unlink($file);
        }

        /* UPDATE STATUS */
        $stmt = $conn->prepare("
            UPDATE upload_laporan SET
                status_verifikasi = 'ditolak',
                catatan_admin = ?,
                file_rekomendasi = NULL,
                tgl_verifikasi = NULL
            WHERE id_laporan = ?
        ");
        $stmt->bind_param("si", $alasan, $id);
        $stmt->execute();

        header("Location:list_laporan.php?unlock=ok");
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Buka Kunci Laporan</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.container{max-width:600px;}
</style>
</head>

<body class="bg-light">
<div class="container mt-4">

<div class="card shadow-sm">
<div class="card-header bg-warning fw-bold">
🔓 Buka Kunci Laporan
</div>

<div class="card-body">

<table class="table table-sm">
<tr><th>NPSN</th><td><?= htmlspecialchars($data['npsn']) ?></td></tr>
<tr><th>Nama Sekolah</th><td><?= htmlspecialchars($data['nama_sekolah']) ?></td></tr>
<tr><th>Bulan</th><td><?= htmlspecialchars($data['bulan']) ?></td></tr>
<tr><th>Tahun</th><td><?= htmlspecialchars($data['tahun']) ?></td></tr>
</table>

<?php if(!empty($error)): ?>
<div class="alert alert-danger"><?= $error ?></div>
<?php endif; ?>

<form method="post">
<input type="hidden" name="id" value="<?= $id ?>">

<div class="mb-3">
<label class="fw-semibold">📝 Alasan Buka Kunci <span class="text-danger">*</span></label>
<?php if(!empty($data['alasan_buka_kunci'])){ ?>
<div class="alert alert-info">
    <b>📨 Alasan dari Sekolah:</b><br>
    <?= nl2br(htmlspecialchars($data['alasan_buka_kunci'])) ?>
</div>
<?php } ?>
<textarea name="alasan" rows="3" class="form-control" required
placeholder="Contoh: Ditemukan ketidaksesuaian nominal BKU dengan laporan..."></textarea>
<small class="text-muted">
Alasan ini akan terlihat oleh pihak sekolah
</small>
</div>

<div class="d-flex gap-2">
<button class="btn btn-danger">🔓 Buka Kunci</button>
<a href="list_laporan.php" class="btn btn-secondary">↩ Batal</a>
</div>

</form>

</div>
</div>

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