<?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/functions.php');
include('../includes/historial.php');

$cookieUser = filter_input(INPUT_COOKIE, 'usuario') ?? null; 
$cookieIdioma = filter_input(INPUT_COOKIE, 'idioma') ?? null; 
if((!isset($usuarioSession) or $awa_session_id != $awa_id) or (isset($cookieUser) AND $cookieUser != $staticUsu )){
    eatCookies();
    header('Location:../index.php');}
empty($cookieIdioma)?
    include('../lg/LG_' . $cookieIdioma . '.php'):
    include('../lg/LG_SP.php');

$codigo = $_SESSION['CODIGO_COCHE'];
echo '

    <table class="table table-sm table-hover" id="historialCompletoTable">
        <thead class="thead-light">
            <tr class="text-center">
                <th scope="col">#</th>
                <th scope="col">' . $usuario_lg .'</th>
                <th scope="col">' . $fecha_lg .'</th>
                <th scope="col">' . $accion_lg . '</th>
                <th scope="col">' . $ordenador_lg . '</th>
                <th scope="col">' . $detalles_lg . '</th>
            </tr>
        </thead>
        <tbody>';
        $nr = 1;
        $sqlHistorial = "SELECT VEHICULOS_HISTORIAL.*,VEHICULOS_LITERALES.*, DESCRIPCION_$lg AS DESCRIPCION_LG
                FROM VEHICULOS_HISTORIAL,VEHICULOS_LITERALES
                WHERE VEHICULOS_LITERALES.CODIGO = VEHICULOS_HISTORIAL.ACCION
                AND VEHICULOS_LITERALES.TIPO = 'TIP_TRA'
                AND VEHICULOS_HISTORIAL.VEHICULO = ?
                ORDER BY VEHICULOS_HISTORIAL.FECHA DESC";
        $stmt = $con->prepare($sqlHistorial);
        $stmt->bind_param('i',$codigo);
        $stmt->execute();
        $res = $stmt->get_result();
        while($filaHistorial = $res->fetch_array()){
            echo '
            <tr class="text-center">
                <th scope="row">'. $nr++ .'</th>
                <td>' . ucwords($filaHistorial['USUARIO']) . '</td>
                <td>' . date('H:i:s d/m/Y',strtotime($filaHistorial['FECHA'])) . '</td>
                <td>' . $filaHistorial['DESCRIPCION_LG'] . '</td>
                <td>' . $filaHistorial['ORDENADOR'] . '</td>
                <td>';
                if($filaHistorial['DETALLE'] == ""){
                    echo " <span class='font-weight-lighter font-italic'>$gestion_interna_lg</span>";
                }else{
                    echo '<p class="font-weight-normal text-info">' . $filaHistorial['DETALLE'] . '</p>';
                }
            echo  '</td>
            </tr>';
        }
echo '</tbody></table>';