<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container">
    <h2 class="text-center mt-4 mb-3"><?= esc($title ?? 'Attendance History') ?></h2>

    <div class="card mb-4">
        <div class="card-body">
            <a href="<?= base_url('attendance/violations') ?>" class="btn btn-secondary mb-3">Back to Attendance</a>

            <p class="text-muted small mb-3">Showing absences and lates for this student within the violation window.</p>
            <?php
                $viCode    = $violation_code ?? null;
                $viDateRaw = $violation_date ?? '';
                $viDateLbl = $viDateRaw !== '' ? date('m-d-Y', strtotime($viDateRaw)) : '—';
                $viNotifRaw = $violation_notified ?? null;
                $viNotif = (is_string($viNotifRaw) && in_array(strtolower(trim($viNotifRaw)), ['yes', '1'], true))
                    || ((int)$viNotifRaw === 1);
            ?>
            <h5 class="card-title mb-3">Attendance Violations</h5>

            <table class="table table-striped attendance-history-table no-mgmt-sticky" data-no-mgmt-sticky="1">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Status</th>
                        <th>Reason</th>
                        <th>Notified</th>
                        <th>Reported</th>
                    </tr>
                </thead>
                <tbody>
                <?php if (!empty($show_violation_summary) && ($viCode || $viDateRaw !== '')): ?>
                    <tr class="table-info">
                        <td><?= esc($viDateLbl) ?></td>
                        <td><?= esc($viCode ?: '—') ?></td>
                        <td>Violation summary</td>
                        <td><?= $viNotif ? 'Yes' : 'No' ?></td>
                        <td>—</td>
                    </tr>
                <?php endif; ?>
                <?php if (!empty($attendance) && is_array($attendance)): ?>
                    <?php foreach ($attendance as $record): ?>
                        <?php
                            $dateStr    = (string)($record['date'] ?? '');
                            $ymd        = $dateStr !== '' ? substr($dateStr, 0, 10) : '';
                            $dateLabel  = $ymd !== '' ? date('m-d-Y', strtotime($ymd)) : '—';
                            $statusRaw  = strtolower(trim((string)($record['status'] ?? '')));   // absent/late
                            $reason     = trim((string)($record['reason'] ?? ''));         // may come from tracking join
                            $notifRaw   = $record['is_notified'] ?? null;
                            $isNotified = (is_string($notifRaw) && in_array(strtolower(trim($notifRaw)), ['yes', '1'], true))
                                || ((int)$notifRaw === 1);
                            $reportedRaw = $record['is_reported'] ?? null;
                            $isReported  = (is_string($reportedRaw) && in_array(strtolower(trim($reportedRaw)), ['yes', '1'], true))
                                || ((int)$reportedRaw === 1);

                            $isAbs  = $statusRaw === 'absent' || $statusRaw === 'abs' || $statusRaw === 'a' || str_starts_with($statusRaw, 'abs');
                            $isLate = $statusRaw === 'late' || $statusRaw === 'l' || str_starts_with($statusRaw, 'late');

                            $status = $statusRaw !== '' ? ucfirst($statusRaw) : 'Unknown';
                            if ($isAbs) {
                                $status = $isReported ? 'Reported Absence' : 'Unreported Absence';
                            } elseif ($isLate) {
                                $status = $isReported ? 'Reported Late' : 'Unreported Late';
                            }
                        ?>
                        <tr>
                            <td><?= esc($dateLabel) ?></td>
                            <td><?= esc($status) ?></td>
                            <td><?= $reason !== '' ? esc($reason) : 'No reason provided' ?></td>
                            <td><?= $isNotified ? 'Yes' : 'No' ?></td>
                            <td><?= $isReported  ? 'Yes' : 'No' ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr><td colspan="5" class="text-center text-muted">No attendance violations found for this student.</td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?= $this->endSection() ?>

<?= $this->section('styles') ?>
<style>
/* Ensure table header is not sticky on this view to avoid overlap */
.attendance-history-table thead th {
    position: static !important;
    top: auto !important;
    background: #f8f9fa;
    z-index: 1;
}
</style>
<?= $this->endSection() ?>
