<?php

require "../menu.php";
$db = new ArticoliDb("..");
$orderIDS = $db->getOrdersData();
while ($order = $orderIDS->fetchArray()) {
    $id = $order["id"];
    if (isset($_POST["id-" . $id])) {
        $db->exec("DELETE FROM ordini WHERE id='{$id}'");
        echo "L'ordine " . $id . " è rimosso correttamente <br/>";
    }
}
echo 'Torna a <a href="seeOrders.php">gestione ordini</a>';
<?php

$id = $_GET["id"];
require "../menu.php";
$db = new ArticoliDb("..");
$result = $db->query("SELECT * FROM oggettiordinati WHERE id_ordine='{$id}'");
echo '<table class="table">
			  <thead>
			    <tr>
			  	<th>Oggetto</th>
			  	<th>Taglia</th>
			  	<th>Quantità</th>
			    </tr>
			  </thead>
			  <tbody>';
while ($row = $result->fetchArray()) {
    echo "<tr>";
    echo "<td>" . $row[1] . "</td>";
    echo "<td>" . $row[2] . "</td>";
    echo "<td>" . $row[3] . "</td>";
    echo "</tr>";
}
echo "\t</tbody>\n    </table>";
<?php

require "../menu.php";
$db = new ArticoliDb("..");
?>

<b>AGGIUNGI ARTICOLI DISPONIBILI DA ACQUISTARE</b>
<form action="" method="POST">
	Nome: <input type="text" name="item"><br/>
	Descrizione: <input type="text" name="descrizione"/><br/>
	Taglie (S, M, ...): <input type"text" name="taglie"/><br/>
	Prezzo: <input type="text" name="prezzo"/><br/>
	<input type="submit" name="submit" value="INSERISCI"/>
</form>

<b>RIMUOVI ARTICOLO DA DISPONIBILI DA ACQUISTARE</b>
<form action="" method="POST">
	Nome: <input type="text" name="item1"><br/>
	<input type="submit" name="submit1" value="RIMUOVI"/>
</form>

<b>!!!ATTENTO: ESEGUI QUERY IN DB</b><br/>
<b>articoli</b>(nome TEXT PRIMARY KEY UNIQUE, descrizione REAL, taglie TEXT, prezzo INT)<br/>
<b>ordini</b> (id INTEGER PRIMARY KEY UNIQUE, data INT, nome TEXT, email TEXT, branca TEXT, telefono INT, totale REAL, pagamento TEXT, saldato INT, consegnato INT)<br/>
<b>oggettiordinati</b> (id_ordine INT, oggetto TEXT, taglia TEXT, quantity INT, FOREIGN KEY(id_ordine) REFERENCES ordine(id), FOREIGN KEY(oggetto) REFERENCES articoli(nome))<br/>
<form action="" method="POST">
	Query: <input type="text" name="item2"><br/>
	<input type="submit" name="submit2" value="INVIA"/>
</form>

<?php 
<?php

//ordini (id INTEGER PRIMARY KEY UNIQUE, data INT, nome TEXT, email TEXT, branca TEXT, telefono INT, totale REAL, pagamento TEXT, saldato INT, consegnato INT)
require "../menu.php";
require "../mail_config.php";
$db = new ArticoliDb("..");
$orderedItems = $db->getAllOrderItem();
$ordiniInSospeso = $db->query("SELECT * FROM ordini WHERE saldato = 0 AND consegnato = 0 ORDER BY id");
$ordiniDaConsegnare = $db->query("SELECT * FROM ordini WHERE saldato = 1 AND consegnato = 0 ORDER by id");
?>
	<script>
		function a_onClick(obj) {
		 
		 $orderId = obj.attr('id');
		 console.log($orderId)
		 $("#TitoloModale").html("Dettagli ordine #".concat($orderId));
		 $.ajax(
		 	{url: "getItems.php?id=".concat($orderId), 
			 	success: function(result){
			 	$("#tabella").html(result);
		 		}
		 	}
		 );
		 
		 $('#modale').modal('show');
		}
	</script>
</head>
		<div class="container">
		  <h2>Ordini Da Saldare</h2>
		  <p>Ordini in sospeso in attesa di essere saldati</p>            
<?php

require "../menu.php";
$db = new ArticoliDb("..");
$orderIDS = $db->getOrdersData();
while ($order = $orderIDS->fetchArray()) {
    $id = $order["id"];
    if (isset($_POST["id-" . $id])) {
        $db->exec("UPDATE ordini SET saldato=1 WHERE id='{$id}'");
        echo "L'ordine " . $id . " è stato impostato pagato <br/>";
    }
}
echo 'Torna a <a href="seeOrders.php">gestione ordini</a>';
<?php

require "menu.php";
require "mail_config.php";
$db = new ArticoliDb(".");
$idOrdine = $_GET["id"];
$mail = $_GET["mail"];
$result = $db->getOrderData($idOrdine);
$row = $result->fetchArray();
$name = $row["nome"];
$phone = $row["telefono"];
$pagamento = $row["pagamento"];
$saldato = $row["saldato"] == 1 ? "SI" : "NO";
$consegnato = $row["consegnato"] == 1 ? "SI" : "NO";
$branca = $row["branca"];
$totale = $row["totale"];
$items = $db->getOrderItem($idOrdine);
require "mail_config.php";
if ($useSMTP) {
    require "PHPMailer/PHPMailerAutoload.php";
}
$mailBody = "Il tuo ordine &egrave; andato a buon fine<br/>";
$mailBodyPT = "Il tuo ordine e' andato a buon fine\n";
//plain-text version
$mailBody .= "<p>Il totale dell'ordine &egrave; di Euro " . number_format((double) $totale, 2, ',', '') . "</p>";
$mailBodyPT .= "Il totale dell'ordine e' di Euro" . number_format((double) $totale, 2, ',', '') . "\n";
$mailBody .= "<p>Per vedere la tua ricevuta vai alla pagina (" . $ricevutaDir . '/showReceipt.php?mail=' . $mail . '&id=' . $idOrdine . ')</p>';
$mailBodyPT .= "Per vedere la tua ricevuta vai alla pagina (" . $ricevutaDir . '/showReceipt.php?mail=' . $mail . '&id=' . $idOrdine . ')\\n';
$mailBody .= "<p>Se hai scelto la modalit&agrave; di pagamento <b>a mano</b> puoi saldare la quota portando i soldi e una copia della ricevuta direttamente ad un capo a fine riunione (o in alternativa, se non &egrave; possibile stampare la pagina, baster&agrave; il numero dell'ordine e l'importo).</p><br/>";
$mailBodyPT .= "Se hai scelto la modalita' di pagamento a mano puoi saldare la quota portando i soldi e una copia della ricevuta direttamente ad un capo a fine riunione (o in alternativa, se non e' possibile stampare la pagina, bastera' il numero dell'ordine e e l'importo).";
$mailBody .= "Se hai scelto di pagare tramite <b>bonifico</b>, puoi effettuarlo al seguente IBAN: " . $iban;
<?php

//ordini (id INTEGER PRIMARY KEY UNIQUE, data INT, nome TEXT, email TEXT, branca TEXT, telefono INT, totale REAL, pagamento TEXT, saldato INT, consegnato INT)
require "../menu.php";
require "../mail_config.php";
$db = new ArticoliDb("..");
$orderedItems = $db->getAllOrderItem();
$ordiniConclusi = $db->query("SELECT * FROM ordini WHERE saldato = 1 AND consegnato = 1 ORDER BY id");
?>
	<div class="container">
	  <h2>Ordini già consegnati</h2>
	  <p>Ordini conclusi che possono essere rimossi dalla base</p>            
	  <table class="table table-striped">
		<thead>
		  <tr>
			<th>ID</th>
			<th>Data</th>
			<th>Nome</th>
			<th>eMail</th>
			<th>Branca</th>
			<th>Telefono</th>
			<th>Totale</th>
			<th>Pagamento</th>
			<th></th>
		  </tr>
		</thead>
		<tbody>
			<form action="deleteOrders.php" method="POST">
<?php 
while ($row = $ordiniConclusi->fetchArray()) {
    ?>
<?php

require "menu.php";
require "mail_config.php";
$db = new ArticoliDb(".");
?>
<script>
<?php 
$item = $db->getItem();
$rows = array();
while ($row = $item->fetchArray()) {
    $rows[$row["nome"]] = $row;
    $rows[$row["nome"]]["taglie"] = $db->getTagliePrezzi($row["nome"]);
}
echo "var db=" . json_encode($rows) . ";";
echo "var costoGestione = " . $costoGestioneOrdine . ";";
?>
var row_count = 0;
var total = 0;
var order_items = [];

function print_row(){
	var row = $("<tr></tr>");
	row.attr("id", "n"+row_count);
	row_count++;
	var tdnome = $("<td></td>");
	tdnome.attr("id", "td_nome");
	select_nome = $("<select></select>");
	select_nome.addClass("form-control");
	select_nome.attr("onchange", "item_selected(this)");
	select_nome.append($("<option disabled selected></option>").text("Scegli un articolo"));
<?php

require "../../menu.php";
$db = new ArticoliDb("../..");
$taglie = explode(",", $_POST["taglie"]);
$db->addItem($_POST["item"], str_replace(",", ".", $_POST["prezzo"]), str_replace("'", "%27", $_POST["descrizione"]), $taglie);
<?php

require "../menu.php";
$db = new ArticoliDb("..");
$result = $db->query("SELECT oggetto, taglia, sum(quantity)  FROM oggettiordinati, ordini WHERE ordini.id = oggettiordinati.id_ordine AND ordini.saldato=1 AND ordini.consegnato=0 GROUP BY oggetto, taglia");
echo '<table class="table">
			  <thead>
				<tr>
			  	<th>Oggetto</th>
			  	<th>Taglia</th>
			  	<th>Quantità</th>
				</tr>
			  </thead>
			  <tbody>';
while ($row = $result->fetchArray()) {
    echo "<tr>";
    echo "<td>" . $row[0] . "</td>";
    echo "<td>" . $row[1] . "</td>";
    echo "<td>" . $row[2] . "</td>";
    echo "</tr>";
}
echo "\t</tbody>\n\t</table>";
<?php

require "ArticoliDb.php";
require "mail_config.php";
$db = new ArticoliDb(".");
$items = json_decode(urldecode($_POST["items"]), true);
$totale = $_POST["totale"];
if (isset($_POST['name'])) {
    $name = $_POST["name"];
} else {
    $name = "undefined";
}
if (isset($_POST['mail'])) {
    $mail = $_POST["mail"];
} else {
    $mail = "undefined";
}
if (isset($_POST['phone'])) {
    $phone = $_POST["phone"];
} else {
    $phone = "undefined";
}
if (isset($_POST['pagamento'])) {
    $pagamento = $_POST['pagamento'];
} else {
    $pagamento = "undefined";
}
if (isset($_POST['branca'])) {
    $branca = $_POST['branca'];
} else {
    $branca = "undefined";