<?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 nama user
$namaUser = getSession('pengurus_nama') ?? "Pengurus";

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

// Hitung total data
$totalQuery = mysqli_query($koneksi, 
    "SELECT COUNT(*) AS total 
     FROM log_jamaah 
     WHERE jenis = 'wanita'"
);
$totalData  = mysqli_fetch_assoc($totalQuery)['total'];
$totalPage  = ceil($totalData / $limit);

// Query data tabel
$query = mysqli_query($koneksi, 
    "SELECT id, tanggal, waktu, update_jamaah, jam_update 
     FROM log_jamaah 
     WHERE jenis = 'wanita' 
     ORDER BY id DESC 
     LIMIT $start, $limit"
);

// Page Identifier for Active Menu State
$currentPage = "jamaah_wanita";
?>
<!doctype html>
<html lang="id">
<head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width,initial-scale=1">
   <title>Jamaah Wanita — Sistem Informasi Masjid</title>

   <!-- Bootstrap + FontAwesome -->
   <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">

   <!-- Dashboard Styles -->
   <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'); ?>">
</head>

<body class="dashboard-page">

<!-- SIDEBAR -->
<?php include __DIR__ . "/../../assets/component/dashboard_sidebar.php"; ?>

<!-- CONTENT WRAPPER -->
<div id="content-wrapper" class="content-wrapper">

    <!-- NAVBAR -->
    <?php include __DIR__ . "/../../assets/component/dashboard_navbar.php"; ?>

    <main class="container-fluid mt-4">
        <h1 class="page-title">Jamaah Wanita</h1>
        <p class="subtitle small text-muted">Ringkasan cepat — <?= date('d M Y'); ?></p>

        <!--  TABEL  -->
        <div class="card mt-4">
            <div class="card-header bg-success text-white fw-bold">
                Data Jamaah Wanita
            </div>

            <div class="card-body">
                <table class="table table-striped table-bordered table-center">
                    <thead class="table-success">
                        <tr>
                            <th>No</th>
                            <th>ID</th>
                            <th>Tanggal</th>
                            <th>Waktu</th>
                            <th>Data Update</th>
                            <th>Waktu Update</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $no = $start + 1; 
                        if ($query && mysqli_num_rows($query) > 0):
                        while ($row = mysqli_fetch_assoc($query)): 
                        ?>
                        <tr>
                            <td><?= $no++ ?></td>
                            <td><?= esc($row['id']) ?></td>
                            <td class="fw-bold text-success"><?= esc($row['tanggal']) ?></td>
                            <td class="fw-bold text-success"><?= esc($row['waktu']) ?></td>
                            <td class="fw-bold text-success"><?= esc($row['update_jamaah']) ?></td>
                            <td class="fw-bold text-success"><?= esc($row['jam_update']) ?></td>
                        </tr>
                        <?php endwhile; else: ?>
                        <tr>
                            <td colspan="6" class="text-center text-muted">
                                Belum ada data Jamaah Wanita tercatat.
                            </td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>

                <!-- PAGINATION -->
                <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>

<!-- FOOTER SCRIPTS -->
<?php include __DIR__ . "/../../assets/component/dashboard_footer.php"; ?>

</body>
</html>
