<?php
session_start();
$usuarioSession = $_SESSION['USUARIO'];
$grupo = $_SESSION['GRUPO'];
$contacto = $_SESSION['CONTACTO'];
$awa_session_id = $_SESSION['AWA_SESION_ID'];
include('../includes/conex.php');
include('../includes/eat.php');
include('../includes/historial.php');
include('../class/Static.php');
include('../class/Schema.php');
include('../class/PuzzleHTML.php');
if(!isset($usuarioSession) or $awa_session_id != $awa_id){
    eatCookies();
    header('Location:../index.php');
}
if(isset($_COOKIE['usuario']) AND $_COOKIE['usuario'] != $staticUsu ){
    eatCookies();
    header('Loction:../index.php');
}
if(isset($_COOKIE['idioma'])){
    include('../lg/LG_' . $_COOKIE['idioma'] . '.php');
}else{
    include('../lg/LG_SP.php');
}
$html = '';
$Schema = new Schema($con);
if(isset($_POST['word'])) {
    if(!empty($_POST['word'])) {
        $word = Statics::SafeInput($con, $_POST['word']);
        $sql = "SELECT * FROM CONTACTOS WHERE NOMBRE LIKE '$word%' OR APELLIDO1 LIKE '$word%' OR TLFNO1 LIKE '$word%' OR EMAIL1 LIKE '$word%' OR MOSTRAR LIKE '$word%' OR NIF LIKE '$word%'";
        $res = $con->query($sql);
        if($res->num_rows) {
            while($row = $res->fetch_array()) {
                $html .= '<a class="dropdown-item listado-contactos" aria-label="' . $row['CODIGO'] . '" href="#">' . mb_strtoupper($row['MOSTRAR'], 'UTF-8') . '</a>';
            }
        } else {
            $html .= '<a class="dropdown-item" href="#">' . strtoupper($sin_resultados_lg) . '</a>';
        }
        $html .= '<div class="dropdown-divider"></div>';
        $html .= '<a class="dropdown-item listado-contactos" aria-label="0" ><i class="fas fa-user-plus mr-2"></i>' . $crear_contacto_lg . '</a>';
    }
}
if(isset($_POST['contacto'])) {
    $contacto = Statics::SafeInput($con, $_POST['contacto']);
    $res = $Schema->executeQuery('SELECT NOMBRE, APELLIDO1, APELLIDO2, EMAIL1, TLFNO1, NIF FROM CONTACTOS WHERE CODIGO = ?', 'i', [$contacto]);
    $row = $res->fetch_array();
    $data = [
        "nombre"                =>      $row['NOMBRE'],
        'ape1'                  =>      $row['APELLIDO1'],
        'ape2'                  =>      $row['APELLIDO2'],
        'email'                 =>      $row['EMAIL1'],
        'telefono'              =>      $row['TLFNO1'],
        'nif'                   =>      $row['NIF'],
        'codigo_contacto'       =>      $contacto
    ];
    $html .= json_encode($data);
}

echo $html;

?>
