<?php
// We will just bundle all logic for Jadwal in index.php (simplified CRUD for brevity)
require_once __DIR__ . '/../../config/session.php';
requireAdmin();
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../functions/helper.php';
require_once __DIR__ . '/../../functions/csrf.php';

$db = getDB();


$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;
$offset = ($page - 1) * $limit;

$total_rows = $db->query("SELECT COUNT(*) FROM jadwal_posyandu")->fetchColumn();
$total_pages = ceil($total_rows / $limit);

$stmt = $db->query("SELECT * FROM jadwal_posyandu ORDER BY tanggal DESC LIMIT $limit OFFSET $offset");
$jadwal = $stmt->fetchAll();

?>
<?php require_once '../../includes/header_admin.php'; ?>

<div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4 mb-6">
    <div>
        <h1 class="text-2xl font-bold text-gray-800">Manajemen Jadwal</h1>
        <p class="text-gray-500 text-sm mt-1">Kelola agenda kegiatan posyandu bulanan.</p>
    </div>
    <div>
        <a href="tambah.php" class="inline-flex items-center justify-center px-4 py-2 bg-primary hover:bg-primary-dark text-white font-medium rounded-lg transition-colors shadow-sm focus:ring-2 focus:ring-offset-2 focus:ring-primary">
            <i class="fas fa-calendar-plus mr-2"></i> Tambah Jadwal
        </a>
    </div>
</div>

<?php $csrfDelete = generateCSRFToken(); ?>

<div class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
    <div class="px-6 py-4 border-b border-gray-100 bg-gray-50/50">
        <h3 class="font-bold text-gray-800">Daftar Jadwal Posyandu</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="w-full text-sm text-left whitespace-nowrap">
            <thead class="text-xs text-gray-500 uppercase bg-gray-50 border-b border-gray-100">
                <tr>
                    <th class="px-6 py-4 font-semibold">Tanggal / Waktu</th>
                    <th class="px-6 py-4 font-semibold">Detail Kegiatan</th>
                    <th class="px-6 py-4 font-semibold text-center">Aksi</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                <?php if(count($jadwal) > 0): ?>
                    <?php foreach($jadwal as $j): ?>
                        <?php 
                            $is_past = (new DateTime($j['tanggal'])) < (new DateTime(date('Y-m-d')));
                        ?>
                        <tr class="hover:bg-gray-50 transition-colors <?= $is_past ? 'opacity-60' : '' ?>">
                            <td class="px-6 py-4">
                                <div class="font-bold <?= $is_past ? 'text-gray-500' : 'text-primary' ?> text-base mb-1">
                                    <?= formatDate($j['tanggal']) ?>
                                </div>
                                <div class="text-xs text-gray-500 flex items-center gap-1.5">
                                    <i class="far fa-clock"></i> <?= substr($j['waktu_mulai'],0,5) ?> - <?= substr($j['waktu_selesai'],0,5) ?> WIB
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-normal min-w-[200px]">
                                <div class="font-bold text-gray-900"><?= escape($j['kegiatan']) ?></div>
                                <div class="text-xs text-gray-500 mt-1 flex items-center gap-1.5">
                                    <i class="fas fa-map-marker-alt text-red-400"></i> <?= escape($j['lokasi']) ?>
                                </div>
                            </td>
                            <td class="px-6 py-4 text-center space-x-1 whitespace-nowrap">
                                <a href="edit.php?id=<?= $j['id'] ?>" class="inline-flex items-center justify-center w-8 h-8 rounded-lg bg-yellow-50 text-yellow-600 hover:bg-yellow-500 hover:text-white transition-colors" title="Edit">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a href="hapus.php?id=<?= $j['id'] ?>&token=<?= urlencode($csrfDelete) ?>" onclick="return confirm('Yakin ingin menghapus jadwal ini? Tindakan ini tidak dapat dibatalkan.')" class="inline-flex items-center justify-center w-8 h-8 rounded-lg bg-red-50 text-red-600 hover:bg-red-500 hover:text-white transition-colors" title="Hapus">
                                    <i class="fas fa-trash-alt"></i>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="3" class="px-6 py-12 text-center text-gray-500">
                            <i class="fas fa-calendar-times text-4xl mb-3 text-gray-300 block"></i>
                            Belum ada jadwal yang diinput.
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?= renderPagination($total_pages, $page, './', $_GET) ?>
</div>

<?php require_once '../../includes/footer_admin.php'; ?>
