<?php
$hazardLabel = \App\Models\Sds::HAZARD_CLASSES[$sheet['hazard_class']] ?? null;
$daysToReview = null;
if (!empty($sheet['review_at'])) {
    $daysToReview = (int) floor((strtotime($sheet['review_at']) - strtotime(date('Y-m-d'))) / 86400);
}
if ($daysToReview === null)   $revBadge = ['bg-secondary text-muted border', 'no review date'];
elseif ($daysToReview < 0)    $revBadge = ['bg-danger',                'overdue ' . abs($daysToReview) . ' days'];
elseif ($daysToReview <= 60)  $revBadge = ['bg-warning text-dark',     'due in ' . $daysToReview . ' days'];
else                          $revBadge = ['bg-success',               'ok · ' . $daysToReview . ' days'];
?>
<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><?= e($sheet['name']) ?>
        <?php if (!empty($sheet['code'])): ?><code class="small text-muted"><?= e($sheet['code']) ?></code><?php endif; ?>
    </h5>
    <div class="d-flex gap-2">
        <?php if ($canManage): ?><a class="btn btn-sm btn-outline-primary" href="<?= url('/Sds/edit/' . (int) $sheet['sds_id']) ?>"><i class="fas fa-pen me-1"></i>Edit</a><?php endif; ?>
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Sds') ?>"><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>Manufacturer</dt><dd><?= e($sheet['manufacturer'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Supplier</dt><dd><?= e($sheet['supplier'] ?? '—') ?></dd></div>
        <div class="col-md-2"><dt>CAS #</dt><dd><?= e($sheet['cas_number'] ?? '—') ?></dd></div>
        <div class="col-md-2"><dt>Hazard class</dt><dd><?= e($hazardLabel ?? '—') ?></dd></div>
        <div class="col-md-2"><dt>Review</dt><dd><span class="badge <?= $revBadge[0] ?> text-uppercase"><?= e($revBadge[1]) ?></span></dd></div>
        <div class="col-md-3"><dt>Issued</dt><dd><?= e($sheet['issued_at'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Next review</dt><dd><?= e($sheet['review_at'] ?? '—') ?></dd></div>
        <div class="col-md-6"><dt>PPE required</dt><dd><?= e($sheet['ppe_required'] ?? '—') ?></dd></div>
    </dl>
</div>

<div class="row g-3">
    <div class="col-md-6"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2 text-danger"><i class="fas fa-triangle-exclamation me-1"></i>Hazards</h6>
        <p class="small mb-0"><?= nl2br(e($sheet['hazards'] ?? '')) ?: '<span class="text-muted">Not documented.</span>' ?></p>
    </div></div>
    <div class="col-md-6"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2 text-primary"><i class="fas fa-warehouse me-1"></i>Storage</h6>
        <p class="small mb-0"><?= nl2br(e($sheet['storage_notes'] ?? '')) ?: '<span class="text-muted">Not documented.</span>' ?></p>
    </div></div>
    <div class="col-md-6"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2 text-success"><i class="fas fa-kit-medical me-1"></i>First aid</h6>
        <p class="small mb-0"><?= nl2br(e($sheet['first_aid'] ?? '')) ?: '<span class="text-muted">Not documented.</span>' ?></p>
    </div></div>
    <div class="col-md-6"><div class="rs-card p-3 h-100">
        <h6 class="fw-bold mb-2 text-secondary"><i class="fas fa-recycle me-1"></i>Disposal</h6>
        <p class="small mb-0"><?= nl2br(e($sheet['disposal_notes'] ?? '')) ?: '<span class="text-muted">Not documented.</span>' ?></p>
    </div></div>
</div>

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