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

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

	if ($id_iscritto != "") {

		$c_ordine = sistemadata($c_ordine);
		$c_pagamento = sistemadata($c_pagamento); 
		$c_consegna = sistemadata($c_consegna);

		($c_ordine != "") 	? $c_ordine_q = "STR_TO_DATE('$c_ordine', '%d.%m.%Y')" 		: $c_ordine_q = "NULL";
		($c_pagamento != "") 	? $c_pagamento_q = "STR_TO_DATE('$c_pagamento', '%d.%m.%Y')" 	: $c_pagamento_q = "NULL";
		($c_consegna != "") 	? $c_consegna_q = "STR_TO_DATE('$c_consegna', '%d.%m.%Y')" 	: $c_consegna_q = "NULL";


		for ($i=1;$i<=$articoli;$i++) {
			$id_materiale = "id_materiale$i";
			$taglia = "taglia$i";
			if ($$id_materiale != "") {

				if ($$taglia == "N/D") $$taglia = "";

				$query = "	INSERT INTO
							wf_ordini
						SET
							id_iscritto = $id_iscritto,
							id_materiale = ".$$id_materiale.",
							taglia = UPPER('".$$taglia."'),
							c_ordine = $c_ordine_q,
							c_pagamento = $c_pagamento_q,
							c_consegna = $c_consegna_q,
							materiale = (SELECT nome FROM wf_materiale WHERE id = ".$$id_materiale."),
							categoria = (SELECT categoria FROM wf_materiale WHERE id = ".$$id_materiale."),
							produttore = (SELECT produttore FROM wf_materiale WHERE id = ".$$id_materiale."),
							costo = (SELECT vendita FROM wf_materiale WHERE id = ".$$id_materiale.")
				";
		
				$res = mysql_query( $query, $DB_ID );
			}
		}
	}

?>
		<script type="text/javascript">
			document.location.href='index.php'
		</script>
<?php
		exit;

} elseif (isset($_GET['articoli'])) {
	$articoli = $_GET['articoli'];
} else {
	$articoli = 1;
}
?>

<form name="" method="post" id="ordine">
<input type="hidden" name="id" value="<?php @print $id ?>">
<input type="hidden" name="articoli" value="<?php @print $articoli ?>">
<?php
@$query = "	SELECT
			wf_ordini.id,
			wf_ordini.id_iscritto,
			wf_iscritto.nome,
			wf_iscritto.cognome,
			wf_materiale.nome AS materiale,
			wf_cat_materiale.nome AS categoria,
			wf_ordini.id_materiale,
			wf_ordini.taglia,
			wf_ordini.costo,
			DATE_FORMAT(wf_ordini.c_ordine, '%d.%m.%Y') AS c_ordine,
			DATE_FORMAT(wf_ordini.c_pagamento, '%d.%m.%Y') AS c_pagamento,
			DATE_FORMAT(wf_ordini.c_consegna, '%d.%m.%Y') AS c_consegna,
			DATE_FORMAT(wf_ordini.f_ordine, '%d.%m.%Y') AS f_ordine,
			DATE_FORMAT(wf_ordini.f_consegna, '%d.%m.%Y') AS f_consegna,
			DATE_FORMAT(wf_ordini.s_pagamento, '%d.%m.%Y') AS s_pagamento,
			DATE_FORMAT(wf_ordini.s_consegna, '%d.%m.%Y') AS s_consegna
		FROM
			wf_ordini
		JOIN
			wf_iscritto
			ON
				wf_ordini.id_iscritto = wf_iscritto.id
		JOIN
			wf_materiale
			ON
				wf_ordini.id_materiale = wf_materiale.id
		JOIN
			wf_cat_materiale
			ON
				wf_materiale.id_cat_materiale = wf_cat_materiale.id
		WHERE
			wf_ordini.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_iscritto.id_palestra = wf_utenti.id_palestra
				AND
				wf_utenti.id_utenti = ".$UTENTE['id']."
				AND
				wf_utenti.attivo = 1";

$query = "	SELECT
			wf_iscritto.id,
			wf_iscritto.nome,
			wf_iscritto.cognome
		FROM
			wf_iscritto
		$query_admin
		WHERE
			wf_iscritto.attivo = 1
		ORDER BY
			wf_iscritto.cognome,
			wf_iscritto.nome
";
$res_atleti = mysql_query( $query, $DB_ID );

$tabella = new html (0,"90%",array(5,30,5,5,30,0));
$tabella->intestazione (array("","Atleta","","","",""));
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colipb">
			<select name=id_iscritto>
				<option value="">SELEZIONARE</option>
<?php		while ($dato_atleta = mysql_fetch_array ( $res_atleti )) { ?>
			<option value="<?php print $dato_atleta['id']?>"><?php print $dato_atleta['cognome']." ".$dato_atleta['nome']?></option>
<?php		} ?>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip">
			<select name="articoli" id="articoli">
				<option value="1" <?php ($articoli == 1) ? print "selected=\"selected\"" : FALSE ?>>1</option>
				<option value="2" <?php ($articoli == 2) ? print "selected=\"selected\"" : FALSE ?>>2</option>
				<option value="3" <?php ($articoli == 3) ? print "selected=\"selected\"" : FALSE ?>>3</option>
				<option value="4" <?php ($articoli == 4) ? print "selected=\"selected\"" : FALSE ?>>4</option>
				<option value="5" <?php ($articoli == 5) ? print "selected=\"selected\"" : FALSE ?>>5</option>
			</select>
		</td>
		<td class="descrizione">Numero di articoli da ordinare, modificare questo valore comporta il reset di tutti gli altri campi</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php
$tabella->riga(30);
$tabella->close();

$tabella = new html (0,"90%",array(5,20,5,20,5,20,5,20,5,20,0));
$tabella->intestazione (array("","Categoria","","Produttore","","Materiale","","Taglia","","Costo",""));

for ($i=1;$i<=$articoli;$i++) {
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip">
			<select name="categoria<?php print $i ?>" id="categoria<?php print $i ?>"> 
				<option value="">SELEZIONARE</option>
<?php
			$query = "SELECT categoria FROM wf_materiale WHERE attivo = 1 GROUP BY categoria ORDER BY categoria";
			$res_categoria = mysql_query( $query, $DB_ID );
			while ($dato_categoria = mysql_fetch_array ( $res_categoria )) {
				print "<option value=\"$dato_categoria[categoria]\">$dato_categoria[categoria]</option>\n";
			}
?>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip">
			<select name="produttore<?php print $i ?>" id="produttore<?php print $i ?>">
				<option value="">Selezionare prima la categoria</option>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip">
			<select name="id_materiale<?php print $i ?>" id="materiale<?php print $i ?>">
				<option value="">Selezionare prima la categoria</option>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>


		<td class="colip">
			<select name="taglia<?php print $i ?>" id="taglia<?php print $i ?>">
				<option value="">Selezionare prima il materiale</option>
			</select>
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input type="text" id="vendita<?php print $i ?>" name="vendita<?php print $i ?>" size="5%" readonly value=""> &#8364;</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php
	$tabella->riga(5);
}
$tabella->riga(30);
$tabella->close();

$tabella = new html (0,"90%",array(5,90,5));
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip">Atleta</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td><hr></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,10,5,10,5,10,0));
$tabella->intestazione (array("","Ordine","","Pagamento","","Consegna",""));
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colipb"><input id="c_ordine" class="data" size=8% type="text" name=c_ordine value="<?php print date("d/m/Y") ?>"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colipb"><input id="c_pagamento" class="data" size=8% type="text" name=c_pagamento value=""></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colipb"><input id="c_consegna" class="data" size=8% type="text" name=c_consegna value=""></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php
$tabella->riga(30);
$tabella->close();


$tabella = new html (0,"90%",array(5,20,0));
$tabella->riga(); ?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="spaziov" colspan=10><input type="submit" value="Conferma"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>


<?php $tabella->close(); ?>
</form>
<?php view_footer(); ?>
