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

<div class="container-fluid py-4">

    <div class="d-flex align-items-center justify-content-between flex-wrap gap-2 mb-3">
        <div>
            <h2 class="mb-0"><i class="bi bi-journal-check me-2"></i>Certificate Audit Log</h2>
            <div class="text-muted small">Every issued certificate is recorded here for tracking and auditing.</div>
        </div>
        <a href="<?= site_url('administrator/certificates') ?>" class="btn btn-outline-secondary btn-sm">
            <i class="bi bi-award me-1"></i>Generate Certificates
        </a>
    </div>

    <!-- Year summary cards -->
    <?php if (!empty($yearSummary)): ?>
    <div class="row g-3 mb-4">
        <?php foreach ($yearSummary as $yr): ?>
        <div class="col-auto">
            <div class="card shadow-sm text-center px-4 py-2" style="min-width:150px;">
                <div class="fw-bold fs-4"><?= (int) $yr['total'] ?></div>
                <div class="text-muted small"><?= esc($yr['school_year']) ?></div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>

    <!-- Year filter -->
    <div class="card shadow-sm mb-4">
        <div class="card-body py-2">
            <form method="get" action="<?= site_url('administrator/certificates/log') ?>" class="row g-2 align-items-center">
                <div class="col-auto">
                    <label class="col-form-label fw-semibold">School Year</label>
                </div>
                <div class="col-auto">
                    <select name="school_year" class="form-select form-select-sm" onchange="this.form.submit()">
                        <option value="">— All years —</option>
                        <?php foreach ($yearSummary as $yr): ?>
                            <option value="<?= esc($yr['school_year']) ?>"
                                <?= ($yr['school_year'] === $schoolYear) ? 'selected' : '' ?>>
                                <?= esc($yr['school_year']) ?> (<?= (int) $yr['total'] ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </form>
        </div>
    </div>

    <!-- Records table -->
    <div class="card shadow-sm">
        <div class="card-header d-flex justify-content-between align-items-center">
            <span class="fw-semibold">Issued Certificates
                <span class="badge bg-secondary ms-1"><?= count($records) ?></span>
            </span>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover table-striped align-middle mb-0 no-mgmt-sticky">
                    <thead class="table-light">
                        <tr>
                            <th>Certificate #</th>
                            <th>Student</th>
                            <th>Grade</th>
                            <!--th>Cert Date</th-->
                            <th>School Year</th>
                            <!--th>Issued By</th-->
                            <th>Issued At</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($records)): ?>
                            <tr><td colspan="7" class="text-center text-muted py-4">No certificates issued yet.</td></tr>
                        <?php else: ?>
                            <?php foreach ($records as $r): ?>
                            <tr>
                                <td><code><?= esc($r['certificate_number']) ?></code></td>
                                <td><?= esc($r['student_name']) ?></td>
                                <td><?= esc($r['grade'] ?? '—') ?></td>
                                <td><?= $r['cert_date'] ? esc(date('m/d/Y', strtotime($r['cert_date']))) : '—' ?></td>
                                <td><?= esc($r['school_year'] ?? '—') ?></td>
                                <td><?= esc(trim(($r['admin_firstname'] ?? '') . ' ' . ($r['admin_lastname'] ?? '')) ?: '—') ?></td>
                                <td><?= esc($r['issued_at'] ? date('m/d/Y g:i A', strtotime($r['issued_at'])) : '—') ?></td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</div>

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