<?php $isEdit = !empty($sheet); ?>
<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><?= $isEdit ? 'Edit' : 'New' ?> Safety Data Sheet</h5>
    <a href="<?= url('/Sds') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="rs-card p-3">
<form method="post" action="<?= url('/Sds/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="sds_id" value="<?= (int) ($sheet['sds_id'] ?? 0) ?>">
    <div class="row g-2">
        <div class="col-md-8"><label class="form-label">Name *</label>
            <input type="text" class="form-control form-control-sm" name="name" required value="<?= e($sheet['name'] ?? '') ?>" placeholder="e.g. Diesel Fuel">
        </div>
        <div class="col-md-4"><label class="form-label">Code</label>
            <input type="text" class="form-control form-control-sm" name="code" value="<?= e($sheet['code'] ?? '') ?>" placeholder="SDS-045">
        </div>
        <div class="col-md-4"><label class="form-label">Manufacturer</label>
            <input type="text" class="form-control form-control-sm" name="manufacturer" value="<?= e($sheet['manufacturer'] ?? '') ?>">
        </div>
        <div class="col-md-4"><label class="form-label">Supplier</label>
            <input type="text" class="form-control form-control-sm" name="supplier" value="<?= e($sheet['supplier'] ?? '') ?>">
        </div>
        <div class="col-md-2"><label class="form-label">CAS #</label>
            <input type="text" class="form-control form-control-sm" name="cas_number" value="<?= e($sheet['cas_number'] ?? '') ?>">
        </div>
        <div class="col-md-2"><label class="form-label">Hazard class</label>
            <select name="hazard_class" class="form-select form-select-sm">
                <option value="">—</option>
                <?php foreach (\App\Models\Sds::HAZARD_CLASSES as $val => $lbl): ?>
                    <option value="<?= e($val) ?>" <?= ($sheet['hazard_class'] ?? '') === $val ? 'selected' : '' ?>><?= e($lbl) ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <div class="col-md-3"><label class="form-label">Issued at</label>
            <input type="date" class="form-control form-control-sm" name="issued_at" value="<?= e($sheet['issued_at'] ?? '') ?>">
        </div>
        <div class="col-md-3"><label class="form-label">Next review</label>
            <input type="date" class="form-control form-control-sm" name="review_at" value="<?= e($sheet['review_at'] ?? '') ?>">
        </div>
        <div class="col-md-6"><label class="form-label">PPE required</label>
            <input type="text" class="form-control form-control-sm" name="ppe_required" value="<?= e($sheet['ppe_required'] ?? '') ?>" placeholder="Nitrile gloves, safety goggles, respirator">
        </div>

        <div class="col-md-6"><label class="form-label">Hazards</label>
            <textarea class="form-control form-control-sm" name="hazards" rows="3" placeholder="Flammable liquid + vapour. Skin/eye irritant."><?= e($sheet['hazards'] ?? '') ?></textarea>
        </div>
        <div class="col-md-6"><label class="form-label">Storage notes</label>
            <textarea class="form-control form-control-sm" name="storage_notes" rows="3" placeholder="Cool, well-ventilated, bunded area. Away from ignition sources."><?= e($sheet['storage_notes'] ?? '') ?></textarea>
        </div>
        <div class="col-md-6"><label class="form-label">First aid</label>
            <textarea class="form-control form-control-sm" name="first_aid" rows="3"><?= e($sheet['first_aid'] ?? '') ?></textarea>
        </div>
        <div class="col-md-6"><label class="form-label">Disposal notes</label>
            <textarea class="form-control form-control-sm" name="disposal_notes" rows="3"><?= e($sheet['disposal_notes'] ?? '') ?></textarea>
        </div>
    </div>
    <div class="mt-3">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
    </div>
</form>
</div>

<?php if ($isEdit): ?>
    <form method="post" action="<?= url('/Sds/delete/' . (int) $sheet['sds_id']) ?>"
          class="mt-3 rs-confirm-submit"
          data-confirm-title="Delete SDS?"
          data-confirm-body="Soft-delete <strong><?= e($sheet['name']) ?></strong>? Attached PDFs remain in storage."
          data-confirm-ok="Delete">
        <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
        <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash me-1"></i>Delete SDS</button>
    </form>
<?php endif; ?>
