<?php
include "core/autoload.php";

include "core/app/model/ProductData.php";
include "core/app/model/PersonData.php";
include "core/app/model/AssetData.php";
include "core/app/model/StateData.php";

// Captura el término de búsqueda
$search = isset($_GET['search']) ? trim($_GET['search']) : '';

// Obtén todos los clientes
$clients =  AssetData::getAllBy('person_id', $_GET["id"]);

// Filtra los clientes según el término de búsqueda
$filteredClients = [];
if ($search !== '') {
    foreach ($clients as $c) {
        if (stripos($c->getProduct()->name, $search) !== false) { // Coincidencias en el nombre
            $filteredClients[] = $c;
        }
    }
} else {
    $filteredClients = $clients; // Si no hay búsqueda, muestra todos los clientes
}

// Genera el HTML de los resultados
if (count($filteredClients) > 0) {
    foreach ($filteredClients as $p): ?>
        <div class="col" data-item="list">
            <div class="col" data-item="list">
                <div class="card flex-row">
                    <div class="card-header rounded-0 rounded-start bg-light p-0"
                        style="width: 200px; height: 200px; background-color: #d3d3d3; display: flex; justify-content: center; align-items: center;">
                        <!-- Contenedor con padding para fla imagen -->
                        <div
                            style="padding: 4px; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center;">
                            <img src="assets/images/products/<?php echo $p->getProduct()->image; ?>" class="img-fluid"
                                style="object-fit: cover; max-width: 100%; max-height: 100%;" alt="wishlist-img"
                                loading="lazy">
                        </div>
                    </div>
                    <div class="card-body rounded-end">
                        <div class="d-flex justify-content-between mb-2">
                            <h6 class="mb-0"><?php echo $p->getProduct()->name; ?></h6>
                            <div class="d-flex align-items-center gap-2"></div>
                        </div>
                        <div class="d-flex justify-content-between mb-4">
                            <p class="mb-0">Código</p>
                            <p class="mb-0"><?php echo $p->getProduct()->code; ?></p>
                        </div>
                        <div class="d-flex justify-content-between">
                            <div class="d-flex gap-2">
                                <button type="button" class="btn btn-primary me-2"
                                    onclick="window.location.href='./?view=product&id=<?php echo $p->getProduct()->id; ?>';">
                                    Ver producto
                                </button>
                                <button type="button" class="btn btn-danger btn-sm" data-bs-toggle="modal"
                                    data-bs-target="#modaleliminarproducto">
                                    <i class="bi bi-trash-fill"></i>
                                    Eliminar
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
<?php endforeach;
} else {
    echo '<p>No se encontraron activos con el término "<strong>' . htmlspecialchars($search) . '</strong>".</p>';
}
?>