<?php 
	if(!isset($_SESSION))
		session_start();
	if (!isset($_SESSION['usuarioLogin']) || $_SESSION['usuarioLogin'] == ""){
		session_unset();
		session_destroy();
		header("Location: login.php");
		exit();
	}
?>
<?php
	require_once('../../Connections/catalogo.php');
	$marcas = $mysqli->query("SELECT * FROM marcasvehiculos ORDER BY marca");
	$segmentos = $mysqli->query("SELECT segmento FROM vehiculos GROUP BY segmento");
	$piezasCodigos = $mysqli->query("SELECT pc2.idPieza as idPieza, 
				IF(pc1.codigo IS NOT NULL, CONCAT(pc1.codigo, ', ', GROUP_CONCAT(pc2.codigo SEPARATOR ', ')), 
				GROUP_CONCAT(pc2.codigo SEPARATOR ', ')) as piezacodigo
			FROM piezascodigos pc2
			LEFT JOIN piezascodigos pc1 ON pc2.idPieza = pc1.idPieza AND pc1.idMarca = 7
			WHERE pc2.idMarca != 7
			GROUP BY idPieza;");
	
	if(!$marcas || !$segmentos)
		echo "Error: " . $mysqli->error;
		
	$datosVehiculo = "";
	if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){
		$query = "SELECT vehiculos.id as idVehiculo, idPieza, idMarcaVehiculo, marca, segmento, modelo, tipo, otros, motor, traccion, posicion, anioInicial, anioFinal, OEMS FROM vehiculos INNER JOIN marcasvehiculos ON vehiculos.idMarcaVehiculo = marcasvehiculos.id WHERE vehiculos.id = " . $_GET['idVehiculo'];
		$vehiculos = $mysqli->query($query);
		$datosVehiculo = $vehiculos->fetch_object();
	}
			
	if(isset($_POST['accion'])){
		$accion = $_POST['accion'];
		if ($accion == 'alta'){
			$query = "INSERT INTO vehiculos (idMarcaVehiculo, segmento, modelo, tipo, otros, idPieza, OEMS, anioInicial, anioFinal, motor, traccion, posicion) VALUES (" . $_POST['idMarca'] . ", '" . $_POST['segmento'] . "', '" . $_POST['modelo'] . "', '" . $_POST['tipo'] . "', '" . $_POST['otros'] . "', '" . $_POST['idPieza'] . "', '" . $_POST['oems'] . "', '" . $_POST['anio_inicio'] . "', '" . $_POST['anio_fin'] . "', '" . $_POST['motor'] . "', '" . $_POST['traccion'] . "', '" . $_POST['posicion'] . "')";
		}
		else if ($accion = 'editar'){
			$query = "UPDATE vehiculos SET idMarcaVehiculo = " . $_POST['idMarca'] . ", segmento = '" . $_POST['segmento'] . "', modelo = '" . $_POST['modelo'] . "', tipo = '" . $_POST['tipo'] . "', otros = '" . $_POST['otros'] . "', idPieza = '" . $_POST['idPieza'] . "', OEMS = '" . $_POST['oems'] . "', anioInicial = '" . $_POST['anio_inicio'] . "', anioFinal = '" . $_POST['anio_fin'] . "', motor = '" . $_POST['motor'] . "', traccion = '" . $_POST['traccion'] . "', posicion = '" . $_POST['posicion'] . "' WHERE id = " . $_POST['idVehiculoEditar'];
		}
		$mysqli->query($query);
		$urlReferer = $_SERVER['HTTP_REFERER'];
		header("Location: " . $urlReferer);
		exit();
	}
if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){
?>
	<h3>Edición de Aplicación:</h3>
<?php	
} else { ?>
	<h3>Nueva Aplicación:</h3>
<?php
} ?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> onsubmit="return validarDatos('vehiculos')" method="POST" name="alta_vehi">
<div align="right">
    <a href="javascript:OcultarAltaEditar('vehiculos')" style="font-family:Arial, Helvetica, sans-serif; font-size:1em; color:#000;margin-right:20px;"><img id="imageField" name="imageField" class="btn_buscar" type="image" src="imagenes/back.png" align="middle" width="53px" height="53px"></a>
     <input id="imageField" name="imageField" class="btn_buscar" type="image" src="imagenes/ok.png" align="middle" width="53px" height="53px">
</div>
	<input type="hidden" name="idVehiculoEditar" value="<?php echo $_GET['idVehiculo'] ?>">
    <?php
    if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){ ?>
		<input type="hidden" name="accion" value="editar">
    <?php	
	} else { ?>
    	<input type="hidden" name="accion" value="alta">
    <?php
	}?>
    <p>&nbsp;</p>
    <label style="clear:both">Marca</label>
    <select name="idMarca" id="idMarca" class="combo_buscar">
        <option value="0">Marca</option>
    <?php while ($renglon = $marcas->fetch_object()){ 
    	if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != '' && ($renglon->id == $datosVehiculo->idMarcaVehiculo)){ ?>
        	<option value="<?php echo $renglon->id ?>" selected="selected">
        <?php
		}
		else{ ?>
        	<option value="<?php echo $renglon->id ?>" >
        <?php }
             echo $renglon->marca ?>
        </option>
    <?php } ?>
    </select>
    <p>&nbsp;</p>
    <label style="clear:both">Segmento</label>
    <select name="segmento" id="segmento" class="combo_buscar">
        <option value="0">Segmento</option>
    <?php while ($renglon = $segmentos->fetch_object()){ 
		if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != '' && ($renglon->segmento == $datosVehiculo->segmento)){ ?>
	        <option value="<?php echo $renglon->segmento ?>" selected="selected">
        <?php
		}
		else{ ?>
        	<option value="<?php echo $renglon->segmento ?>" >
        <?php }
			echo $renglon->segmento ?>
        </option>
    <?php } ?>
    </select>  
    <p>&nbsp;</p>
    <label style="clear:both">Modelo</label>
    <?php
    if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){ ?>
    	<input name="modelo" id="modelo" type="text" class="combo_buscar" placeholder="Modelo" value="<?php echo $datosVehiculo->modelo ?>">
    <?php
	}
	else{ ?>
    	<input name="modelo" id="modelo" type="text" class="combo_buscar" placeholder="Modelo">
    <?php 
	}?>
    <p>&nbsp;</p>
    <label style="clear:both">Tipo</label>
    <?php
    if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){ ?>
   		<input name="tipo" id="tipo" type="text" class="combo_buscar" placeholder="Tipo" value="<?php echo $datosVehiculo->tipo ?>">
    <?php
	}
	else{ ?>
    	<input name="tipo" id="tipo" type="text" class="combo_buscar" placeholder="Tipo">
    <?php 
	}?>
    <p>&nbsp;</p>
    <label style="clear:both">Otros</label>
    <?php
    if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){ ?>
    	<input name="otros" id="otros" type="text" class="combo_buscar" placeholder="Otros" value="<?php echo $datosVehiculo->otros ?>">
    <?php
	}
	else{ ?>
    	<input name="otros" id="otros" type="text" class="combo_buscar" placeholder="Otros">
    <?php 
	}?>
    <p>&nbsp;</p>
    <label style="clear:both">Pieza Relacionada</label>
    <select name="idPieza" id="idPieza" class="combo_buscar">
        <option value="0">Pieza Relacionada</option>
    <?php while ($renglon = $piezasCodigos->fetch_object()){ 
        if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != '' && ($renglon->idPieza == $datosVehiculo->idPieza)){ ?>
            <option value="<?php echo $renglon->idPieza ?>" selected="selected">
        <?php
        }
        else{ ?>
            <option value="<?php echo $renglon->idPieza ?>" >
        <?php }
             echo $renglon->piezacodigo ?>
        </option>
    <?php } ?>
    </select>
    <p>&nbsp;</p>
    <label style="clear:both">OEMs Nº (separar con ;)</label>
    <?php
    if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){ ?>
    	<input name="oems" id="oems" type="text" class="combo_buscar" placeholder="OEMs Nº (separar con ;)" value="<?php echo $datosVehiculo->OEMS ?>">
    <?php
	}
	else{ ?>
    	<input name="oems" id="oems" type="text" class="combo_buscar" placeholder="OEMs Nº (separar con ;)">
    <?php 
	}?>
    <p>&nbsp;</p>
    <label style="clear:both">Año Inicio</label>
    <?php
    if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){ ?>
    	<input name="anio_inicio" id="anio_inicio" type="text" class="combo_buscar" placeholder="Año Inicio" value="<?php echo $datosVehiculo->anioInicial ?>">
    <?php
	}
	else{ ?>
    	<input name="anio_inicio" id="anio_inicio" type="text" class="combo_buscar" placeholder="Año Inicio">
    <?php 
	}?>
    <p>&nbsp;</p>
    <label style="clear:both">Año Fin</label>
    <?php
    if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){ ?>
    	<input name="anio_fin" id="anio_fin" type="text" class="combo_buscar" placeholder="Año Fin" value="<?php echo $datosVehiculo->anioFinal ?>">
    <?php
	}
	else{ ?>
    	<input name="anio_fin" id="anio_fin" type="text" class="combo_buscar" placeholder="Año Fin" >
    <?php 
	}?>
    <p>&nbsp;</p>
    <label style="clear:both">Motor</label>
    <?php
    if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){ ?>
    	<input name="motor" id="motor" type="text" class="combo_buscar" placeholder="Motor" value="<?php echo $datosVehiculo->motor ?>">
    <?php
	}
	else{ ?>
    	<input name="motor" id="motor" type="text" class="combo_buscar" placeholder="Motor">
    <?php 
	}?>
    <p>&nbsp;</p>
    <label style="clear:both">Tracción</label>
    <?php
    if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){ ?>
    	<input name="traccion" id="traccion" type="text" class="combo_buscar" placeholder="Tracción" value="<?php echo $datosVehiculo->traccion ?>">
    <?php
	}
	else{ ?>
    	<input name="traccion" id="traccion" type="text" class="combo_buscar" placeholder="Tracción">
    <?php 
	}?>
    <p>&nbsp;</p>
    <label style="clear:both">Posición</label>
    <?php
    if (isset($_GET['idVehiculo']) && $_GET['idVehiculo'] != ''){ ?>
    	<input name="posicion" id="posicion" type="text" class="combo_buscar" placeholder="Posición" value="<?php echo $datosVehiculo->posicion ?>">
    <?php
	}
	else{ ?>
    	<input name="posicion" id="posicion" type="text" class="combo_buscar" placeholder="Posición">
    <?php 
	/*
	  <a style="color:#333" href='javascript:MostrarAltaEditar("vehiculos_posicion", <?php echo $row->idVehiculo ?>)'><img src="imagenes/editar.png" width="62" height="62" /> Imagenes</a>
	*/
	
	}?>
    
    
</form>