<?php
	session_start();
	include './conexion.php';
	if(isset($_SESSION['carrito'])){
		if(isset($_GET['id'])){
					$arreglo=$_SESSION['carrito'];
					$encontro=false;
					$numero=0;
					for($i=0;$i<count($arreglo);$i++){
						if($arreglo[$i]['Id']==$_GET['id']){
							$encontro=true;
							$numero=$i;
						}
					}
					if($encontro==true){
						$arreglo[$numero]['Cantidad']=$arreglo[$numero]['Cantidad']+1;
						$_SESSION['carrito']=$arreglo;
					}else{
						$nombre="";
						$precio=0;
						$imagen="";
						$re=mysql_query("select * from articulos where id=".$_GET['id']);
						while ($f=mysql_fetch_array($re)) {
							$nombre=$f['nombre'];
							$precio=$f['precio'];
							$imagen=$f['imagen'];
						}
						$datosNuevos=array('Id'=>$_GET['id'],
										'Nombre'=>$nombre,
										'Precio'=>$precio,
										'Imagen'=>$imagen,
										'Cantidad'=>1);

						array_push($arreglo, $datosNuevos);
						$_SESSION['carrito']=$arreglo;

					}
		}




	}else{
		if(isset($_GET['id'])){
			$nombre="";
			$precio=0;
			$imagen="";
			$re=mysql_query("select * from articulos where id=".$_GET['id']);
			while ($f=mysql_fetch_array($re)) {
				$nombre=$f['nombre'];
				$precio=$f['precio'];
				$imagen=$f['imagen'];
			}
			$arreglo[]=array('Id'=>$_GET['id'],
							'Nombre'=>$nombre,
							'Precio'=>$precio,
							'Imagen'=>$imagen,
							'Cantidad'=>1);
			$_SESSION['carrito']=$arreglo;
		}
	}
?>
<!DOCTYPE html>
<html>
<head>
	<title>Carrito de Compras</title>
	<meta charset="utf-8" />
	<link rel="stylesheet" type="text/css" href="css/estilo.css" />
	<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
	<script type="text/javascript"  src="./js/scripts.js"></script>

</head>
<body>

<?php    include_once("cabecera.php"); ?>  

	<section>
		<div id="contenedor">

			<?php
				$total=0;
				if(isset($_SESSION['carrito'])){
				$datos=$_SESSION['carrito'];
				
				$total=0;
				for($i=0;$i<count($datos);$i++){
					
					?>
					<div class="producto">
						<center>
							<img src="./images/<?php echo $datos[$i]['Imagen'];?>" width="30%"><br>
							<span><div class="titulo"><strong><?php echo $datos[$i]['Nombre'];?></strong></div></span><br>
							<span>Precio: <?php echo $datos[$i]['Precio'];?></span><br>
							<span>Cantidad: <?php echo $datos[$i]['Cantidad'];?></span><br>
							<data-precio="<?php echo $datos[$i]['Precio'];?>"
								data-id="<?php echo $datos[$i]['Id'];?>"
								class="cantidad">
							</span>
							<span class="subtotal"><strong>Subtotal: $<?php echo $datos[$i]['Cantidad']*$datos[$i]['Precio'];?></strong></span><br><br>
							<a href="#" class="eliminar" data-id="<?php echo $datos[$i]['Id']?>">Eliminar</a>
						</center>
					</div>
				<div class="total">
					<?php
						$total=($datos[$i]['Cantidad']*$datos[$i]['Precio'])+$total;
					}
						
					}else{
						echo '<center><h2>No has añadido ningun producto</h2></center>';
					}
					echo '<center><h1>Total: $'.$total.'</h1></center>';
					if($total!=0){
					//echo '<center><a href="./mostrar.php" class="aceptar">Comprar</a></center>;
				?>	

					<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="formulario">
					<input type="hidden" name="cmd" value="_cart">
					<input type="hidden" name="upload" value="1">
					<input type="hidden" name="business" value="tu_correo_paypal@gmail.com">
					<input type="hidden" name="currency_code" value="MXN">

					<input type="hidden" name="item_name_<?php echo $i+1;?>" value="<?php echo $datos[$i]['Nombre'];?>">
						<input type="hidden" name="amount_<?php echo $i+1;?>" value="<?php echo $datos[$i]['Precio'];?>">
						<input	type="hidden" name="quantity_<?php echo $i+1;?>" value="<?php echo $datos[$i]['Cantidad'];?>">	
					<?php 
						}
					?>

					<center><input type="submit" value="comprar" class="aceptar" style="width:200px"></center>
					<div id="catalogo">
						<center><br><br><a href="./catalogo.php">Ver catalogo</a></center>
					</div>
				</div>
		</div>
	</section>

			
<?php 
include_once("pie.php")
 ?>

</body>
</html>