<?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('../includes/functions.php');
include('../includes/esquemas.php');
include('../class/Schema.php');
include('../class/MotherCar.php');
include('../class/Static.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');
}
$codigo = $_SESSION['CODIGO_COCHE'];
$hoy = date('Y-m-d');
$telefono = Statics::SafeInput($con,$_POST['telefono']);
$fecha = Statics::SafeInput($con,$_POST['fecha']);
if($fecha < $hoy)
{
    header("Location:../resultados-ficha.php?idCAR=$codigo");
    $_SESSION['MSG'] = 'FECHA_INFERIOR';
}
else
{
    $sqlMain = "SELECT EMAIL1, CODIGO FROM CONTACTOS WHERE TLFNO1 = ?";
    $stmt = $con->prepare($sqlMain);
    $stmt->bind_param('s',$telefono);
    $stmt->execute();
    $resMain = $stmt->get_result();
    if($resMain->num_rows)
    {
        $filaMain = $resMain->fetch_array();
        $sql = "UPDATE VEHICULOS SET RESERVA = ? WHERE CODIGO = ?";
        $stmt = $con->prepare($sql);
        $stmt->bind_param('si',$fecha,$codigo);
        $stmt->execute();

        $_SESSION['MSG'] = 'RESERVADO';
        Statics::add_trace_history($con, $usuarioSession, $_SESSION['CODIGO_COCHE'], 8, $filaMain['EMAIL1'], NULL, $filaMain['CODIGO']);
        header("Location:../resultados-ficha.php?idCAR=$codigo");
    }
    else
    {
        $_SESSION['MSG'] = 'NO_DNI';
        header("Location:../resultados-ficha.php?idCAR=$codigo");
    }
}


?>