<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>Incidents</h5>
    <a class="btn btn-sm btn-primary" href="<?= url('/Incidents/create') ?>"><i class="fas fa-plus me-1"></i>New Incident</a>
</div>

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

<form method="get" action="<?= url('/Incidents') ?>" 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 title / code…" value="<?= e($filters['q'] ?? '') ?>"></div>
        <div class="col-md-2">
            <select name="category" class="form-select form-select-sm"><option value="">All categories</option>
                <?php foreach (['equipment','external','health_safety'] as $c): ?><option value="<?= $c ?>" <?= ($filters['category'] ?? '') === $c ? 'selected' : '' ?>><?= $c ?></option><?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <select name="severity" class="form-select form-select-sm"><option value="">Any severity</option>
                <?php foreach (['low','medium','high','critical'] as $s): ?><option value="<?= $s ?>" <?= ($filters['severity'] ?? '') === $s ? 'selected' : '' ?>><?= $s ?></option><?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <select name="status" class="form-select form-select-sm"><option value="">Any status</option>
                <?php foreach (['draft','open','investigating','rca','closed_permanent','closed_temporary','cancelled'] as $s): ?><option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>><?= $s ?></option><?php endforeach; ?>
            </select>
        </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>
    <div class="row g-2 align-items-end mt-2">
        <div class="col-md-2"><select name="sort" class="form-select form-select-sm" onchange="this.form.submit()"><option value="newest" <?= ($sort ?? 'newest') === 'newest' ? 'selected' : '' ?>>Newest first</option><option value="oldest" <?= ($sort ?? '') === 'oldest' ? 'selected' : '' ?>>Oldest first</option><option value="start_desc" <?= ($sort ?? '') === 'start_desc' ? 'selected' : '' ?>>Start date ↓</option><option value="severity" <?= ($sort ?? '') === 'severity' ? 'selected' : '' ?>>Severity</option><option value="status_group" <?= ($sort ?? '') === 'status_group' ? 'selected' : '' ?>>Status group</option></select></div>
    </div>
</form>

<div class="rs-card p-3">
<?php if (empty($incidents)): ?>
    <p class="text-muted small mb-0">No incidents found.</p>
<?php else: ?>
    <table class="table table-sm table-hover mb-0">
        <thead><tr><th>Code</th><th>Title</th><th>Category</th><th>Asset / Site</th><th>Severity</th><th>Status</th><th>Assigned</th><th>Start</th></tr></thead>
        <tbody>
            <?php foreach ($incidents as $i): ?>
                <tr>
                    <td class="small"><?= e($i['code'] ?? '#' . $i['incident_id']) ?></td>
                    <td><a href="<?= url('/Incidents/show/' . (int) $i['incident_id']) ?>" class="fw-semibold text-decoration-none"><?= e($i['title']) ?></a></td>
                    <td class="small"><span class="badge bg-secondary text-uppercase"><?= e($i['category']) ?></span></td>
                    <td class="small text-muted"><?= e($i['asset_name'] ?? $i['site_name'] ?? '—') ?></td>
                    <td><span class="badge bg-secondary text-uppercase"><?= e($i['severity']) ?></span></td>
                    <td><span class="badge bg-secondary text-uppercase"><?= e($i['status']) ?></span></td>
                    <td class="small text-muted"><?= e($i['assigned_username'] ?? '—') ?></td>
                    <td class="small text-muted"><?= e($i['start_at'] ?? '—') ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>
</div>
