<?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');
		
	$datosPalabra = "";
	if (isset($_GET['idPalabra']) && $_GET['idPalabra'] != ''){
		$query = "SELECT * FROM idiomas WHERE id = " . $_GET['idPalabra'];
		$palabra = $mysqli->query($query);
		$datosPalabra = $palabra->fetch_object();
	}
			
	if(isset($_POST['accion'])){
		$accion = $_POST['accion'];
		$idiomas = $mysqli->query("SELECT DISTINCT idioma FROM idiomas");
		if ($accion == 'alta'){
			while ($rowIdiomas = $idiomas->fetch_object()){
				$query = "INSERT INTO idiomas (palabra, idioma, traduccion) VALUES ('" . $_POST['palabra'] . "', '" . $rowIdiomas->idioma ."', '" . $_POST[$rowIdiomas->idioma] . "')";
				$mysqli->query($query);
			}
		}
		else if ($accion = 'editar'){
			while ($rowIdiomas = $idiomas->fetch_object()){
				$query = "UPDATE idiomas SET traduccion = '" . $_POST[$rowIdiomas->idioma] . "' WHERE idioma = '". $rowIdiomas->idioma . "' AND palabra = '" . $_POST['palabra'] . "'" ;
				$mysqli->query($query);
			}
		}
		$urlReferer = $_SERVER['HTTP_REFERER'];
		header("Location: " . $urlReferer);
		exit();
	}
if (isset($_GET['idPalabra']) && $_GET['idPalabra'] != ''){
?>
	<h3>Edición de Idiomas:</h3>
<?php	
} else { ?>
	<h3>Nueva Palabra:</h3>
<?php
} ?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> onsubmit="return validarDatos('idiomas')" method="POST" name="alta_idio">
<div align="right">
    <a href="javascript:OcultarAltaEditar('idiomas')">
    	<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="idPalabraEditar" value="<?php echo $_GET['idPalabra'] ?>">
    <?php
    if (isset($_GET['idPalabra']) && $_GET['idPalabra'] != ''){ ?>
		<input type="hidden" name="accion" value="editar">
    <?php	
	} else { ?>
    	<input type="hidden" name="accion" value="alta">
    <?php
	}?>  
    <p>&nbsp;</p>
    <?php
    if (isset($_GET['idPalabra']) && $_GET['idPalabra'] != ''){ ?>
    	<label style="clear:both">Palabra</label>
    	<input name="palabra" id="palabra" type="text" class="combo_buscar" placeholder="Palabra" value="<?php echo $datosPalabra->palabra ?>" readonly="readonly">
        <?php
		$idiomas = $mysqli->query("SELECT DISTINCT idioma FROM idiomas");
		while ($rowIdiomas = $idiomas->fetch_object()){ 
			$traduccion = $mysqli->query("SELECT traduccion FROM idiomas WHERE idioma = '" . $rowIdiomas->idioma . "' AND palabra = '" . $datosPalabra->palabra . "'");
			$palabraTrad = $traduccion->fetch_object(); 
			?>
			<p>&nbsp;</p>
			<label style="clear:both"><?php echo $rowIdiomas->idioma ?></label>
			<input name="<?php echo $rowIdiomas->idioma ?>" id="<?php echo $rowIdiomas->idioma ?>" type="text" class="combo_buscar" placeholder="<?php echo $rowIdiomas->idioma ?>" value="<?php echo $palabraTrad->traduccion ?>">
		<?php
		} 
	}
	else{?>
    	<label style="clear:both">Palabra</label>
    	<input name="palabra" id="palabra" type="text" class="combo_buscar" placeholder="Palabra">
        <?php
		$idiomas = $mysqli->query("SELECT DISTINCT idioma FROM idiomas");
		while ($rowIdiomas = $idiomas->fetch_object()){ ?>
			<p>&nbsp;</p>
			<label style="clear:both"><?php echo $rowIdiomas->idioma ?></label>
			<input name="<?php echo $rowIdiomas->idioma ?>" id="<?php echo $rowIdiomas->idioma ?>" type="text" class="combo_buscar" placeholder="<?php echo $rowIdiomas->idioma ?>">
		<?php
		} 
 	} ?>
</form>