<?php
session_start();
include 'includes/config.php';

// Pastikan admin login
if (!isset($_SESSION['id_admin'])) {
    header('Location: login_admin.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $aktiviti_id = $_POST['aktiviti_id'] ?? '';
    $pelajar_id = $_POST['pelajar_id'] ?? '';
    $catatan = trim($_POST['catatan'] ?? '');

    if ($aktiviti_id == '' || $pelajar_id == '') {
        $error = "Sila pilih aktiviti dan pelajar.";
    } else {
        // Semak duplicate penyertaan
        $stmt_check = $conn->prepare("SELECT id FROM penyertaan WHERE aktiviti_id = ? AND pelajar_id = ?");
        $stmt_check->bind_param('ii', $aktiviti_id, $pelajar_id);
        $stmt_check->execute();
        $result_check = $stmt_check->get_result();

        if ($result_check->num_rows > 0) {
            $error = "Pelajar ini sudah didaftarkan dalam aktiviti yang sama.";
        } else {
            $stmt_insert = $conn->prepare("INSERT INTO penyertaan (aktiviti_id, pelajar_id, catatan) VALUES (?, ?, ?)");
            $stmt_insert->bind_param('iis', $aktiviti_id, $pelajar_id, $catatan);
            if ($stmt_insert->execute()) {
                echo "<script>alert('Penyertaan berjaya ditambah.'); window.location='senarai_penyertaan.php';</script>";
                exit;
            } else {
                $error = "Ralat semasa menambah penyertaan.";
            }
        }
    }
}

// Dapatkan senarai aktiviti dan pelajar untuk dropdown
$aktiviti_list = $conn->query("SELECT id, nama_aktiviti FROM aktiviti ORDER BY nama_aktiviti ASC");
$pelajar_list = $conn->query("SELECT id, nama FROM pelajar ORDER BY nama ASC");

include 'includes/header_admin.php';
include 'includes/sidebar_admin.php';
?>

<div style="margin-left: 200px; padding: 80px 20px; max-width: 600px;">
    <h3>Tambah Penyertaan</h3>

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

    <form method="post" novalidate>
        <div class="mb-3">
            <label>Aktiviti</label>
            <select name="aktiviti_id" class="form-select" required>
                <option value="">-- Pilih Aktiviti --</option>
                <?php while ($row = $aktiviti_list->fetch_assoc()): ?>
                    <option value="<?= $row['id'] ?>" <?= (isset($_POST['aktiviti_id']) && $_POST['aktiviti_id'] == $row['id']) ? 'selected' : '' ?>>
                        <?= htmlspecialchars($row['nama_aktiviti']) ?>
                    </option>
                <?php endwhile; ?>
            </select>
        </div>

        <div class="mb-3">
            <label>Pelajar</label>
            <select name="pelajar_id" class="form-select" required>
                <option value="">-- Pilih Pelajar --</option>
                <?php while ($row = $pelajar_list->fetch_assoc()): ?>
                    <option value="<?= $row['id'] ?>" <?= (isset($_POST['pelajar_id']) && $_POST['pelajar_id'] == $row['id']) ? 'selected' : '' ?>>
                        <?= htmlspecialchars($row['nama']) ?>
                    </option>
                <?php endwhile; ?>
            </select>
        </div>

        <div class="mb-3">
            <label>Catatan (optional)</label>
            <textarea name="catatan" class="form-control" rows="3"><?= htmlspecialchars($_POST['catatan'] ?? '') ?></textarea>
        </div>

        <button type="submit" class="btn btn-primary">Tambah Penyertaan</button>
    </form>
</div>

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