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

$sql = "SELECT * FROM aktiviti ORDER BY tarikh_mula DESC";
$result = mysqli_query($conn, $sql);
?>

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

.btn-success {
    background: #198754;
    border: none;
    padding: 8px 15px;
    font-weight: 600;
    border-radius: 8px;
    transition: 0.2s;
}
.btn-success:hover {
    background: #146c43;
}

.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-sm {
    padding: 4px 10px;
    border-radius: 6px;
}

.text-center {
    text-align: center;
    color: #999;
}
</style>

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

    <div class="card-container">
        <div style="margin-bottom: 20px;">
            <a href="tambah_aktiviti.php" class="btn btn-success">
                <i class="bi bi-plus-circle"></i> Tambah Aktiviti Baru
            </a>
        </div>

        <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 style="width: 180px;">Tindakan</th>
                </tr>
            </thead>
            <tbody>
                <?php if (mysqli_num_rows($result) > 0): ?>
                    <?php while ($row = mysqli_fetch_assoc($result)): ?>
                        <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>
                                <a href="kemaskini_aktiviti.php?id=<?= $row['id'] ?>" class="btn btn-primary btn-sm">
                                    <i class="bi bi-pencil-square"></i> Kemaskini
                                </a>
                                <form action="padam_aktiviti.php" method="get" style="display:inline;" onsubmit="return confirm('Padam aktiviti <?= addslashes($row['nama_aktiviti']) ?>? Ini tidak boleh dikembalikan.');">
                                    <input type="hidden" name="id" value="<?= $row['id'] ?>">
                                    <button type="submit" class="btn btn-danger btn-sm">
                                        <i class="bi bi-trash"></i> Padam
                                    </button>
                                </form>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="7" class="text-center">Tiada rekod aktiviti.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php include 'includes/footer.php'; ?>
