<?php
session_start();
if (!isset($_SESSION['id_admin'])) {
    header('Location: login_admin.php');
    exit;
}
include 'includes/header_admin.php';
include 'includes/sidebar_admin.php';
include 'includes/config.php';

$id = $_GET['id'] ?? null;
if (!$id) {
    header('Location: senarai_aktiviti.php');
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $nama_aktiviti = trim($_POST['nama_aktiviti']);
    $penganjur = trim($_POST['penganjur']);
    $peringkat = trim($_POST['peringkat']);
    $tempat = trim($_POST['tempat']);
    $tarikh_mula = $_POST['tarikh_mula'];
    $tarikh_tamat = $_POST['tarikh_tamat'];

    if (!$nama_aktiviti || !$penganjur || !$peringkat || !$tempat || !$tarikh_mula || !$tarikh_tamat) {
        $error = "Sila isi semua medan.";
    } elseif ($tarikh_mula > $tarikh_tamat) {
        $error = "Tarikh mula mesti sebelum atau sama dengan tarikh tamat.";
    } else {
        $stmt = mysqli_prepare($conn, "UPDATE aktiviti SET nama_aktiviti=?, penganjur=?, peringkat=?, tempat=?, tarikh_mula=?, tarikh_tamat=? WHERE id=?");
        mysqli_stmt_bind_param($stmt, "ssssssi", $nama_aktiviti, $penganjur, $peringkat, $tempat, $tarikh_mula, $tarikh_tamat, $id);
        if (mysqli_stmt_execute($stmt)) {
            echo "<script>alert('Aktiviti berjaya dikemaskini.'); window.location='senarai_aktiviti.php';</script>";
            exit;
        } else {
            $error = "Ralat semasa kemaskini aktiviti.";
        }
    }
} else {
    $stmt = mysqli_prepare($conn, "SELECT * FROM aktiviti WHERE id=? LIMIT 1");
    mysqli_stmt_bind_param($stmt, "i", $id);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    $aktiviti = mysqli_fetch_assoc($result);
    if (!$aktiviti) {
        header('Location: senarai_aktiviti.php');
        exit;
    }
}

// Senarai peringkat
$peringkat_options = ["Kolej", "Daerah", "Negeri", "Kebangsaan", "Antarabangsa"];
?>

<style>
.main-content {
    margin-left: 180px;
    margin-top: 100px;
    padding: 30px 40px;
    background-color: #f8f9fc;
    min-height: calc(100vh - 100px);
    font-family: 'Rubik', sans-serif;
}
.card-container {
    background: white;
    padding: 25px 30px;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.08);
    max-width: 700px;
    margin: 0 auto;
}
.page-title {
    font-size: 28px;
    font-weight: 700;
    color: #3b0a77;
    margin-bottom: 20px;
}
.form-control, .form-select {
    border-radius: 8px;
    padding: 8px 12px;
}
.btn-primary {
    background-color: #0d6efd;
    border: none;
    padding: 10px 18px;
    border-radius: 8px;
    font-weight: 600;
}
.btn-primary:hover {
    background-color: #0b5ed7;
}
.alert-danger {
    background-color: #f8d7da;
    color: #842029;
    padding: 10px 15px;
    border-radius: 8px;
    margin-bottom: 20px;
}
</style>

<div class="main-content">
    <div class="card-container">
        <h2 class="page-title">Kemaskini Aktiviti</h2>

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

        <form method="post">
            <div class="mb-3">
                <label>Nama Aktiviti</label>
                <input type="text" name="nama_aktiviti" class="form-control" value="<?= htmlspecialchars($aktiviti['nama_aktiviti']) ?>" required>
            </div>

            <div class="mb-3">
                <label>Penganjur</label>
                <input type="text" name="penganjur" class="form-control" value="<?= htmlspecialchars($aktiviti['penganjur']) ?>" required>
            </div>

            <div class="mb-3">
                <label>Peringkat</label>
                <select name="peringkat" class="form-select" required>
                    <option value="">-- Pilih Peringkat --</option>
                    <?php foreach ($peringkat_options as $option): ?>
                        <option value="<?= $option ?>" <?= ($aktiviti['peringkat'] === $option) ? 'selected' : '' ?>><?= $option ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="mb-3">
                <label>Tempat</label>
                <input type="text" name="tempat" class="form-control" value="<?= htmlspecialchars($aktiviti['tempat']) ?>" required>
            </div>

            <div class="mb-3">
                <label>Tarikh Mula</label>
                <input type="date" name="tarikh_mula" class="form-control" value="<?= $aktiviti['tarikh_mula'] ?>" required>
            </div>

            <div class="mb-3">
                <label>Tarikh Tamat</label>
                <input type="date" name="tarikh_tamat" class="form-control" value="<?= $aktiviti['tarikh_tamat'] ?>" required>
            </div>

            <button type="submit" class="btn btn-primary">Kemaskini Aktiviti</button>
        </form>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
