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

// ======================================================
// AUTO NON-AKTIF EVENT YG TANGGALNYA SUDAH LEWAT
// ======================================================
mysqli_query($koneksi,
    "UPDATE kegiatan SET status_event = 0 WHERE tanggal < CURDATE()"
);

// ======================================================
// Pagination
// ======================================================
$limit = 8;
$page  = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$start = ($page - 1) * $limit;

// Total event aktif
$totalQuery = mysqli_query($koneksi,
    "SELECT COUNT(*) AS total FROM kegiatan WHERE status_event = 1"
);
$totalData = mysqli_fetch_assoc($totalQuery)['total'];
$totalPage = ceil($totalData / $limit);

// Query data aktif
$query = mysqli_query($koneksi,
    "SELECT * FROM kegiatan 
     WHERE status_event = 1
     ORDER BY tanggal ASC
     LIMIT $start, $limit"
);
?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Semua Agenda — 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;
}
</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">Semua Agenda</h1>
        <a href="tambah_kegiatan.php" class="btn btn-success">
            <i class="fa fa-plus"></i> Tambah Agenda
        </a>
    </div>
    <p class="subtitle small text-muted">Daftar kegiatan masjid yang masih aktif</p>

    <div class="card shadow-sm">
        <div class="card-header bg-success text-white fw-bold">
            Daftar Agenda Aktif
        </div>

        <div class="card-body table-responsive">

            <!-- ALERT MESSAGE -->
            <?php if (isset($_GET['deleted'])): ?>
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    Agenda berhasil dihapus!
                    <button type="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" role="alert">
                    Agenda berhasil diperbarui!
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>

            <?php if (isset($_GET['error'])): ?>
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    Terjadi kesalahan saat memproses!
                    <button type="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</th>
                        <th>Tanggal</th>
                        <th>Deskripsi</th>
                        <th>Status</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $no = $start + 1;
                if ($totalData > 0):
                while ($row = mysqli_fetch_assoc($query)):
                    $isExpired = (strtotime($row['tanggal']) < strtotime(date("Y-m-d")));
                ?>
                    <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><?= esc(substr($row['deskripsi'], 0, 50)); ?>...</td>
                        <td class="text-center">
                            <?= $isExpired ? '<span class="badge bg-secondary">Non-Aktif</span>' : '<span class="badge bg-success">Aktif</span>'; ?>
                        </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 href="hapus_kegiatan.php?id=<?= $row['id']; ?>"
                               onclick="return confirm('Hapus agenda ini?')"
                               class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></a>
                        </td>
                    </tr>
                <?php endwhile;
                else: ?>
                    <tr>
                        <td colspan="7" class="text-center text-muted">Belum ada kegiatan aktif.</td>
                    </tr>
                <?php endif; ?>
                </tbody>
            </table>

            <nav>
            <ul class="pagination justify-content-center mt-3">
                <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                    <a class="page-link" href="?page=<?= $page - 1 ?>">Previous</a>
                </li>
                <?php for ($i = 1; $i <= $totalPage; $i++): ?>
                <li class="page-item <?= $page == $i ? 'active' : '' ?>">
                    <a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
                </li>
                <?php endfor; ?>
                <li class="page-item <?= $page >= $totalPage ? 'disabled' : '' ?>">
                    <a class="page-link" href="?page=<?= $page + 1 ?>">Next</a>
                </li>
            </ul>
            </nav>

        </div>
    </div>

</main>
</div>

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