<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-shield-heart me-2 text-primary"></i>HSE Library</h5>
    <div class="d-flex gap-2">
        <a class="btn btn-sm btn-outline-primary" href="<?= url('/Sds') ?>"><i class="fas fa-flask-vial me-1"></i>SDS catalogue</a>
        <a class="btn btn-sm btn-outline-primary" href="<?= url('/Training') ?>"><i class="fas fa-graduation-cap me-1"></i>Training records</a>
    </div>
</div>

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

<p class="text-muted small">
    Central compliance library: Safety Data Sheets for every controlled substance on site, and training
    records for every operator, contractor and visitor. Both drive the dashboard tile.
</p>

<div class="row g-3 mb-3">
    <div class="col-md-6">
        <div class="rs-card p-3 h-100">
            <div class="d-flex justify-content-between align-items-baseline mb-2">
                <h6 class="fw-bold mb-0"><i class="fas fa-flask-vial me-1 text-primary"></i>Safety Data Sheets</h6>
                <a class="small" href="<?= url('/Sds') ?>">Open →</a>
            </div>
            <div class="row g-2">
                <div class="col text-center"><div class="fs-5 fw-bold"><?= (int) $sdsCounts['total'] ?></div><div class="small text-muted">total</div></div>
                <div class="col text-center"><div class="fs-5 fw-bold text-danger"><?= (int) $sdsCounts['overdue'] ?></div><div class="small text-muted">review overdue</div></div>
                <div class="col text-center"><div class="fs-5 fw-bold text-warning"><?= (int) $sdsCounts['due_soon'] ?></div><div class="small text-muted">due ≤ 60d</div></div>
                <div class="col text-center"><div class="fs-5 fw-bold text-muted"><?= (int) $sdsCounts['no_review_date'] ?></div><div class="small text-muted">no review date</div></div>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="rs-card p-3 h-100">
            <div class="d-flex justify-content-between align-items-baseline mb-2">
                <h6 class="fw-bold mb-0"><i class="fas fa-graduation-cap me-1 text-primary"></i>Training Records</h6>
                <a class="small" href="<?= url('/Training') ?>">Open →</a>
            </div>
            <div class="row g-2">
                <div class="col text-center"><div class="fs-5 fw-bold"><?= (int) $trainingCounts['total'] ?></div><div class="small text-muted">total</div></div>
                <div class="col text-center"><div class="fs-5 fw-bold text-danger"><?= (int) $trainingCounts['expired'] ?></div><div class="small text-muted">expired</div></div>
                <div class="col text-center"><div class="fs-5 fw-bold text-warning"><?= (int) $trainingCounts['expiring_soon'] ?></div><div class="small text-muted">expiring ≤ 60d</div></div>
                <div class="col text-center"><div class="fs-5 fw-bold text-success"><?= (int) $trainingCounts['valid'] ?></div><div class="small text-muted">valid</div></div>
            </div>
        </div>
    </div>
</div>

<?php if (!empty($expired) || !empty($expiring)): ?>
    <div class="rs-card p-3 mb-3" style="border-left: 4px solid var(--rs-danger);">
        <h6 class="fw-bold mb-2"><i class="fas fa-triangle-exclamation me-1 text-danger"></i>Training expiry watchlist</h6>
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead><tr><th>User</th><th>Course</th><th>Expires</th><th class="text-center">Status</th><th></th></tr></thead>
            <tbody>
                <?php foreach (array_merge($expired, $expiring) as $r):
                    $isExp = ($r['days_to_expiry'] ?? 0) < 0;
                ?>
                    <tr>
                        <td><?= e(trim(($r['f_name'] ?? '') . ' ' . ($r['l_name'] ?? '')) ?: ($r['username'] ?? $r['external_name'] ?? '—')) ?></td>
                        <td class="small"><code><?= e($r['course_code']) ?></code> <?= e($r['course_name']) ?></td>
                        <td class="small"><?= e($r['expires_at'] ?? '—') ?></td>
                        <td class="text-center">
                            <?php if ($isExp): ?>
                                <span class="badge bg-danger text-uppercase" style="font-size:.6rem;">expired <?= abs((int) $r['days_to_expiry']) ?>d</span>
                            <?php else: ?>
                                <span class="badge bg-warning text-dark text-uppercase" style="font-size:.6rem;">in <?= (int) $r['days_to_expiry'] ?>d</span>
                            <?php endif; ?>
                        </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; ?>

<?php if (!empty($reviewSoon)): ?>
    <div class="rs-card p-3">
        <h6 class="fw-bold mb-2"><i class="fas fa-flask-vial me-1 text-warning"></i>SDS reviews due within 60 days</h6>
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead><tr><th>Name</th><th>Hazard class</th><th>Review date</th><th class="text-center">Days</th><th></th></tr></thead>
            <tbody>
                <?php foreach ($reviewSoon as $s): ?>
                    <tr>
                        <td>
                            <strong><?= e($s['name']) ?></strong>
                            <?php if (!empty($s['code'])): ?><code class="small text-muted ms-1"><?= e($s['code']) ?></code><?php endif; ?>
                        </td>
                        <td class="small text-muted"><?= e($s['hazard_class'] ?? '—') ?></td>
                        <td class="small"><?= e($s['review_at']) ?></td>
                        <td class="text-center small"><?= (int) $s['days_to_review'] ?></td>
                        <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="<?= url('/Sds/show/' . (int) $s['sds_id']) ?>"><i class="fas fa-eye"></i></a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>
