<?php
$who = trim(($record['f_name'] ?? '') . ' ' . ($record['l_name'] ?? '')) ?: ($record['username'] ?? $record['external_name'] ?? '—');
$daysToExpiry = null;
if (!empty($record['expires_at'])) {
    $daysToExpiry = (int) floor((strtotime($record['expires_at']) - strtotime(date('Y-m-d'))) / 86400);
}
if ($daysToExpiry === null)     $expBadge = ['bg-secondary text-muted border', 'no expiry'];
elseif ($daysToExpiry < 0)      $expBadge = ['bg-danger',                       'expired ' . abs($daysToExpiry) . ' days ago'];
elseif ($daysToExpiry <= 60)    $expBadge = ['bg-warning text-dark',            'expires in ' . $daysToExpiry . ' days'];
else                            $expBadge = ['bg-success',                      'valid · ' . $daysToExpiry . ' days left'];
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-graduation-cap me-2 text-primary"></i>Training record #<?= (int) $record['record_id'] ?></h5>
    <div class="d-flex gap-2">
        <?php if ($canManage): ?><a class="btn btn-sm btn-outline-primary" href="<?= url('/Training/editRecord/' . (int) $record['record_id']) ?>"><i class="fas fa-pen me-1"></i>Edit</a><?php endif; ?>
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Training') ?>"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<div class="rs-card p-3 mb-3">
    <dl class="rs-def row g-2 mb-0">
        <div class="col-md-3"><dt>Who</dt><dd><?= e($who) ?><?php if (empty($record['user_id']) && !empty($record['external_name'])): ?><span class="badge bg-info text-dark ms-1" style="font-size:.55rem;">external</span><?php endif; ?></dd></div>
        <div class="col-md-3"><dt>Course</dt><dd><code class="small text-muted"><?= e($record['course_code']) ?></code> <?= e($record['course_name']) ?></dd></div>
        <div class="col-md-3"><dt>Completed</dt><dd><?= e($record['completed_at']) ?></dd></div>
        <div class="col-md-3"><dt>Expires</dt><dd><?= e($record['expires_at'] ?? '—') ?> <span class="badge <?= $expBadge[0] ?> ms-1 text-uppercase" style="font-size:.6rem;"><?= e($expBadge[1]) ?></span></dd></div>
        <div class="col-md-3"><dt>Certificate #</dt><dd><?= e($record['certificate_number'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Validity</dt><dd><?= (int) $record['validity_months'] ?> months</dd></div>
        <?php if (!empty($record['notes'])): ?><div class="col-12"><dt>Notes</dt><dd><?= nl2br(e($record['notes'])) ?></dd></div><?php endif; ?>
    </dl>
</div>

<?php require ROOT . '/app/Views/partials/_attachments.php'; ?>
