<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>Assets</h5>
    <div class="d-flex gap-2">
        <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-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; ?>

<form method="get" action="<?= url('/Assets') ?>" 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 name / serial / tag…" value="<?= e($filters['q'] ?? '') ?>"></div>
        <div class="col-md-3">
            <select name="site_id" class="form-select form-select-sm">
                <option value="">All sites</option>
                <?php foreach ($sites as $s): ?>
                    <option value="<?= (int) $s['site_id'] ?>" <?= (int) ($filters['site_id'] ?? 0) === (int) $s['site_id'] ? 'selected' : '' ?>><?= e($s['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-3">
            <select name="asset_type_id" class="form-select form-select-sm">
                <option value="">All types</option>
                <?php foreach ($types as $t): ?>
                    <option value="<?= (int) $t['asset_type_id'] ?>" <?= (int) ($filters['asset_type_id'] ?? 0) === (int) $t['asset_type_id'] ? 'selected' : '' ?>><?= e($t['name']) ?></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 (['active','inactive','scrapped','in_transit'] as $s): ?>
                    <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>><?= $s ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-12"><button type="submit" class="btn btn-sm btn-outline-primary"><i class="fas fa-filter me-1"></i>Filter</button></div>
    </div>
</form>

<div class="rs-card p-3">
<?php if (empty($assets)): ?>
    <p class="text-muted small mb-0">No assets found.</p>
<?php else: ?>
    <table class="table table-sm table-hover mb-0">
        <thead><tr><th>Name</th><th>Type</th><th>Site</th><th>Serial</th><th>Tag</th><th>Status</th><th></th></tr></thead>
        <tbody>
            <?php foreach ($assets as $a): ?>
                <tr>
                    <td><span class="rs-swatch" style="background: <?= e($a['color'] ?? '#0f172a') ?>"></span><a href="<?= url('/Assets/show/' . (int) $a['asset_id']) ?>" class="fw-semibold text-decoration-none"><?= e($a['name']) ?></a></td>
                    <td class="small text-muted"><?= e($a['asset_type_name'] ?? '') ?></td>
                    <td class="small text-muted"><?= e($a['site_name'] ?? '—') ?></td>
                    <td class="small"><?= e($a['serial_number'] ?? '—') ?></td>
                    <td class="small"><?= e($a['asset_tag'] ?? '—') ?></td>
                    <td><span class="badge bg-secondary text-uppercase"><?= e($a['status']) ?></span></td>
                    <td class="text-end"><a href="<?= url('/Assets/edit/' . (int) $a['asset_id']) ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-pen"></i></a></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>
</div>
