<?php
require_once __DIR__ . "/../../app/config/auth.php";
require_login();
require_once __DIR__ . "/../../app/config/app.php";
require_once __DIR__ . "/../../app/config/session.php";
require_once __DIR__ . "/../../app/config/koneksi.php";

// Ambil user
$nama     = getSession('pengurus_nama') ?? 'Pengurus';
$jabatan  = getSession('pengurus_jabatan') ?? '—';
$fotoUser = getSession('pengurus_foto')
    ? base_url("/assets/img/pengurus/" . getSession('pengurus_foto'))
    : base_url("/assets/img/pengurus/user_default.png");

function esc($v) { return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }

$currentPage = "kegiatan";

$now = date("Y-m-d");

// Ambil kegiatan yang sudah lewat / nonaktif
$query = mysqli_query($koneksi,
    "SELECT id, nama_kegiatan, tanggal, deskripsi, foto, status_event 
     FROM kegiatan
     WHERE tanggal < '$now' OR status_event = 0
     ORDER BY tanggal DESC"
);
?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Riwayat Kegiatan — Sistem Informasi Masjid</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/sidebar.css'); ?>">
<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/layout.css'); ?>">
<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/dashboard.css'); ?>">
<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/widgets.css'); ?>">

<style>
.thumbnail-img {
    width: 75px;
    height: 45px;
    object-fit: cover;
    border-radius: 6px;
}
.badge-expired { background:#dc3545; }
</style>

</head>

<body class="dashboard-page">

<?php include __DIR__ . "/../../assets/component/dashboard_sidebar.php"; ?>
<div id="content-wrapper" class="content-wrapper">
<?php include __DIR__ . "/../../assets/component/dashboard_navbar.php"; ?>

<main class="container-fluid mt-4">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h1 class="page-title">Riwayat Kegiatan</h1>
        <a href="semua_kegiatan.php" class="btn btn-secondary"><i class="fa fa-arrow-left"></i> Kembali</a>
    </div>

    <p class="subtitle small text-muted">Daftar kegiatan yang sudah selesai atau non-aktif</p>

    <div class="card shadow-sm">
        <div class="card-header bg-danger text-white fw-bold">
            Data Kegiatan Berakhir
        </div>

        <div class="card-body table-responsive">
            <table class="table table-bordered table-striped align-middle">
                <thead class="table-danger">
                    <tr class="text-center">
                        <th>No</th>
                        <th>Foto</th>
                        <th>Nama Kegiatan</th>
                        <th>Tanggal</th>
                        <th>Status</th>
                        <th>Deskripsi</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $no = 1;
                if ($query && mysqli_num_rows($query) > 0):
                    while ($row = mysqli_fetch_assoc($query)):
                ?>
                    <tr>
                        <td class="text-center"><?= $no++; ?></td>

                        <td class="text-center">
                            <img src="<?= base_url('/assets/img/kegiatan/' . esc($row['foto'])); ?>" class="thumbnail-img">
                        </td>

                        <td><?= esc($row['nama_kegiatan']) ?></td>

                        <td class="fw-bold text-danger"><?= date("d M Y", strtotime($row['tanggal'])); ?></td>

                        <td class="text-center">
                            <span class="badge badge-expired">Berakhir</span>
                        </td>

                        <td>
                            <?= esc(substr($row['deskripsi'], 0, 55)); ?>...
                        </td>
                    </tr>
                <?php endwhile; else: ?>
                    <tr>
                        <td colspan="6" class="text-center text-muted">
                            Tidak ada kegiatan yang sudah berakhir.
                        </td>
                    </tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>

    </div>

</main>
</div>

<?php include __DIR__ . "/../../assets/component/dashboard_footer.php"; ?>

</body>
</html>
