<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-triangle-exclamation me-2 text-danger"></i><?= $incident ? 'Edit Incident' : 'New Incident' ?></h5>
    <a href="<?= url('/Incidents') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<form method="post" action="<?= url('/Incidents/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="incident_id" value="<?= (int) ($incident['incident_id'] ?? 0) ?>">

    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2">Overview</h6>
        <div class="row g-2">
            <div class="col-md-8"><label class="form-label">Title *</label><input type="text" class="form-control form-control-sm" name="title" required value="<?= e($incident['title'] ?? '') ?>"></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($incident['code'] ?? '') ?>"></div>
            <div class="col-md-3"><label class="form-label">Category *</label>
                <select name="category" class="form-select form-select-sm" required>
                    <?php foreach (['equipment','external','health_safety'] as $c): ?><option value="<?= $c ?>" <?= ($incident['category'] ?? '') === $c ? 'selected' : '' ?>><?= $c ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3"><label class="form-label">Type</label>
                <select name="incident_type_id" class="form-select form-select-sm"><option value="">— none —</option>
                    <?php foreach ($types as $t): ?><option value="<?= (int) $t['incident_type_id'] ?>" <?= (int) ($incident['incident_type_id'] ?? 0) === (int) $t['incident_type_id'] ? 'selected' : '' ?>>[<?= e($t['category']) ?>] <?= e($t['name']) ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3"><label class="form-label">Severity</label>
                <select name="severity" class="form-select form-select-sm">
                    <?php foreach (['low','medium','high','critical'] as $s): ?><option <?= ($incident['severity'] ?? 'medium') === $s ? 'selected' : '' ?>><?= $s ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3"><label class="form-label">Status</label>
                <select name="status" class="form-select form-select-sm">
                    <?php foreach (['draft','open','investigating','rca','closed_permanent','closed_temporary','cancelled'] as $s): ?><option <?= ($incident['status'] ?? 'draft') === $s ? 'selected' : '' ?>><?= $s ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="col-12"><label class="form-label">Description</label><textarea class="form-control form-control-sm" name="description" rows="3"><?= e($incident['description'] ?? '') ?></textarea></div>
        </div>
    </div>

    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2">Where + Who</h6>
        <div class="row g-2">
            <div class="col-md-4"><label class="form-label">Asset</label>
                <select name="asset_id" class="form-select form-select-sm"><option value="">— none —</option>
                    <?php foreach ($assets as $a): ?><option value="<?= (int) $a['asset_id'] ?>" <?= (int) ($incident['asset_id'] ?? 0) === (int) $a['asset_id'] ? 'selected' : '' ?>><?= e($a['name']) ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-4"><label class="form-label">Site</label>
                <select name="site_id" class="form-select form-select-sm"><option value="">— none —</option>
                    <?php foreach ($sites as $s): ?><option value="<?= (int) $s['site_id'] ?>" <?= (int) ($incident['site_id'] ?? 0) === (int) $s['site_id'] ? 'selected' : '' ?>><?= e($s['name']) ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-4"><label class="form-label">Assigned To</label>
                <select name="assigned_to" class="form-select form-select-sm"><option value="">— unassigned —</option>
                    <?php foreach ($users as $u): ?><option value="<?= (int) $u['user_id'] ?>" <?= (int) ($incident['assigned_to'] ?? 0) === (int) $u['user_id'] ? 'selected' : '' ?>><?= e($u['username']) ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-6"><label class="form-label">Start</label><input type="datetime-local" class="form-control form-control-sm" name="start_at" value="<?= e(str_replace(' ', 'T', (string) ($incident['start_at'] ?? ''))) ?>"></div>
            <div class="col-md-6"><label class="form-label">End</label><input type="datetime-local" class="form-control form-control-sm" name="end_at" value="<?= e(str_replace(' ', 'T', (string) ($incident['end_at'] ?? ''))) ?>"></div>
        </div>
    </div>

    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2">Failure Analysis (RCA)</h6>
        <div class="row g-2">
            <div class="col-md-8"><label class="form-label">Failure Code</label>
                <select name="failure_code_id" class="form-select form-select-sm">
                    <option value="">— unclassified —</option>
                    <?php foreach (($failureCodes ?? []) as $fc): ?>
                        <option value="<?= (int) $fc['failure_code_id'] ?>" <?= (int) ($incident['failure_code_id'] ?? 0) === (int) $fc['failure_code_id'] ? 'selected' : '' ?>>
                            <?= e($fc['code']) ?> · <?= e($fc['mode']) ?> — <?= e($fc['mechanism']) ?> / <?= e($fc['cause']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
                <small class="text-muted">ISO 14224-style taxonomy — feeds MTBF/MTTR reports.</small>
            </div>
            <div class="col-md-4"><label class="form-label">Downtime (minutes)</label>
                <input type="number" class="form-control form-control-sm" name="downtime_minutes" min="0" value="<?= e((string) ($incident['downtime_minutes'] ?? '')) ?>" placeholder="e.g. 90">
            </div>
            <div class="col-md-6"><label class="form-label">Root Cause</label><textarea class="form-control form-control-sm" name="root_cause" rows="3"><?= e($incident['root_cause'] ?? '') ?></textarea></div>
            <div class="col-md-6"><label class="form-label">Resolution</label><textarea class="form-control form-control-sm" name="resolution" rows="3"><?= e($incident['resolution'] ?? '') ?></textarea></div>
        </div>
    </div>

    <div class="d-flex gap-2">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
        <?php if (!empty($incident['incident_id'])): ?><a class="btn btn-sm btn-outline-danger" href="<?= url('/Incidents/delete/' . (int) $incident['incident_id']) ?>" onclick="return confirm('Delete?')"><i class="fas fa-trash me-1"></i>Delete</a><?php endif; ?>
    </div>
</form>
