<?php
include "core/autoload.php";

include "core/app/model/ProductData.php";
include "core/app/model/PersonData.php";
include "core/app/model/TypeData.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 =  ProductData::getAllBy('brand_id', $_GET["id"]);

// Filtra los clientes según el término de búsqueda
// Filtra los clientes según el término de búsqueda
$filteredClients = [];
if ($search !== '') {
    foreach ($clients as $c) {
        if (stripos($c->name, $search) !== false || stripos($c->code, $search) !== false) {
            $filteredClients[] = $c;
        }
    }
} else {
    $filteredClients = $clients;
}


// Genera el HTML de los resultados
if (count($filteredClients) > 0) {
    foreach ($filteredClients as $p): ?>
        <div class="col">
            <div class="card position-relative h-100" style="border: none; box-shadow: 0 2px 8px rgba(0,0,0,0.05);">
                <!-- Product Image -->
                <div class="position-absolute start-50 translate-middle-x" style="top: -35px; z-index: 1;">
                    <div class="d-flex align-items-center justify-content-center"
                        style="width: clamp(120px, 14vw, 140px); 
                              height: clamp(120px, 14vw, 140px); 
                              border: 4px solid #e9ecef;
                              border-radius: 15px;
                              box-shadow: 0 4px 12px rgba(0,0,0,0.08);
                              background: #ffffff;">
                        <div class="d-flex align-items-center justify-content-center w-100 h-100 p-3">
                            <img src="assets/images/products/<?php echo $p->image; ?>"
                                class="img-fluid"
                                style="width: 100%; height: 100%; object-fit: contain; transition: transform 0.2s;"
                                alt="<?php echo $p->name; ?>"
                                loading="lazy">
                        </div>
                    </div>
                </div>
                <hr>
                <div class="card-body" style="padding-top: 95px;">
                    <!-- Title Section -->
                    <div class="mb-4 text-center">
                        <h6 class="text-primary mb-2" style="font-size: 1.25rem; font-weight: 600;"><?php echo $p->name; ?></h6>
                        <p class="text-muted mb-0" style="font-size: 0.95rem;">Código: <span class="fw-medium"><?php echo $p->code; ?></span></p>
                    </div>

                    <!-- Footer Section -->
                    <div class="mt-4 pt-3 border-top">
                        <div class="d-flex gap-3 justify-content-center align-items-center">
                            <button type="button" class="btn btn-sm btn-info rounded-pill px-4 py-1"
                                onclick="window.location.href='./?view=product&id=<?php echo $p->id; ?>';"
                                style="transition: all 0.2s;">
                                <i class="bi bi-eye-fill me-1"></i>
                                Ver
                            </button>
                            <button type="button" class="btn btn-sm btn-warning rounded-pill px-4 py-1"
                                data-bs-toggle="modal"
                                data-bs-target="#modaleliminarproducto"
                                style="transition: all 0.2s;">
                                <i class="bi bi-trash-fill me-1"></i>
                                Eliminar
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
<?php endforeach;
} else {
    echo '<p>No se encontraron oportunidades con el término "<strong>' . htmlspecialchars($search) . '</strong>".</p>';
}
?>