<?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');
		
	$datosMarca = "";
	if (isset($_GET['idMarca']) && $_GET['idMarca'] != ''){
		$query = "SELECT * FROM marcasvehiculos WHERE id = " . $_GET['idMarca'];
		$marca = $mysqli->query($query);
		$datosMarca = $marca->fetch_object();
	}
			
	if(isset($_POST['accion'])){
		$accion = $_POST['accion'];
		if ($accion == 'alta'){
			$query = "INSERT INTO marcasvehiculos (marca) VALUES ('" . $_POST['marca'] . "')";
			$mysqli->query($query);
		}
		else if ($accion = 'editar'){
			$query = "UPDATE marcasvehiculos SET marca = '" . $_POST['marca'] . "' WHERE id = '". $_POST['idMarcaEditar'] . "'" ;
			$mysqli->query($query);
		}
		$urlReferer = $_SERVER['HTTP_REFERER'];
		header("Location: " . $urlReferer);
		exit();
	}
if (isset($_GET['idMarca']) && $_GET['idMarca'] != ''){
?>
	<h3>Edición de Marcas de Aplicaciones:</h3>
<?php	
} else { ?>
	<h3>Nueva Marca de Aplicación:</h3>
<?php
} ?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> onsubmit="return validarDatos('marcasvehi')" method="POST" name="alta_marcas_vehi">
<div align="right">
    <a href="javascript:OcultarAltaEditar('marcasvehi')">
    	<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="idMarcaEditar" value="<?php echo $_GET['idMarca'] ?>">
    <?php
    if (isset($_GET['idMarca']) && $_GET['idMarca'] != ''){ ?>
		<input type="hidden" name="accion" value="editar">
    <?php	
	} else { ?>
    	<input type="hidden" name="accion" value="alta">
    <?php
	}?>  
    <p>&nbsp;</p>
    <?php
    if (isset($_GET['idMarca']) && $_GET['idMarca'] != ''){ ?>
    	<label style="clear:both">Marca</label>
    	<input name="marca" id="marca" type="text" class="combo_buscar" placeholder="Marca" value="<?php echo $datosMarca->marca ?>">
    <?php
	}
	else{ ?>
    	<label style="clear:both">Marca</label>
    	<input name="marca" id="marca" type="text" class="combo_buscar" placeholder="Marca">
	<?php
 	} ?>
</form>