<?php
include('koneksi.php');
session_start();

// 1. Proteksi Admin
if (!isset($_SESSION['status']) || $_SESSION['role'] != "admin") {
    die("Akses ditolak. Anda tidak memiliki izin untuk mengakses halaman ini.");
}

// 2. Ambil parameter tanggal dari URL (jika ada)
$tgl_mulai = isset($_GET['mulai']) ? mysqli_real_escape_string($koneksi, $_GET['mulai']) : '';
$tgl_selesai = isset($_GET['selesai']) ? mysqli_real_escape_string($koneksi, $_GET['selesai']) : '';

// 3. LOGIKA PEMILIHAN TAMPILAN
// Jika tanggal kosong, tampilkan FORM INPUT. Jika ada, jalankan DOWNLOAD EXCEL.
if (empty($tgl_mulai) || empty($tgl_selesai)) {
    // TAMPILAN FORM INPUT (HTML)
?>
    <!DOCTYPE html>
    <html lang="id">
    <head>
        <meta charset="UTF-8">
        <title>Cetak Rekapitulasi - PIAUD</title>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
        <style>
            body { background-color: #f4f7f6; height: 100vh; display: flex; align-items: center; }
            .card-rekap { border: none; border-radius: 15px; box-shadow: 0 10px 25px rgba(0,0,0,0.1); }
        </style>
    </head>
    <body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-5">
                <div class="card card-rekap">
                    <div class="card-header bg-success text-white text-center py-3">
                        <h5 class="mb-0"><i class="fa fa-file-excel mr-2"></i> Rekapitulasi Plagiasi</h5>
                    </div>
                    <div class="card-body p-4">
                        <form action="" method="GET">
                            <div class="form-group">
                                <label class="font-weight-bold">Dari Tanggal</label>
                                <input type="date" name="mulai" class="form-control" value="<?= date('Y-m-01') ?>" required>
                            </div>
                            <div class="form-group">
                                <label class="font-weight-bold">Sampai Tanggal</label>
                                <input type="date" name="selesai" class="form-control" value="<?= date('Y-m-d') ?>" required>
                            </div>
                            <hr>
                            <button type="submit" class="btn btn-success btn-block shadow-sm font-weight-bold">
                                <i class="fa fa-download mr-2"></i> GENERATE EXCEL
                            </button>
                            <a href="admin_dashboard.php" class="btn btn-light btn-block mt-2 border">Batal</a>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    </body>
    </html>
<?php
    exit; // Berhenti di sini agar bagian Excel di bawah tidak tereksekusi
}

// 4. BAGIAN DOWNLOAD EXCEL (Hanya jalan jika tgl_mulai & tgl_selesai terisi)
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=Rekap_Cek_Plagiasi_PIAUD_" . $tgl_mulai . "_sd_" . $tgl_selesai . ".xls");
header("Pragma: no-cache");
header("Expires: 0");
?>

<style>
    .header-rekap { font-family: sans-serif; text-align: center; margin-bottom: 20px; }
    table { border-collapse: collapse; width: 100%; font-family: sans-serif; }
    th { background-color: #006400; color: white; padding: 10px; border: 1px solid #000; }
    td { padding: 8px; vertical-align: top; border: 1px solid #000; }
    .bg-selesai { background-color: #e8f5e9; }
</style>

<div class="header-rekap">
    <h2>REKAPITULASI PEMERIKSAAN PLAGIARISME MAHASISWA PIAUD</h2>
    <p>Periode Pengajuan: <strong><?= date('d/m/Y', strtotime($tgl_mulai)) ?></strong> s/d <strong><?= date('d/m/Y', strtotime($tgl_selesai)) ?></strong></p>
</div>

<table border="1">
    <thead>
        <tr>
            <th width="50">No</th>
            <th width="120">NIM</th>
            <th width="250">Nama Mahasiswa</th>
            <th width="450">Judul Karya Ilmiah</th>
            <th width="100">Hasil (%)</th>
            <th width="150">Nomor Surat</th>
            <th width="150">Status Periksa</th>
            <th width="200">Link Hasil (G-Drive)</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $no = 1;
        $query = "SELECT * FROM pengajuan_plagiasi 
                  WHERE DATE(tgl_pengajuan) BETWEEN '$tgl_mulai' AND '$tgl_selesai'
                  ORDER BY tgl_pengajuan ASC";

        $data = mysqli_query($koneksi, $query);
        
        if(mysqli_num_rows($data) > 0) {
            while ($row = mysqli_fetch_assoc($data)) {
                $is_selesai = ($row['status_periksa'] == 'Selesai');
                ?>
                <tr class="<?= $is_selesai ? 'bg-selesai' : '' ?>">
                    <td align="center"><?= $no++; ?></td>
                    <td>'<?= $row['nim']; ?></td> 
                    <td><?= strtoupper($row['nama']); ?></td>
                    <td><?= $row['judul']; ?></td>
                    <td align="center"><?= $row['hasil_plagiasi'] ? $row['hasil_plagiasi'].'%' : '-'; ?></td>
                    <td align="center"><?= $row['no_surat_resmi'] ? $row['no_surat_resmi'] : '-'; ?></td>
                    <td align="center"><strong><?= strtoupper($row['status_periksa']); ?></strong></td>
                    <td><?= $row['link_hasil'] ? $row['link_hasil'] : '-'; ?></td>
                </tr>
                <?php
            }
        } else {
            echo "<tr><td colspan='8' align='center'>Tidak ada data aktivitas pada periode ini.</td></tr>";
        }
        ?>
    </tbody>
</table>