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

$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");
$future = date("Y-m-d", strtotime("+7 days"));

// Event mendatang 7 hari (dan masih aktif)
$query = mysqli_query($koneksi,
    "SELECT * FROM kegiatan 
     WHERE tanggal >= '$now' AND tanggal <= '$future' AND status_event = 1
     ORDER BY tanggal ASC"
);
?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Agenda 7 Hari — 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-aktif{background:#198754;}
.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 mb-3">
        <h1 class="page-title">Agenda 7 Hari Kedepan</h1>
        <a href="semua_kegiatan.php" class="btn btn-secondary btn-sm"><i class="fa fa-arrow-left"></i> Kembali</a>
    </div>

    <p class="subtitle small text-muted">Periode: <?= date("d M Y") ?> — <?= date("d M Y", strtotime("+7 day")); ?></p>

    <div class="card shadow-sm">
        <div class="card-header bg-success text-white fw-bold">Jadwal Kegiatan Terdekat</div>
        <div class="card-body table-responsive">

            <!-- ALERT MESSAGE -->
            <?php if (isset($_GET['deleted'])): ?>
                <div class="alert alert-success alert-dismissible fade show">Agenda berhasil dihapus!
                    <button class="btn-close" data-bs-dismiss="alert"></button></div>
            <?php endif; ?>
            <?php if (isset($_GET['updated'])): ?>
                <div class="alert alert-info alert-dismissible fade show">Berhasil diupdate!
                    <button class="btn-close" data-bs-dismiss="alert"></button></div>
            <?php endif; ?>
            <!-- END ALERT -->

            <table class="table table-bordered table-striped align-middle">
                <thead class="table-success text-center">
                    <tr>
                        <th>No</th>
                        <th>Foto</th>
                        <th>Nama Kegiatan</th>
                        <th>Tanggal</th>
                        <th>Status</th>
                        <th>Deskripsi</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $no=1;
                if(mysqli_num_rows($query) > 0):
                while($row=mysqli_fetch_assoc($query)):
                    $expired = (strtotime($row['tanggal']) < strtotime($now));
                ?>
                    <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-success text-center"><?= date("d M Y", strtotime($row['tanggal'])) ?></td>
                        <td class="text-center">
                            <?= $expired ? '<span class="badge badge-expired">Berakhir</span>' : '<span class="badge badge-aktif">Aktif</span>' ?>
                        </td>
                        <td><?= esc(substr($row['deskripsi'],0,60)) ?>...</td>
                        <td class="text-center">
                            <a href="edit_kegiatan.php?id=<?= $row['id']; ?>" class="btn btn-warning btn-sm"><i class="fa fa-edit"></i></a>
                            <a class="btn btn-danger btn-sm"
                               onclick="return confirm('Hapus agenda?')"
                               href="hapus_kegiatan.php?id=<?= $row['id']; ?>"><i class="fa fa-trash"></i></a>
                        </td>
                    </tr>
                <?php endwhile;
                else: ?>
                    <tr><td colspan="7" class="text-center text-muted">Tidak ada acara dalam 7 hari kedepan.</td></tr>
                <?php endif; ?>
                </tbody>
            </table>

        </div>
    </div>
</main>
</div>

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