<?php
header('Content-Type: text/html; charset=UTF-8');
mb_internal_encoding('UTF-8');
include "core/autoload.php";

include "core/app/model/ContactData.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 = ContactData::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->name, $search) !== false) {
            $filteredClients[] = $c;
        }
    }
} else {
    $filteredClients = $clients;
}

// Genera el HTML de los resultados
if (count($filteredClients) > 0) {
    $output = '';
    foreach ($filteredClients as $c) {
        $output .= '<div  class="col" data-item="list">
               <div class="card position-relative">
                  <!-- Left indicator -->
                  <div class="position-absolute top-0 start-0 h-100" style="width: 6px;">
                     <div class="bg-info h-100"></div>
                  </div>

                  <!-- Logo circle -->
                  <div class="position-absolute top-0 end-0 me-2" style="margin-top: -15px; z-index: 1;">
                     <div class="rounded-circle bg-white d-flex align-items-center justify-content-center" style="width: 60px; height: 60px; border: 2px solid white; box-shadow: 0 2px 4px rgba(0,0,0,0.1); background: linear-gradient(145deg, #ffffff, #f5f5f5);">
                        <div class="rounded-circle bg-primary bg-opacity-10 d-flex align-items-center justify-content-center w-100 h-100 p-1">
                           <img src="assets\icons\user-w.svg"
                              class="img-fluid"
                              style="width: 70%; height: 70%; object-fit: contain;"
                              loading="lazy"
                              data-bs-toggle="popover"
                              data-bs-trigger="hover"
                              data-bs-placement="left"
                              data-bs-html="true"
                              data-bs-content="
                                <div class=\'p-2\' style=\'min-width: 220px;\'>
                                    <div class=\'d-flex align-items-center mb-2\'>
                                        <div class=\'bg-primary-subtle rounded-circle p-2 me-2\'>
                                            <i class=\'bi bi-person-fill text-primary fs-5\'></i>
                                        </div>
                                        <div>
                                            <h6 class=\'mb-0 text-primary\'>' . html_entity_decode(htmlspecialchars($c->name . ' ' . $c->lastname, ENT_QUOTES, 'UTF-8')) . '</h6>
                                            <small class=\'text-muted\'>Gerente de Ventas</small>
                                        </div>
                                    </div>
                                    <div class=\'d-flex flex-column gap-1\'>
                                           <div class=\'d-flex align-items-center\'>
                                            <i class=\'bi bi-envelope text-primary me-2\'></i>
                                            <small>' . htmlspecialchars($c->email) . '</small>
                                        </div>
                                       <div class=\'d-flex align-items-center\'>
                                            <i class=\'bi bi-telephone-fill text-primary me-2\'></i>
                                            <small>' . htmlspecialchars($c->phone) . '</small>
                                        </div>
                                        <div class=\'d-flex align-items-center\'>
                                            <i class=\'bi bi-building text-primary me-2\'></i>
                                            <small>' . htmlspecialchars($c->phone_i) . '</small>
                                        </div>
                                        <div class=\'d-flex align-items-center\'>
                                            <i class=\'bi bi-phone-fill text-primary me-2\'></i>
                                            <small>' . htmlspecialchars($c->phone_c) . '</small>
                                        </div>
                                        <div class=\'d-flex align-items-center\'>
                                            <i class=\'bi bi-person-badge text-primary me-2\'></i>
                                            <small>' . htmlspecialchars($c->phone_p) . '</small>
                                        </div>
                                    </div>
                                </div>">
                           </span>
                        </div>
                     </div>
                     </a>
                  </div>
                  <div class="card-body p-3">
                     <!-- Header -->
                     <div class="mb-2" style="margin-top: 20px">
                        <h6 class="text-primary mb-0" style="font-size: 0.9rem;">' . html_entity_decode(htmlspecialchars($c->name . ' ' . $c->lastname, ENT_QUOTES, 'UTF-8')) . '</h6>
                        <!-- <small class="text-dark d-block">Axcel Fernando Flores Iriarte
                        </small> -->
                        <small class="text-muted" style="font-size: 0.7rem;">15/01/2024 14:30</small>
                     </div>
                     <div class="d-flex justify-content-between align-items-center mb-1">
                        <span class="text-muted small">Área o departamento:</span>
                        <span class="text-dark small">' . html_entity_decode(htmlspecialchars($c->department), ENT_QUOTES, 'UTF-8') . '</span>
                     </div>

                     <!-- Footer -->
                     <div class="d-flex gap-1 justify-content-center">
                        <button class="btn btn-sm btn-icon btn-warning-subtle text-warning rounded-circle p-1"
                           title="Editar"
                           data-bs-toggle="tooltip">
                           <i class="bi bi-pencil-square fs-6" data-bs-toggle="modal" data-bs-target="#modaleditarcontacto"></i>
                        </button>
                        <button class="btn btn-sm btn-icon btn-danger-subtle text-danger rounded-circle p-1"
                           title="Eliminar"
                           data-bs-toggle="tooltip">
                           <i class="bi bi-trash3-fill fs-6" data-bs-toggle="modal" data-bs-target="#modaleliminarcontacto"></i>
                        </button>
                     </div>
                  </div>
                  </div>
               </div>';
    }
    echo $output;
} else {
    echo '<p>No se encontraron contactos con el término "<strong>' . htmlspecialchars($search) . '</strong>".</p>';
}
