<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>
<?php
    $isEditable = (bool) ($isEditable ?? true);
    $disabledAttr = $isEditable ? '' : ' disabled';
?>
<div class="container my-5">
    <div class="d-flex flex-wrap align-items-center justify-content-between gap-2 mb-3">
        <div>
            <h3 class="text-success mb-1" style="font-family: Arial, sans-serif;">Report Cards</h3>
            <div class="text-muted small">
                <?= esc($schoolYear ?: 'N/A') ?> <?= $semester ? '• ' . esc($semester) . ' Semester' : '' ?>
            </div>
        </div>
    </div>

    <?php if (session()->getFlashdata('error')): ?>
        <div class="alert alert-danger"><?= esc(session()->getFlashdata('error')) ?></div>
    <?php endif; ?>
    <?php if (session()->getFlashdata('success')): ?>
        <div class="alert alert-success"><?= esc(session()->getFlashdata('success')) ?></div>
    <?php endif; ?>
    <?php if (! $isEditable): ?>
        <div class="alert alert-info">
            You are viewing <?= esc($schoolYear ?: 'a closed school year') ?>. Report card signatures are read-only for closed years.
        </div>
    <?php endif; ?>

    <?php if (empty($students)): ?>
        <div class="alert alert-info">No students available for report cards.</div>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table table-striped table-bordered align-middle">
                <thead class="table-light">
                    <tr>
                        <th>Student</th>
                        <th>Class Section</th>
                        <th>Viewed</th>
                        <th>Signature</th>
                        <th class="text-end">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($students as $student): ?>
                        <?php
                            $sid = (int) ($student['id'] ?? 0);
                            $ack = $ackMap[$sid] ?? null;
                            $viewedAt = $ack['viewed_at'] ?? '';
                            $signedAt = $ack['signed_at'] ?? '';
                            $signedName = $ack['signed_name'] ?? '';
                        ?>
                        <tr>
                            <td><?= esc(trim(($student['firstname'] ?? '') . ' ' . ($student['lastname'] ?? ''))) ?></td>
                            <td><?= esc($student['class_section_name'] ?? 'N/A') ?></td>
                            <td><?= $viewedAt ? esc(local_datetime($viewedAt, 'm-d-Y H:i')) : 'Not viewed' ?></td>
                            <td>
                                <?php if ($signedAt): ?>
                                    <?= esc($signedName ?: 'Signed') ?><br>
                                    <small class="text-muted"><?= esc(local_datetime($signedAt, 'm-d-Y H:i')) ?></small>
                                <?php else: ?>
                                    <span class="text-muted">Not signed</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end">
                                <a class="btn btn-sm btn-outline-primary" target="_blank" href="<?= base_url('parent/report-cards/view/' . $sid) ?>">View Report</a>
                                <?php if (! $signedAt): ?>
                                    <form class="d-inline-flex align-items-center gap-2 ms-2" method="post" action="<?= base_url('parent/report-cards/sign/' . $sid) ?>">
                                        <?= csrf_field() ?>
                                        <input type="text" name="signed_name" class="form-control form-control-sm" placeholder="Full name" required<?= $disabledAttr ?>>
                                        <button class="btn btn-sm btn-success" type="submit"<?= $disabledAttr ?>>Sign</button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>
<?= $this->endSection() ?>
