<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-flask-vial me-2 text-primary"></i>Safety Data Sheets</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-primary" href="<?= url('/Sds/create') ?>"><i class="fas fa-plus me-1"></i>New SDS</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">Total</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">Overdue review</div><div class="fw-bold fs-5 text-danger"><?= (int) $counts['overdue'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Due ≤ 60d</div><div class="fw-bold fs-5 text-warning"><?= (int) $counts['due_soon'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">No review date</div><div class="fw-bold fs-5 text-muted"><?= (int) $counts['no_review_date'] ?></div></div>
</div>

<form method="get" action="<?= url('/Sds') ?>" class="rs-card p-2 mb-3">
    <div class="row g-2 align-items-end">
        <div class="col-md-4"><input type="search" name="q" class="form-control form-control-sm" placeholder="Search name / code / manufacturer / CAS…" value="<?= e($filters['q'] ?? '') ?>"></div>
        <div class="col-md-3">
            <select name="hazard_class" class="form-select form-select-sm">
                <option value="">All hazard classes</option>
                <?php foreach (\App\Models\Sds::HAZARD_CLASSES as $val => $lbl): ?>
                    <option value="<?= e($val) ?>" <?= ($filters['hazard_class'] ?? '') === $val ? 'selected' : '' ?>><?= e($lbl) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-3 d-flex align-items-end">
            <div class="form-check">
                <input type="checkbox" class="form-check-input" name="due_soon" value="1" id="dueSoon" <?= !empty($filters['due_soon']) ? 'checked' : '' ?>>
                <label for="dueSoon" class="form-check-label">Review due soon</label>
            </div>
        </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>

<?php if (empty($sheets)): ?>
    <div class="rs-card p-4 text-center text-muted">No SDS match your filter.</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>Name</th><th>Manufacturer</th><th>Hazard class</th><th>CAS</th>
                <th class="text-center">Review</th><th></th>
            </tr></thead>
            <tbody>
                <?php foreach ($sheets as $s):
                    $days = $s['days_to_review'] !== null ? (int) $s['days_to_review'] : null;
                    if ($days === null)           $rev = ['bg-secondary text-muted border', '—'];
                    elseif ($days < 0)            $rev = ['bg-danger', 'overdue ' . abs($days) . 'd'];
                    elseif ($days <= 60)          $rev = ['bg-warning text-dark', 'in ' . $days . 'd'];
                    else                          $rev = ['bg-success', 'ok · ' . $days . 'd'];
                ?>
                    <tr>
                        <td>
                            <a href="<?= url('/Sds/show/' . (int) $s['sds_id']) ?>"><strong><?= e($s['name']) ?></strong></a>
                            <?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['manufacturer'] ?? '—') ?></td>
                        <td class="small"><?= e(\App\Models\Sds::HAZARD_CLASSES[$s['hazard_class']] ?? '—') ?></td>
                        <td class="small text-muted"><?= e($s['cas_number'] ?? '—') ?></td>
                        <td class="text-center"><span class="badge <?= $rev[0] ?>" style="font-size:.6rem;"><?= e($rev[1]) ?></span></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; ?>
