<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><?= e($incident['code'] ?? '#' . $incident['incident_id']) ?> · <?= e($incident['title']) ?></h5>
    <div class="d-flex gap-2">
        <a class="btn btn-sm btn-outline-primary" href="<?= url('/Incidents/edit/' . (int) $incident['incident_id']) ?>"><i class="fas fa-pen me-1"></i>Edit</a>
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Incidents/pdf/' . (int) $incident['incident_id']) ?>" target="_blank"><i class="fas fa-file-pdf me-1"></i>PDF</a>
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Incidents') ?>"><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>Category</dt><dd><span class="badge bg-secondary text-uppercase"><?= e($incident['category']) ?></span></dd></div>
        <div class="col-md-3"><dt>Type</dt><dd><?= e($incident['type_name'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Severity</dt><dd><span class="badge bg-secondary text-uppercase"><?= e($incident['severity']) ?></span></dd></div>
        <div class="col-md-3"><dt>Status</dt><dd><span class="badge bg-secondary text-uppercase"><?= e($incident['status']) ?></span></dd></div>
        <div class="col-md-3"><dt>Asset</dt><dd><?= e($incident['asset_name'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Site</dt><dd><?= e($incident['site_name'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Reported by</dt><dd><?= e($incident['reported_username'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Assigned to</dt><dd><?= e($incident['assigned_username'] ?? 'unassigned') ?></dd></div>
        <div class="col-md-4"><dt>Start</dt><dd><?= e($incident['start_at'] ?? '—') ?></dd></div>
        <div class="col-md-4"><dt>End</dt><dd><?= e($incident['end_at'] ?? '—') ?></dd></div>
        <div class="col-md-4"><dt>Closed at</dt><dd><?= e($incident['closed_at'] ?? '—') ?></dd></div>
        <div class="col-md-4"><dt>Failure Code</dt><dd>
            <?php if (!empty($incident['failure_code'])): ?>
                <span class="badge bg-dark text-uppercase me-1"><?= e($incident['failure_code']) ?></span>
                <span class="small"><?= e($incident['failure_mode'] ?? '') ?></span>
                <div class="small text-muted"><?= e($incident['failure_mechanism'] ?? '') ?> / <?= e($incident['failure_cause'] ?? '') ?></div>
            <?php else: ?>—<?php endif; ?>
        </dd></div>
        <div class="col-md-4"><dt>Downtime</dt><dd>
            <?php if ($incident['downtime_minutes'] !== null): ?>
                <?php $mins = (int) $incident['downtime_minutes']; $h = intdiv($mins, 60); $m = $mins % 60; ?>
                <strong><?= (int) $mins ?></strong> min<?php if ($h > 0): ?> <span class="text-muted small">(<?= $h ?>h <?= $m ?>m)</span><?php endif; ?>
            <?php else: ?>—<?php endif; ?>
        </dd></div>
    </dl>
</div>

<div class="row g-3">
    <div class="col-md-4"><div class="rs-card p-3 h-100"><h6 class="fw-bold mb-2">Description</h6><p class="small mb-0"><?= nl2br(e($incident['description'] ?? '')) ?: '<span class="text-muted">No description.</span>' ?></p></div></div>
    <div class="col-md-4"><div class="rs-card p-3 h-100"><h6 class="fw-bold mb-2">Root Cause</h6><p class="small mb-0"><?= nl2br(e($incident['root_cause'] ?? '')) ?: '<span class="text-muted">Not documented.</span>' ?></p></div></div>
    <div class="col-md-4"><div class="rs-card p-3 h-100"><h6 class="fw-bold mb-2">Resolution</h6><p class="small mb-0"><?= nl2br(e($incident['resolution'] ?? '')) ?: '<span class="text-muted">No resolution yet.</span>' ?></p></div></div>
</div>

<div class="rs-card p-3 mt-3">
    <h6 class="fw-bold mb-2">Activity Feed</h6>
    <form method="post" action="<?= url('/Incidents/note/' . (int) $incident['incident_id']) ?>" class="mb-3">
        <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
        <label class="form-label">Add a note</label>
        <textarea class="form-control form-control-sm" name="message" rows="2" required></textarea>
        <button type="submit" class="btn btn-sm btn-primary mt-2"><i class="fas fa-paper-plane me-1"></i>Post note</button>
    </form>
    <?php if (!empty($events)): ?>
        <ul class="rs-feed">
            <?php foreach ($events as $ev): ?>
                <li>
                    <span class="badge bg-secondary text-uppercase me-1"><?= e($ev['event_type']) ?></span>
                    <strong><?= e($ev['username'] ?? 'system') ?></strong>
                    <?php if ($ev['event_type'] === 'status_change'): ?>: <em><?= e($ev['from_value'] ?? '—') ?></em> → <em><?= e($ev['to_value'] ?? '—') ?></em><?php endif; ?>
                    <?php if (!empty($ev['message'])): ?> — <?= e($ev['message']) ?><?php endif; ?>
                    <small class="text-muted d-block"><?= e($ev['created_at']) ?></small>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php else: ?><p class="text-muted small mb-0">No activity yet.</p><?php endif; ?>
</div>

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