<?php
$sections = [];
foreach ($fields as $f) { $sections[$f['field_section'] ?: 'General'][] = $f; }

$crit = $asset['criticality'] ?? 'C';
$critBadge = ['A' => 'bg-danger', 'B' => 'bg-warning text-dark', 'C' => 'bg-secondary'][$crit] ?? 'bg-secondary';
$warrantyText = $asset['warranty_expires_at'] ?? null;
$warrantyBadge = null;
if ($warrantyText) {
    $daysLeft = (int) floor((strtotime($warrantyText) - time()) / 86400);
    if ($daysLeft < 0)         $warrantyBadge = ['bg-secondary text-muted border', 'Expired'];
    elseif ($daysLeft <= 60)   $warrantyBadge = ['bg-warning text-dark', 'Ends in ' . $daysLeft . 'd'];
    else                       $warrantyBadge = ['bg-success', 'Active · ' . $daysLeft . 'd left'];
}

$children  = ($children  ?? []);
$ancestors = ($ancestors ?? []);
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-cogs me-2 text-primary"></i><?= e($asset['name']) ?></h5>
    <div class="d-flex gap-2">
        <a class="btn btn-sm btn-outline-primary" href="<?= url('/Assets/edit/' . (int) $asset['asset_id']) ?>"><i class="fas fa-pen me-1"></i>Edit</a>
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Assets/hierarchy') ?>"><i class="fas fa-sitemap me-1"></i>Hierarchy</a>
        <a class="btn btn-sm btn-outline-secondary" href="<?= url('/Assets') ?>"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<?php if (count($ancestors) > 1): ?>
    <nav aria-label="breadcrumb"><ol class="breadcrumb small">
        <?php foreach ($ancestors as $i => $a):
            $isLast = $i === count($ancestors) - 1;
        ?>
            <li class="breadcrumb-item <?= $isLast ? 'active' : '' ?>">
                <?php if ($isLast): ?>
                    <?= e($a['name']) ?>
                <?php else: ?>
                    <a href="<?= url('/Assets/show/' . (int) $a['asset_id']) ?>"><?= e($a['name']) ?></a>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ol></nav>
<?php endif; ?>

<?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">
    <h6 class="fw-bold mb-2">Details</h6>
    <dl class="rs-def row g-2 mb-0">
        <div class="col-md-3"><dt>Type</dt><dd><?= e($asset['asset_type_name']) ?></dd></div>
        <div class="col-md-3"><dt>Site</dt><dd><?= e($asset['site_name'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Serial</dt><dd><?= e($asset['serial_number'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Asset Tag</dt><dd><?= e($asset['asset_tag'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Status</dt><dd><span class="badge bg-secondary text-uppercase"><?= e($asset['status']) ?></span></dd></div>
        <div class="col-md-3"><dt>Criticality</dt><dd><span class="badge <?= $critBadge ?>"><?= e($crit) ?></span></dd></div>
        <div class="col-md-3"><dt>Installed</dt><dd><?= e($asset['installed_at'] ?? '—') ?></dd></div>
        <div class="col-md-3"><dt>Warranty</dt><dd>
            <?php if ($warrantyText): ?>
                <?= e($warrantyText) ?> <span class="badge <?= $warrantyBadge[0] ?> ms-1"><?= e($warrantyBadge[1]) ?></span>
            <?php else: ?>—<?php endif; ?>
        </dd></div>
        <div class="col-12"><dt>Notes</dt><dd><?= nl2br(e($asset['notes'] ?? '—')) ?></dd></div>
    </dl>
</div>

<?php if (!empty($children)): ?>
    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2"><i class="fas fa-sitemap me-1"></i>Sub-assets (<?= count($children) ?>)</h6>
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead><tr><th>Name</th><th>Type</th><th class="text-center">Crit</th><th class="text-center">Status</th></tr></thead>
            <tbody>
                <?php foreach ($children as $c):
                    $cCrit = $c['criticality'] ?? 'C';
                    $cBadge = ['A' => 'bg-danger', 'B' => 'bg-warning text-dark', 'C' => 'bg-secondary'][$cCrit] ?? 'bg-secondary';
                ?>
                    <tr>
                        <td><a href="<?= url('/Assets/show/' . (int) $c['asset_id']) ?>"><?= e($c['name']) ?></a> <?php if (!empty($c['code'])): ?><code class="small text-muted"><?= e($c['code']) ?></code><?php endif; ?></td>
                        <td class="small text-muted"><?= e($c['asset_type_name'] ?? '—') ?></td>
                        <td class="text-center"><span class="badge <?= $cBadge ?>"><?= e($cCrit) ?></span></td>
                        <td class="text-center"><span class="badge bg-secondary text-uppercase"><?= e($c['status']) ?></span></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>

<?php foreach ($sections as $sectionName => $sectionFields): ?>
    <div class="rs-card p-3 mb-3">
        <h6 class="fw-bold mb-2"><?= e($sectionName) ?></h6>
        <dl class="rs-def row g-2 mb-0">
            <?php foreach ($sectionFields as $f): $val = $meta[$f['field_key']] ?? null; ?>
                <div class="col-md-4"><dt><?= e($f['field_label']) ?></dt><dd>
                    <?php if (is_array($val)): ?><?= e(implode(', ', $val)) ?>
                    <?php elseif ($val === null || $val === ''): ?>—
                    <?php else: ?><?= e((string) $val) ?><?= $f['field_unit'] ? ' ' . e($f['field_unit']) : '' ?>
                    <?php endif; ?>
                </dd></div>
            <?php endforeach; ?>
        </dl>
    </div>
<?php endforeach; ?>

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