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

if($_SESSION['role'] !== 'admin'){
    die("Akses ditolak");
}

$id = intval($_GET['id']);

$q = mysqli_query($conn,"
    SELECT nama_sekolah, bulan, tahun
    FROM upload_laporan
    WHERE id_laporan='$id'
");
$r = mysqli_fetch_assoc($q);

if(!$r){
    die("Data tidak ditemukan");
}

if(isset($_POST['tolak'])){
    $alasan = mysqli_real_escape_string($conn, $_POST['alasan']);

    if(empty($alasan)){
        echo "<script>alert('Alasan wajib diisi');</script>";
    } else {
        mysqli_query($conn,"
            UPDATE upload_laporan SET
            status_verifikasi='ditolak',
            alasan_penolakan='$alasan'
            WHERE id_laporan='$id'
        ");

        header("Location: list_laporan.php");
        exit;
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Tolak Laporan</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container mt-5">
<div class="card shadow-sm">
    <div class="card-header bg-danger text-white">
        ❌ Tolak Laporan
    </div>
    <div class="card-body">
        <p>
            <strong>Sekolah:</strong> <?= $r['nama_sekolah'] ?><br>
            <strong>Periode:</strong> <?= $r['bulan'] ?> <?= $r['tahun'] ?>
        </p>

        <form method="post">
            <div class="mb-3">
                <label class="form-label fw-bold">Alasan Penolakan</label>
                <textarea name="alasan" class="form-control" rows="4" required
                    placeholder="Contoh: Dokumen RKAS tidak lengkap / format tidak sesuai ARKAS"></textarea>
            </div>

            <div class="d-flex gap-2">
                <button type="submit" name="tolak" class="btn btn-danger">
                    ❌ Tolak Laporan
                </button>
                <a href="list_laporan.php" class="btn btn-secondary">
                    ↩️ Batal
                </a>
            </div>
        </form>
    </div>
</div>
</div>

</body>
</html>
