<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-sitemap me-2 text-primary"></i>Asset Hierarchy</h5>
    <div class="d-flex gap-2">
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Assets') ?>"><i class="fas fa-list me-1"></i>Flat list</a>
        <a class="btn btn-sm btn-primary" href="<?= url('/Assets/create') ?>"><i class="fas fa-plus me-1"></i>New Asset</a>
    </div>
</div>

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

<p class="text-muted small">Assets are ordered by parent → child. Use the pen icon to move an asset under a different parent.</p>

<?php if (empty($tree)): ?>
    <div class="rs-card p-4 text-center text-muted">No assets yet.</div>
<?php else: ?>
    <div class="rs-card p-0">
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead class="table-light"><tr>
                <th>Asset</th>
                <th>Type</th>
                <th class="text-center">Criticality</th>
                <th class="text-center">Status</th>
                <th class="text-end" style="width:120px;">Actions</th>
            </tr></thead>
            <tbody>
                <?php foreach ($tree as $n):
                    $indent = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', (int) $n['depth']);
                    $crit   = $n['criticality'] ?? 'C';
                    $critBadge = ['A' => 'bg-danger', 'B' => 'bg-warning text-dark', 'C' => 'bg-secondary'][$crit] ?? 'bg-secondary';
                ?>
                    <tr>
                        <td>
                            <?= $indent ?>
                            <?php if ((int) $n['depth'] > 0): ?><i class="fas fa-level-up-alt fa-rotate-90 text-muted me-1"></i><?php endif; ?>
                            <a href="<?= url('/Assets/show/' . (int) $n['asset_id']) ?>"><strong><?= e($n['name']) ?></strong></a>
                            <?php if (!empty($n['code'])): ?><code class="small text-muted ms-1"><?= e($n['code']) ?></code><?php endif; ?>
                        </td>
                        <td class="small text-muted"><?= e($n['asset_type_name'] ?? '—') ?></td>
                        <td class="text-center"><span class="badge <?= $critBadge ?>"><?= e($crit) ?></span></td>
                        <td class="text-center"><span class="badge bg-secondary text-uppercase"><?= e($n['status']) ?></span></td>
                        <td class="text-end">
                            <a class="btn btn-sm btn-outline-primary" title="Edit"
                               href="<?= url('/Assets/edit/' . (int) $n['asset_id']) ?>"><i class="fas fa-pen"></i></a>
                            <a class="btn btn-sm btn-outline-secondary" title="View"
                               href="<?= url('/Assets/show/' . (int) $n['asset_id']) ?>"><i class="fas fa-eye"></i></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>
