<?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');
}
$Schema = new Schema($con, $database);
$MotherCar = new MotherCar($con);
$key = '';
$matricula = empty($_POST['matricula']) ? '' : Statics::SafeInput($con, $_POST['matricula']);
$marca = Statics::SafeInput($con, $_POST['marca']);
$modelo = Statics::SafeInput($con, $_POST['modelo']);
$bastidor = empty($_POST['bastidor']) ? '' : Statics::SafeInput($con, $_POST['bastidor']);
$carroceria = empty($_POST['carroceria']) ? 0 : Statics::SafeInput($con, $_POST['carroceria']);
$color = empty($_POST['color']) ? 0 : Statics::SafeInput($con, $_POST['color']);
$cambio = empty($_POST['cambio']) ? 0 : Statics::SafeInput($con, $_POST['cambio']);
$combustible = empty($_POST['combustible']) ? 0 : Statics::SafeInput($con, $_POST['combustible']);
$distintivo_ambiental = empty($_POST['distintivo_ambiental']) ? 0 : Statics::SafeInput($con, $_POST['distintivo_ambiental']);
$version = empty($_POST['version']) ? '' : Statics::SafeInput($con, $_POST['version']);
$motor_cc = empty($_POST['motor_cc']) ? '' : Statics::SafeInput($con, $_POST['motor_cc']);
$potencia = empty($_POST['potencia']) ? '' : Statics::SafeInput($con, $_POST['potencia']);
$fecha_matriculacion = empty($_POST['fecha_matriculacion']) ? '' : Statics::SafeInput($con, $_POST['fecha_matriculacion']);
$ano_fabricacion = empty($_POST['ano_matriculacion']) ? '' : Statics::SafeInput($con, $_POST['ano_matriculacion']);
$sqlCOLIT = "SELECT DESCRIPCION_SP AS DESCRIPCION_LG FROM VEHICULOS_LITERALES WHERE TIPO = ? AND CODIGO = ?";
$res = $Schema->executeQuery($sqlCOLIT, 'si', ['COL_PRI', $color]);
$color_literal = $res->fetch_array();

if(isset($_POST['btn_datos_principales_update'])) {
    $idcar = Statics::SafeInput($con, $_POST['idcar']);
    $MotherCar->set_codigo_vehiculo_tabla_vehiculos($_SESSION['VEHICULO_RECIEN_CREADO']);
    $MotherCar->set_id_car($idcar);
    $MotherCar->set_marca($marca);
    $MotherCar->set_modelo($modelo);
    $MotherCar->set_matricula($matricula);
    $MotherCar->set_bastidor($bastidor);
    $MotherCar->set_carroceria($carroceria);
    $MotherCar->set_color_principal($color);
    $MotherCar->set_color_principal_literal($color_literal['DESCRIPCION_LG']);
    $MotherCar->set_cajacambios($cambio);
    $MotherCar->set_distintivo_ambiental($distintivo_ambiental);
    $MotherCar->set_activo(1);
    $MotherCar->set_combustible($combustible);
    $MotherCar->set_estado('STK');
    $MotherCar->set_version($version);
    $MotherCar->set_motor_cc($motor_cc);
    $MotherCar->set_potencia($potencia);
    $MotherCar->set_matriculacion($fecha_matriculacion);
    $MotherCar->set_ano_fabricacion($ano_fabricacion);
    $MotherCar->set_mostrar($idcar . ' - ' . $MotherCar->nombre_vehiculo($marca, $modelo) . ' - ' . $matricula);
    $MotherCar->update_vehiculo();
    $_SESSION['MSG_INFO'] = 'DATOS_PRINCIPALES';
    $detalle = $actualizacion_lg . ': ' . $datos_principales_lg;
    Statics::add_trace_history($con, $usuarioSession, $_SESSION['VEHICULO_RECIEN_CREADO'], 2, $detalle);
    header("Location:nuevo-vehiculo-datos.php");

} else {

    $codigo_vehiculo = Statics::max_codigo($con, 'VEHICULOS');
    $idcar = Statics::get_contadores($con,'IDCAR0000000000000');
    $version = empty($_POST['version']) ? '' : Statics::SafeInput($con, $_POST['version']);
    $motor_cc = empty($_POST['motor_cc']) ? '' : Statics::SafeInput($con, $_POST['motor_cc']);
    $potencia = empty($_POST['potencia']) ? '' : Statics::SafeInput($con, $_POST['potencia']);
    $fecha_matriculacion = empty($_POST['fecha_matriculacion']) ? '' : Statics::SafeInput($con, $_POST['fecha_matriculacion']);
    $ano_fabricacion = empty($_POST['ano_matriculacion']) ? '' : Statics::SafeInput($con, $_POST['ano_matriculacion']);

    $MotherCar->set_codigo_vehiculo_tabla_vehiculos(Statics::max_codigo($con, 'VEHICULOS'));
    $MotherCar->set_usuario_session($usuarioSession);
    $MotherCar->set_id_car($idcar);
    $MotherCar->set_marca($marca);
    $MotherCar->set_modelo($modelo);
    $MotherCar->set_matricula($matricula);
    $MotherCar->set_bastidor($bastidor);
    $MotherCar->set_carroceria($carroceria);
    $MotherCar->set_color_principal($color);
    $MotherCar->set_color_principal_literal($color_literal['DESCRIPCION_LG']);
    $MotherCar->set_cajacambios($cambio);
    $MotherCar->set_distintivo_ambiental($distintivo_ambiental);
    $MotherCar->set_activo(1);
    $MotherCar->set_combustible($combustible);
    $MotherCar->set_estado('STK');
    $MotherCar->set_version($version);
    $MotherCar->set_motor_cc($motor_cc);
    $MotherCar->set_potencia($potencia);
    $MotherCar->set_matriculacion($fecha_matriculacion);
    $MotherCar->set_ano_fabricacion($ano_fabricacion);
    $mostrarMatricula = !empty($matricula) ? ' - ' . $matricula : '';
    $MotherCar->set_mostrar($idcar . ' - ' . $MotherCar->nombre_vehiculo($marca, $modelo) . $mostrarMatricula);
    $save = $MotherCar->insert_vehiculo();
    if($save) {
        $_SESSION['MSG_INFO'] = 'DATOS_OTROS';
        $_SESSION['MSG_INFO_2'] = 'DATOS_OTROS_CREADO';
        if (isset($_POST['btn_continuar'])) {
            $_SESSION['VEHICULO_RECIEN_CREADO'] = $codigo_vehiculo;
            header("Location:nuevo-vehiculo-datos.php");
        } else {
            header("Location:nuevo-vehiculo.php");
        }
    } else {
        $_SESSION['MSG_INFO'] = 'ERROR';
        header("Location:nuevo-vehiculo.php");
    }

        
    

     
    
}


?>