<?php
	// Sólo permitimos el acceso desde unos pocos números IP
	
	$ip_admin = array(
		'127.0.0.1',
		'200.16.17.235'
	);
	
	if ( !in_array($_SERVER['REMOTE_ADDR'], $ip_admin) ) {
		echo '<h3>No está autorizado a ingresar a esta página</h3>';
		exit();
	}

?>

<h3>Acreditación</h3>


<form action="inscruma.php" method="post" style="margin-bottom: 2em; /*margin-top: -0.5em;*/">
	<p>
		Búsqueda por apellido:
		<input type="text" name="apellido" id="input-apellido">
	</p>
	<p style="font-style: italic;">
		Sólo apellido, sin nombres. No necesita escribir apellido completo, alcanza con algunas letras
	</p>
	<input type="submit" value="Buscar">
</form>

<div id="admin">

<?php
	function clean($input) {
		$input = preg_replace("/ +$|^ +/","",$input);
		$input = preg_replace("/ +/"," ",$input);
		$input = preg_replace("/'/","\\'",$input);
		return ($input);
	}
	
	// nos conectamos a la base de datos
	include('inmabb/inmabb_conexion.inc');
	
	
	// =============================== RESULTADOS DE BUSQUEDA =====================================	
	
	if ( isset($_POST['apellido']) ) {
		$apellido = clean($_POST['apellido']);
		$query = "SELECT apellido, nombre, institucion, id, fecha_preinscripcion, fecha_registro FROM personas WHERE apellido LIKE '%" . $apellido . "%' order by apellido, nombre;";
		echo '<p>Búsqueda: <b>' . $apellido . '</b></p>';
		$result = mysql_query($query);
		if (mysql_num_rows($result) > 0) {
?>
			<table cellspacing="0" id="tabla-inscriptos" style="background: #FFC;">
			<tr>
				<th>Nombre</th>
				<th>Institución</th>
				<th>Preinscripto</th>
				<th>Registrado</th>
			</tr>
<?php
		   while ($row = mysql_fetch_row($result)) {
				echo '<tr>';
				echo '<td style="font-weight: bold; /*white-space: nowrap;*/">';
				echo '<a href="inscriptouma.php?id=' . $row[3] . '" title="Entrar a la ficha personal">' . $row[0] . ', ' . $row[1] . '</a>';
				echo '</td>';
				echo '<td>' . $row[2] . '</td>';
				$fecha_preinscripcion = ( $row[4] != '' ) ? substr($row[4],8,2) . '/' . substr($row[4],5,2) . ',&nbsp;' . substr($row[4],11,5) : '' ;
				$fecha_registro = ( $row[5] != '' ) ? substr($row[5],8,2) . '/' . substr($row[5],5,2) . ',&nbsp;' . substr($row[5],11,5) : '' ;
				echo '<td>' . $fecha_preinscripcion . '</td>';
				echo '<td>' . $fecha_registro . '</td>';
				echo '</tr>';
				echo "\n";
		   }
?>
			</table>
<?php
		} else {   // mysql_num_rows($result) = 0
?>
			<p>No se encuentran coincidencias.</p>
			<p class="pregunta">¿Se preinscribió vía web?</p>
<?php
		}
		
	// =================================== GRABACION ===================================	

	} else if ( isset($_POST['grabar']) ) {
	
		// esto para averiguar si fecha_registro no está cargada
		$query = 'SELECT * FROM personas WHERE id = ' . $_POST['id'] . ' AND fecha_registro is NULL';
		$result = mysql_query($query);
		
		$query = "UPDATE personas SET observaciones = '" . clean($_POST['observaciones']) . "'";
		if ( mysql_num_rows($result) == 1 ) {
			$query .= ', fecha_registro = NOW()';  // fecha_registro no está cargada
		}
		if ( $_POST['monto'] != '') {
			$query .= ', monto_abonado = ' . $_POST['monto'];
		}
		$query .= ' WHERE id = ' . $_POST['id'] ;
		//echo $query;
		$result = mysql_query($query);
		
		if (!$result) {
			echo '<p style="font-weight: bold;">¡ERROR AL GRABAR DATOS!</p>';
		} else {
			echo '<p style="font-weight: bold;">Los datos fueron grabados.</p>';
		}
	
	// ============================== FICHA PERSONAL ==================================	
	
	} else if ( !empty($_GET['id']) ) {
	
		$id = $_GET['id'];
?>
		<form action="uma.php" method="post" id="ficha-personal">
		<input type="hidden" name="p" value="admin">
		<input type="hidden" name="id" value="<?= $id ?>">
		<input type="hidden" name="monto" value="">
<?php		
		$query = 'SELECT apellido, nombre, institucion, monto_abonado, fecha_registro, observaciones, es_socio_UMA, tiene_cuota_al_dia, tiene_cuota_anterior, estudiante_carrera, es_local, beca_otorgada, certificado_impreso FROM personas WHERE id = ' . $id;
		$result = mysql_query($query);
		$row = mysql_fetch_row($result);
		$apellido = $row[0];
		$nombre = $row[1];
		$institucion = $row[2];
		$monto_abonado = $row[3];
		$fecha_registro = $row[4];
		$observaciones = $row[5];
		$es_socio_UMA = $row[6];
		$tiene_cuota_al_dia = $row[7];
		$tiene_cuota_anterior = $row[8];
		$estudiante_carrera = $row[9];
		$es_local = $row[10];
		$beca_otorgada = $row[11];
		$certificado_impreso = $row[12];
	}
?>
