<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>

<!-- Circle Styles -->

<link rel="stylesheet" href="<?= base_url('assets/css/parent_dashboard.css?v=1.1') ?>">
<!-- Dashboard Circles -->
<div class="container py-5">
    <div class="row g-4 justify-content-center text-center">

        <!-- Parent_Guide -->
        <div class="col-auto">
            <a href="<?= base_url('parent_guide.pdf') ?>"
                target="_blank"
                rel="noopener noreferrer"
                class="d-inline-block text-decoration-none">
                <div class="circle-box bg-info position-relative" role="button" aria-label="Open Parent Guide PDF">
                    <h4 class="fw-bold mb-0">Your Quick Guide</h4>
                </div>
            </a>
        </div>
        <!-- Notifications -->
        <div class="col-auto">
            <div class="circle-box bg-primary position-relative">
                <h4 class="fw-bold">Notifications</h4>
                <small><?= count($notifications ?? []) ?> new</small>
                <div class="hover-popup" style="max-height: 250px; overflow-y: auto;">
                    <?php if (!empty($notifications)): ?>
                        <ul class="mb-0 ps-3">
                            <?php foreach ($notifications as $n): ?>
                                <li class="mb-2">
                                    <strong><?= esc($n['title']) ?></strong><br>
                                    <small class="text-muted">
                                        <?= isset($n['created_at']) && strtotime($n['created_at'])
                                            ? esc(local_datetime($n['created_at'], 'm-d-Y H:i'))
                                            : 'No date' ?>
                                    </small>
                                    <br>
                                    <span class="badge bg-light text-dark"><?= ucfirst($n['notification_type'] ?? 'broadcast') ?></span>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php else: ?>
                        <p class="mb-0">No new notifications.</p>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Student Info -->
        <div class="col-auto">
            <div class="circle-box bg-success">
                <h4 class="fw-bold">Students</h4>
                <small><?= count($students ?? []) ?> total</small>
                <div class="hover-popup">
                    <?php if (!empty($students)): ?>
                        <ul class="mb-0 ps-3">
                            <?php foreach ($students as $student): ?>
                                <li><?= esc($student['firstname']) ?> <?= esc($student['lastname']) ?> - <?= esc($student['grade'] ?? 'N/A') ?></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php else: ?>
                        <p class="mb-0">No student data.</p>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Attendance -->
        <div class="col-auto">
            <div class="circle-box bg-danger">
                <h4 class="fw-bold">Absences</h4>
                <?php
                $absentCount = 0;
                foreach ($attendance ?? [] as $a) {
                    if (strtolower($a['status']) === 'absent') $absentCount++;
                }
                ?>
                <small><?= $absentCount ?> absent</small>
                <div class="hover-popup">
                    <?php if ($absentCount): ?>
                        <ul class="mb-0 ps-3">
                            <?php foreach ($attendance as $a): ?>
                                <?php if (strtolower($a['status']) === 'absent'): ?>
                                    <li><?= esc($a['date'] ? local_date($a['date'], 'm-d-Y') : '') ?> - <?= esc($a['firstname']) ?> <?= esc($a['lastname']) ?></li>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </ul>
                    <?php else: ?>
                        <p class="mb-0">No absences.</p>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Report Absence -->
        <div class="col-auto">
            <a href="<?= base_url('parent/report-attendance') ?>" class="d-inline-block text-decoration-none">
                <div class="circle-box bg-secondary text-white position-relative" role="button" aria-label="Report absence or late">
                    <h4 class="fw-bold mb-0">Report Absence</h4>
                </div>
            </a>
        </div>

        <!-- Scores -->
        <div class="col-auto">
            <div class="circle-box bg-warning text-dark">
                <h4 class="fw-bold">Scores</h4>
                <small><?= count($scores ?? []) ?> entries</small>
                <div class="hover-popup">
                    <?php if (!empty($scores)): ?>
                        <ul class="mb-0 ps-3">
                            <?php foreach ($scores as $score): ?>
                                <li><?= esc($score['grade'] ?? 'N/A') ?> - <?= esc($score['firstname']) ?> <?= esc($score['lastname']) ?></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php else: ?>
                        <p class="mb-0">No scores yet.</p>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Report Cards -->
        <div class="col-auto">
            <a href="<?= base_url('parent/report-cards') ?>" class="d-inline-block text-decoration-none">
                <div class="circle-box bg-dark text-white position-relative" role="button" aria-label="Open report cards">
                    <h4 class="fw-bold mb-0">Report Cards</h4>
                </div>
            </a>
        </div>

        <!-- Enrollment -->
        <div class="col-auto">
            <div class="circle-box bg-info text-white">
                <h4 class="fw-bold">Enrollment</h4>
                <small>Info</small>
                <div class="hover-popup">
                    <div>
                        <strong>Enrollment deadline:</strong>
                        <?= $lastDayOfRegistration ? local_date($lastDayOfRegistration, 'm-d-Y') : 'N/A' ?>
                    </div>
                    <!-- <div><strong>Withdrawal:</strong> <?= esc($withdrawalDeadline ?? 'N/A') ?></div> -->
                    <hr class="my-2">
                    <?php if (!empty($enrollments)): ?>
                        <ul class="mb-0 ps-3">
                            <?php foreach ($enrollments as $e): ?>
                                <li><?= esc($e['class_name'] ?? 'N/A') ?> on <?= esc(!empty($e['enrollment_date']) ? local_date($e['enrollment_date'], 'm-d-Y') : 'N/A') ?></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php else: ?>
                        <p class="mb-0">No enrollment records.</p>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <!-- Payment -->
        <div class="col-auto">
            <div class="circle-box bg-secondary position-relative">
                <h4 class="fw-bold">Payment</h4>
                <small>$<?= number_format($paymentBalance, 2) ?></small>
                <div class="hover-popup">
                    <p class="mb-0">Latest outstanding balance from the most recent invoice.</p>
                </div>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
