<?php
include_once ("../core/config.php");
include_once ("funzioni.php");
$UTENTE = login();
$DIRITTI = diritti('Utente');
view_top();

if (isset($_POST['id'])) {
	$post = addslashes_array ($_POST);
	extract($post);

	$certificato = sistemadata($certificato);
	$nascita_data = sistemadata($nascita_data);

	if (is_null ($tcintura) || $tcintura == "") {
		$tcintura = 0;
	}

	if ($id) {
		$query = "	UPDATE
					wf_iscritto
				SET
					nome = '$nome',
					cognome = '$cognome',
					id_cintura = $id_cintura,
					tcintura = $tcintura,
					id_palestra = $id_palestra,
					certificato = STR_TO_DATE('$certificato', '%d.%m.%Y'),
					sesso = '$sesso',
					nascita_data = STR_TO_DATE('$nascita_data', '%d.%m.%Y'),
					commento = '$commento'
				WHERE
					id = $id	
		";
	} else {
		$query = "	INSERT INTO
					wf_iscritto
				SET
					attivo = 1,
					nome = '$nome',
					cognome = '$cognome',
					id_cintura = $id_cintura,
					tcintura = $tcintura,
					id_palestra = $id_palestra,
					certificato = STR_TO_DATE('$certificato', '%d.%m.%Y'),
					sesso = '$sesso',
					nascita_data = STR_TO_DATE('$nascita_data', '%d.%m.%Y'),
					commento = '$commento'
		";
	}

	$res = mysql_query( $query, $DB_ID );
	if ($id == "") {
		$id = mysql_insert_id();
	}
?>
		<script type="text/javascript">
			document.location.href='iscritti_lista.php?id=<?php print $id ?>'
		</script>
<?php
		exit;
} elseif (isset($_GET['id'])) {
	$id = $_GET['id'];
}
?>

<form name="" method="post">
<input type="hidden" name="id" value="<?php print $id ?>">
<?php

$query = "	SELECT 
			id,
			attivo,
			nome,
			cognome,
			id_cintura,
			tcintura,
			id_palestra,
			DATE_FORMAT(certificato, '%d.%m.%Y') AS certificato,
			sesso,
			DATE_FORMAT(nascita_data, '%d.%m.%Y') AS nascita_data,
			commento
		FROM
			wf_iscritto
		WHERE
			id = $id
";
$res = mysql_query( $query, $DB_ID );
$dato = mysql_fetch_array ( $res );

(livello_modulo($DIRITTI) == "Admin") ? $query_admin = "" : $query_admin = "
		JOIN
			wf_utenti
			ON
				wf_palestra.id = wf_utenti.id_palestra
				AND
				wf_utenti.id_utenti = ".$UTENTE['id'];

$query = "	SELECT
			wf_palestra.id,
			wf_palestra.palestra
		FROM
			wf_palestra
		$query_admin
		WHERE	
			wf_palestra.attivo = 1
		ORDER BY
			palestra
";
$res_palestra = mysql_query( $query, $DB_ID );

$query = "	SELECT
			id,
			cintura
		FROM
			wf_cinture
		ORDER BY
			kup desc,
			dan
";
$res_cintura = mysql_query( $query, $DB_ID );

$query = "	SELECT 
			DATE_FORMAT(data, '%d.%m.%Y') AS data,
			peso,
			altezza,
			cooper,
			sargeant,
			slungo,
			pallaav,
			pallaind,
			mobart,
			flesstro,
			cingolo,
			calcisx,
			calcidx,
			commenti
		FROM
			wf_storico
		WHERE
			wf_storico.id_iscritto = $id
		ORDER BY
			wf_storico.data DESC
		LIMIT 1
";
$res = mysql_query( $query, $DB_ID );
$storico = mysql_fetch_array ( $res );

$tabella = new html (0,"90%",array(5,20,5,20,5,5,5,20,0));
$tabella->intestazione (array("","Cognome","","Nome","","Sesso","","Data di nascita",""));
?>
	<tr>
<?php
if ($dato['attivo']) {
?>
		<td class="spazioh"><a href="wf_gest.php?azione=wf_off&id=<?php print $dato['id']?>"><img src="<?php print $CONF['base_url'] ?>/img/attivo.gif" ALT="Atleta Attivo" TITLE="Atleta Attivo"></a></td>
<?php	} else { ?>
		<td class="spazioh"><a href="wf_gest.php?azione=wf_on&id=<?php print $dato['id']?>"><img src="<?php print $CONF['base_url'] ?>/img/non_attivo.gif" ALT="Atleta non Attivo" TITLE="Atleta non Attivo"></a></td>
<?php	} ?>
		<td class="colip"><input size=20% type="text" name=cognome value="<?php print $dato['cognome'] ?>"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size=20% type="text" name=nome value="<?php print $dato['nome'] ?>"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip">
			<select name=sesso>
			<option value="F" <?php if ($dato['sesso'] == "F") { print "selected=\"selected\"";}?>>F</option>
			<option value="M" <?php if ($dato['sesso'] == "M") { print "selected=\"selected\"";}?>>M</option>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input id="nascita_data" class="data" size=10% type="text" name=nascita_data value="<?php print $dato['nascita_data'] ?>"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>


	</tr>
<?php
$tabella->riga(5);
$tabella->close();

$tabella = new html (0,"90%",array(5,15,5,15,5,10,5,15,5,15,0));
$tabella->intestazione (array("","Palestra","","Cintura","","Lunghezza","","Categoria","","Scad. certificato",""));
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip">
			<select name=id_palestra>
<?php	while ($dato_palestra = mysql_fetch_array ( $res_palestra )) { ?>
			<option value="<?php print $dato_palestra['id']?>" <?php if ($dato['id_palestra'] == $dato_palestra['id']) { print "selected=\"selected\"";}?>><?php print $dato_palestra['palestra']?></option>
<?php	} ?>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip">
			<select name=id_cintura>
<?php	while ($dato_cintura = mysql_fetch_array ( $res_cintura )) { ?>
			<option value="<?php print $dato_cintura['id']?>" <?php if ($dato['id_cintura'] == $dato_cintura['id']) { print "selected=\"selected\"";}?>><?php print $dato_cintura['cintura']?></option>
<?php	} ?>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size=5% type="text" name=tcintura value="<?php print $dato[tcintura] ?>"> cm.</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print categoria($dato['nascita_data']) ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input id="certificato" class="data" size=10% type="text" name=certificato value="<?php print $dato['certificato'] ?>"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php
$tabella->close();

$tabella = new html (0,"90%",array(5,85,0));
$tabella->riga(10);
$tabella->intestazione (array("","Commenti",""));
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td><textarea name="commento" rows="5" cols="100%"><?php print $dato[commento]?></textarea></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php
$tabella->close();

$tabella = new html (0,"90%",array(40,5,10,5,40));
$tabella->riga(20);
$tabella->intestazione (array("","Conferma","","Ritorna",""), array("","textc","","textc",""));
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="textc"><input type="image" alt="Conferma" src="<?php print $CONF[base_url] ?>/img/applica.png" value="Conferma"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="textc"><a href="iscritti_lista.php" alt="Ritorna" ><img src="<?php print $CONF['base_url'] ?>/img/redx.png"></a></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php 
$tabella->riga(40);
$tabella->close(); 

if (!empty ($storico)) { 

	$tabella = new html (0,"90%",array(5,40,10,40,5));
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td><hr align="center" size="2" width="100%" color="#FFC350"></td>
		<td class="textc"><?php print $storico[data] ?></td>
		<td><hr align="center" size="2" width="100%" color="#FFC350"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php
	$tabella->close();

	$tabella = new html (0,"90%",array(2,7,1,7,1,6,1,7,1,7,1,5,1,5,1,7,1,7,1,7,1,6,1,6,2,4,0));
	$tabella->intestazione (array("","Peso","","Altezza","","Cooper","","Sargeant","","Fles. Tr.","","Calci SX","","Calci DX","","S. Lungo","","Arti S. Av","","Arti S. Ind","","Mob. Art.","","Cingolo","","",""));

	$img = "<a href=\"iscritti_storico.php?id=$id\"><img src=\"$CONF[base_url]/img/modify.gif\"></a>";
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="peso" value="<?php print $storico[peso] ?>" <?php print $tipo ?>> Kg</input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="2" name="altezza" value="<?php print $storico[altezza] ?>" <?php print $tipo ?>> cm</input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="cooper" value="<?php print $storico[cooper] ?>" <?php print $tipo ?>> m</input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="sargeant" value="<?php print $storico[sargeant] ?>" <?php print $tipo ?>> cm</input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="flesstro" value="<?php print $storico[flesstro] ?>" <?php print $tipo ?>> cm</input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="calcisx" value="<?php print $storico[calcisx] ?>" <?php print $tipo ?>></input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="calcidx" value="<?php print $storico[calcidx] ?>" <?php print $tipo ?>></input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="slungo" value="<?php print $storico[slungo] ?>" <?php print $tipo ?>> cm</input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="pallaav" value="<?php print $storico[pallaav] ?>" <?php print $tipo ?>> cm</input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="pallaind" value="<?php print $storico[pallaind] ?>" <?php print $tipo ?>> cm</input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="mobart" value="<?php print $storico[mobart] ?>" <?php print $tipo ?>> &#176</input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input size="3" name="cingolo" value="<?php print $storico[cingolo] ?>" <?php print $tipo ?>></input></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="spazioh"><?php print $img ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php
	$tabella->close();
} else {
	$tabella = new html (0,"90%",array(48,4,48));
        $tabella->intestazione (array("","Storico",""), array("","textc",""));
?>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="textc"><a href="iscritti_storico.php?id=<?php print $id ?>"><img src="<?php print $CONF[base_url] ?>/img/modify.gif"></a></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<?php
	$tabella->close();
}
?>
</form>
<?php view_footer(); ?>
