<?php
include '../koneksi.php';

echo "<h1>✏️ EDIT TANGGAL UJIAN</h1>";

// Update tanggal ujian untuk kelas MERDEKA 5
$tanggal_mulai_baru = date('Y-m-d H:i:s'); // Sekarang
$tanggal_selesai_baru = date('Y-m-d H:i:s', strtotime('+7 days')); // 7 hari dari sekarang

$sql = "UPDATE ujian SET 
        tanggal_mulai = '$tanggal_mulai_baru',
        tanggal_selesai = '$tanggal_selesai_baru'
        WHERE kelas = 'MERDEKA 5'";

if (mysqli_query($koneksi, $sql)) {
    $affected = mysqli_affected_rows($koneksi);
    echo "<div style='background: #d4edda; padding: 20px; border-radius: 10px;'>";
    echo "✅ <strong>BERHASIL UPDATE $affected UJIAN!</strong><br><br>";
    echo "Tanggal mulai: $tanggal_mulai_baru<br>";
    echo "Tanggal selesai: $tanggal_selesai_baru<br><br>";
    echo "<a href='../siswa/ujian.php' style='background: #28a745; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;'>CEK HALAMAN SISWA</a>";
    echo "</div>";
} else {
    echo "<div style='background: #f8d7da; padding: 20px; border-radius: 10px;'>";
    echo "❌ Gagal update: " . mysqli_error($koneksi);
    echo "</div>";
}

// Tampilkan ujian setelah update
echo "<h2>Ujian Setelah Update:</h2>";
$result = mysqli_query($koneksi, "SELECT * FROM ujian WHERE kelas = 'MERDEKA 5'");
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Nama Ujian</th><th>Kelas</th><th>Mulai</th><th>Selesai</th><th>Status</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
    $sekarang = date('Y-m-d H:i:s');
    $status = ($sekarang < $row['tanggal_mulai']) ? '⏳ Belum Mulai' : 
             (($sekarang > $row['tanggal_selesai']) ? '❌ Selesai' : '✅ Aktif');
    
    echo "<tr>";
    echo "<td>{$row['nama_ujian']}</td>";
    echo "<td>{$row['kelas']}</td>";
    echo "<td>{$row['tanggal_mulai']}</td>";
    echo "<td>{$row['tanggal_selesai']}</td>";
    echo "<td>{$status}</td>";
    echo "</tr>";
}
echo "</table>";
?>