<?php
session_start();
if (!isset($_SESSION['id_pelajar'])) {
    header('Location: login_pelajar.php');
    exit;
}

include 'includes/config.php';
include 'includes/header_pelajar.php';
include 'includes/sidebar_pelajar.php';

$id_pelajar = $_SESSION['id_pelajar'];
$message = '';
$message_type = ''; // 'success' atau 'danger'

// Handle Daftar / Batal
if (isset($_GET['action'], $_GET['id'])) {
    $aktiviti_id = intval($_GET['id']);
    if ($_GET['action'] === 'daftar') {
        $cek = mysqli_query($conn, "SELECT * FROM penyertaan WHERE aktiviti_id=$aktiviti_id AND pelajar_id=$id_pelajar");
        if (mysqli_num_rows($cek) === 0) {
            mysqli_query($conn, "INSERT INTO penyertaan (aktiviti_id, pelajar_id) VALUES ($aktiviti_id, $id_pelajar)");
        }
        $message = "Anda telah daftar aktiviti ini.";
        $message_type = 'success';
    } elseif ($_GET['action'] === 'batal') {
        mysqli_query($conn, "DELETE FROM penyertaan WHERE aktiviti_id=$aktiviti_id AND pelajar_id=$id_pelajar");
        $message = "Anda telah batalkan penglibatan aktiviti ini.";
        $message_type = 'danger';
    }
}

// Dapatkan semua aktiviti
$sql = "SELECT * FROM aktiviti ORDER BY tarikh_mula DESC";
$result = mysqli_query($conn, $sql);

// Dapatkan aktiviti yang pelajar dah daftar
$daftar_aktiviti = [];
$daftar_res = mysqli_query($conn, "SELECT aktiviti_id FROM penyertaan WHERE pelajar_id=$id_pelajar");
while ($row = mysqli_fetch_assoc($daftar_res)) {
    $daftar_aktiviti[] = $row['aktiviti_id'];
}
?>

<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;
}

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

.card-container {
    background: white;
    padding: 25px 30px;
    border-radius: 12px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.08);
}

.table-custom {
    width: 100%;
    border-collapse: separate !important;
    border-spacing: 0 8px !important;
    font-size: 15px;
}

.table-custom thead tr {
    background-color: #3b0a77;
    color: white;
}

.table-custom th,
.table-custom td {
    padding: 12px 18px;
    vertical-align: middle !important;
}

.table-custom tbody tr {
    background-color: white;
    box-shadow: 0 2px 6px rgba(0,0,0,0.04);
    transition: transform 0.1s ease;
}
.table-custom tbody tr:hover {
    transform: translateY(-2px);
}

.btn {
    padding: 6px 12px;
    border-radius: 6px;
    font-weight: 600;
    text-decoration: none;
}

.btn-success { background-color: #198754; color: white; }
.btn-success:hover { background-color: #146c43; }

.btn-danger { background-color: #dc3545; color: white; }
.btn-danger:hover { background-color: #b02a37; }

.btn-secondary { background-color: #6c757d; color: white; margin-bottom: 15px; }
.btn-secondary:hover { background-color: #5a6268; }

.text-center { text-align: center; color: #999; }

/* Toast dekat table */
.toast-container-local {
    margin-bottom: 20px;
}
.toast-success { background-color: #198754; color: white; }
.toast-danger { background-color: #dc3545; color: white; }
.toast-container-local .toast {
    padding: 12px 20px;
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.toast-container-local .btn-close-toast {
    background-color: white;
    border: none;
    padding: 3px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
}
</style>

<div class="main-content">
    <h2 class="page-title">Senarai Aktiviti</h2>

    <!-- Butang Kembali ke Dashboard -->
    <a href="dashboard_pelajar.php" class="btn btn-secondary mb-3">
        <i class="bi bi-arrow-left-circle"></i> Kembali ke Dashboard
    </a>

    <!-- Toast lokal -->
    <?php if($message): ?>
    <div class="toast-container-local">
        <div class="toast <?= $message_type === 'success' ? 'toast-success' : 'toast-danger' ?>" role="alert">
            <span><?= htmlspecialchars($message) ?></span>
            <button class="btn-close-toast" onclick="this.parentElement.style.display='none'">OK</button>
        </div>
    </div>
    <?php endif; ?>

    <div class="card-container">
        <table class="table table-bordered table-custom">
            <thead>
                <tr>
                    <th>Nama Aktiviti</th>
                    <th>Penganjur</th>
                    <th>Peringkat</th>
                    <th>Tempat</th>
                    <th>Tarikh Mula</th>
                    <th>Tarikh Tamat</th>
                    <th>Status</th>
                    <th style="width: 160px;">Tindakan</th>
                </tr>
            </thead>
            <tbody>
                <?php if (mysqli_num_rows($result) > 0): ?>
                    <?php while ($row = mysqli_fetch_assoc($result)): ?>
                        <?php 
                            $id = $row['id'];
                            $status = in_array($id, $daftar_aktiviti) ? 'Telah daftar' : 'Belum daftar';
                        ?>
                        <tr>
                            <td><?= htmlspecialchars($row['nama_aktiviti']) ?></td>
                            <td><?= htmlspecialchars($row['penganjur']) ?></td>
                            <td><?= htmlspecialchars($row['peringkat']) ?></td>
                            <td><?= htmlspecialchars($row['tempat']) ?></td>
                            <td><?= date("d/m/Y", strtotime($row['tarikh_mula'])) ?></td>
                            <td><?= date("d/m/Y", strtotime($row['tarikh_tamat'])) ?></td>
                            <td><?= $status ?></td>
                            <td>
                                <?php if ($status === 'Belum daftar'): ?>
                                    <a href="?action=daftar&id=<?= $id ?>" class="btn btn-success btn-sm">Daftar</a>
                                <?php else: ?>
                                    <a href="?action=batal&id=<?= $id ?>" class="btn btn-danger btn-sm">Batal</a>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="8" class="text-center">Tiada rekod aktiviti.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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