<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</h5>
    <div class="d-flex gap-2">
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Hse') ?>"><i class="fas fa-arrow-left me-1"></i>HSE Library</a>
        <?php if ($canManage): ?>
            <a class="btn btn-sm btn-outline-primary" href="<?= url('/Training/createCourse') ?>"><i class="fas fa-plus me-1"></i>New course</a>
            <a class="btn btn-sm btn-primary" href="<?= url('/Training/newRecord') ?>"><i class="fas fa-plus me-1"></i>Log completion</a>
        <?php endif; ?>
    </div>
</div>

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

<div class="kpi-grid mb-3">
    <div class="rs-card p-2 text-center"><div class="text-muted small">Records</div><div class="fw-bold fs-5"><?= (int) $counts['total'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Expired</div><div class="fw-bold fs-5 text-danger"><?= (int) $counts['expired'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Expiring ≤ 60d</div><div class="fw-bold fs-5 text-warning"><?= (int) $counts['expiring_soon'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Valid</div><div class="fw-bold fs-5 text-success"><?= (int) $counts['valid'] ?></div></div>
</div>

<form method="get" action="<?= url('/Training') ?>" class="rs-card p-2 mb-3">
    <div class="row g-2 align-items-end">
        <div class="col-md-3">
            <select name="course_id" class="form-select form-select-sm">
                <option value="">All courses</option>
                <?php foreach ($courses as $c): ?>
                    <option value="<?= (int) $c['course_id'] ?>" <?= (int) ($filters['course_id'] ?? 0) === (int) $c['course_id'] ? 'selected' : '' ?>><?= e($c['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-3">
            <select name="user_id" class="form-select form-select-sm">
                <option value="">All people</option>
                <?php foreach ($users as $u): ?>
                    <option value="<?= (int) $u['user_id'] ?>" <?= (int) ($filters['user_id'] ?? 0) === (int) $u['user_id'] ? 'selected' : '' ?>><?= e($u['username']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-3">
            <select name="status" class="form-select form-select-sm">
                <option value="">All statuses</option>
                <option value="expired"        <?= ($filters['status'] ?? '') === 'expired'        ? 'selected' : '' ?>>Expired</option>
                <option value="expiring_soon"  <?= ($filters['status'] ?? '') === 'expiring_soon'  ? 'selected' : '' ?>>Expiring ≤ 60d</option>
                <option value="valid"          <?= ($filters['status'] ?? '') === 'valid'          ? 'selected' : '' ?>>Valid</option>
            </select>
        </div>
        <div class="col-md-2"><button type="submit" class="btn btn-sm btn-outline-primary w-100"><i class="fas fa-filter me-1"></i>Filter</button></div>
    </div>
</form>

<h6 class="fw-bold mt-4 mb-2">Records</h6>
<?php if (empty($records)): ?>
    <div class="rs-card p-4 text-center text-muted mb-3">No training records match your filter.</div>
<?php else: ?>
    <div class="rs-card p-0 mb-3">
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead class="table-light"><tr>
                <th>User</th><th>Course</th><th>Completed</th><th>Expires</th>
                <th class="text-center">Status</th><th></th>
            </tr></thead>
            <tbody>
                <?php foreach ($records as $r):
                    $days = $r['days_to_expiry'] !== null ? (int) $r['days_to_expiry'] : null;
                    if ($days === null)         $st = ['bg-secondary text-muted border', 'no expiry'];
                    elseif ($days < 0)          $st = ['bg-danger', 'expired ' . abs($days) . 'd'];
                    elseif ($days <= 60)        $st = ['bg-warning text-dark', 'in ' . $days . 'd'];
                    else                        $st = ['bg-success', 'ok · ' . $days . 'd'];
                    $who = trim(($r['f_name'] ?? '') . ' ' . ($r['l_name'] ?? '')) ?: ($r['username'] ?? $r['external_name'] ?? '—');
                ?>
                    <tr>
                        <td><?= e($who) ?><?php if (empty($r['user_id']) && !empty($r['external_name'])): ?><span class="badge bg-info text-dark ms-1" style="font-size:.55rem;">external</span><?php endif; ?></td>
                        <td class="small"><code><?= e($r['course_code']) ?></code> <?= e($r['course_name']) ?></td>
                        <td class="small text-muted"><?= e($r['completed_at']) ?></td>
                        <td class="small"><?= e($r['expires_at'] ?? '—') ?></td>
                        <td class="text-center"><span class="badge <?= $st[0] ?>" style="font-size:.6rem;"><?= e($st[1]) ?></span></td>
                        <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="<?= url('/Training/showRecord/' . (int) $r['record_id']) ?>"><i class="fas fa-eye"></i></a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>

<h6 class="fw-bold mt-4 mb-2">Course catalogue</h6>
<?php if (empty($courses)): ?>
    <div class="rs-card p-4 text-center text-muted">No courses defined yet.</div>
<?php else: ?>
    <div class="rs-card p-0">
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead class="table-light"><tr>
                <th>Code</th><th>Name</th><th>Validity</th><th>Issuer</th>
                <th class="text-center">Records</th><th></th>
            </tr></thead>
            <tbody>
                <?php foreach ($courses as $c): ?>
                    <tr>
                        <td><code class="small"><?= e($c['code'] ?? '—') ?></code></td>
                        <td>
                            <strong><?= e($c['name']) ?></strong>
                            <?php if (!$c['is_active']): ?><span class="badge bg-secondary ms-1">inactive</span><?php endif; ?>
                            <?php if (!empty($c['description'])): ?><div class="small text-muted"><?= e($c['description']) ?></div><?php endif; ?>
                        </td>
                        <td class="small"><?= (int) $c['validity_months'] ?> months</td>
                        <td class="small text-muted"><?= e($c['issuer'] ?? '—') ?></td>
                        <td class="text-center small"><?= (int) $c['record_count'] ?></td>
                        <td class="text-end">
                            <?php if ($canManage): ?>
                                <a class="btn btn-sm btn-outline-primary" href="<?= url('/Training/editCourse/' . (int) $c['course_id']) ?>"><i class="fas fa-pen"></i></a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>
