<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta charset="utf-8" />
	<script type="text/javascript" src="./js/js.js"></script>
		<link rel="stylesheet" href="./css/bootstrap.min.css">
</head>
<body>
	<div class="container">
		<div class="jumbotron text-center"><h1>Restaurant</h1><br><h5>Fes la teva reserva ara mateix!</h5></div>
		<div class="row"><hr /></div>
<?php
if (isset($_POST['submit'])) { 
	$link = mysqli_connect("127.0.0.1", "alumne", "keiL2lai", "jbalmes");
	$nom = $_REQUEST['nom'];
	$taula = $_REQUEST['taula'];
	$plats = $_REQUEST['plats'];
	//funció split:
	//This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0. 
	$plats = explode(';', $plats);

	//echo $nom;
	//echo $taula;
	//echo $plats[0];

	$sql = "select max(id_client) as max_id_client from client";
	$result = mysqli_query($link,$sql);
	$row = mysqli_fetch_assoc($result);
	if (!isset($row['max_id_client'])){
		$vid_client=1;
	} else {
		$vid_client=$row['max_id_client']+1;
	}

	$sql = "INSERT INTO client (id_client, nom, taula) VALUES (".$vid_client.",'".$nom."', '".$taula."');";
	//echo $sql;
	mysqli_query($link, $sql);


	$string = "Hola ".$nom.", has reservat la taula ".$taula." amb els següents plats:";
	echo $string;
	echo "<ul>";
	for($i = 0; $i<sizeof($plats)-1;$i++){
		$sentencia = "INSERT INTO plat (id_client, nom_plat) VALUES (".$vid_client.", '".$plats[$i]."');";
		//echo $sentencia."<br />";
		mysqli_query($link, $sentencia);
		echo "<li>".$plats[$i]."</li>";
		$err=mysqli_error($link);
		print"$err";
		
	}
	echo "</ul>";
}
?>

		<div class="row">
			<div class="col-md-3" >
					<img src="./img/amanida.jpg" />
			</div>
			<div class="col-md-3" >
					<img src="./img/canelons.jpg" />
			</div>
			<div class="col-md-3" >
					<img src="./img/flam.jpeg" />
			</div>
			<div class="col-md-3" >
					<img src="./img/fruita.jpg" />
			</div>
		</div>
		<div class="row"><hr /></div>
		<div class="row">
			<div class="col-md-3" >
					<img src="./img/macarrons.jpg" />
			</div>
			<div class="col-md-3" >
					<img src="./img/pollastre.jpg" />
			</div>
			<div class="col-md-3" >
					<img src="./img/tallat.jpg" />
			</div>
			<div class="col-md-3" >
					<img class="img-responsive" src="./img/vedella.jpg" />
			</div>
		</div>		
	<div class="row"><hr /></div>		
	<div class="row">
		<div class="col-md-8">
	   		<form class="form-horizontal" method="post"action="index.php">
	   			<div class="form-group">
					<label class="col-sm-2 control-label">Nom:</label>
				    <div class="col-sm-6">
				    	<input name="nom" maxlength="20" class="form-control" placeholder="Introdueix un el teu nom" onchange="validarnom(this)" /> 		
				    </div>
				    <div class="col-sm-4 validacio" id="validarnom"></div> 
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">Taula:</label>
				    <div class="col-sm-6">
				    	<input id="taula" name="taula" maxlength="2" class="form-control" placeholder="Ex: A1" onchange="validartaula(this)" /> 		
				    </div>
				     <div class="col-sm-4 validacio" id="validartaula"></div> 
				</div>
				<div class="form-group">
					<div class="col-sm-6">
				    	<input type="hidden" id="platsocults" name="plats" class="form-control" onsubmit="validarplats()" /> 		
				    </div>
				</div>
				<div class="form-group">
				    <div class="col-sm-offset-2 col-sm-10">
				    	<button type="submit" class="btn btn-default" name="submit">Enviar</button>
				    </div>
				</div>
			</form>
		</div>
	</div>
	</div>
	<script src="./js/jquery-2.1.4.min.js"></script>
	<script src="./js/bootstrap.min.js"></script>
</body>
</html>