<?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';

$error = '';
$success = '';
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, "INSERT INTO aktiviti (nama_aktiviti, penganjur, peringkat, tempat, tarikh_mula, tarikh_tamat) VALUES (?, ?, ?, ?, ?, ?)");
        mysqli_stmt_bind_param($stmt, "ssssss", $nama_aktiviti, $penganjur, $peringkat, $tempat, $tarikh_mula, $tarikh_tamat);
        if (mysqli_stmt_execute($stmt)) {
            $success = "Aktiviti berjaya ditambah.";
        } else {
            $error = "Ralat semasa tambah aktiviti.";
        }
    }
}
?>

<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: 30px 25px;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.08);
    max-width: 600px;
    margin: 0 auto;
}

.page-title {
    font-size: 32px;
    font-weight: 700;
    color: #3b0a77;
    margin-bottom: 25px;
    text-align: center;
}

.form-label {
    font-weight: 600;
    margin-bottom: 5px;
}

.form-control {
    padding: 8px 12px;
    border-radius: 8px;
    border: 1px solid #ccc;
}

.btn-primary {
    background-color: #3b0a77;
    border: none;
    padding: 10px 20px;
    font-weight: 600;
    border-radius: 8px;
    transition: 0.2s;
}
.btn-primary:hover {
    background-color: #2e065e;
}

.alert {
    padding: 10px 15px;
    border-radius: 8px;
    margin-bottom: 20px;
    font-weight: 500;
}

.alert-danger {
    background-color: #f8d7da;
    color: #842029;
}

.alert-success {
    background-color: #d1e7dd;
    color: #0f5132;
}
</style>

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

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

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

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

            <div class="mb-3">
                <label class="form-label">Peringkat</label>
                <select name="peringkat" class="form-control" required>
                    <option value="">-- Pilih Peringkat --</option>
                    <?php 
                    $levels = ['Kolej', 'Daerah', 'Negeri', 'Kebangsaan', 'Antarabangsa'];
                    foreach ($levels as $level):
                        $selected = (($_POST['peringkat'] ?? '') === $level) ? 'selected' : '';
                    ?>
                        <option value="<?= $level ?>" <?= $selected ?>><?= $level ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

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

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

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

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

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