
<?php 
  if (!isset($_SESSION['username'])) {
    $_SESSION['msg'] = "To access this page, Login is required";
    header('location: login.php');
  }  ?>

<?php

include('inc/db.php');
$status="";
if (isset($_POST['code']) && $_POST['code']!=""){
$code = $_POST['code'];
$result = mysqli_query($con,"SELECT * FROM `products` WHERE `code`='$code'");
$row = mysqli_fetch_assoc($result);
$name = $row['name'];
$code = $row['code'];
$price = $row['price'];
$size = $row['size'];
$image = $row['image'];

$cartArray = array(
	$code=>array(
	'name'=>$name,
	'code'=>$code,
	'price'=>$price,
	'size'=>$size,
	'quantity'=>1,
	'image'=>$image)
);

if(empty($_SESSION["shopping_cart"])) {
	$_SESSION["shopping_cart"] = $cartArray;
	$status = "<div class='box'>Product is added to your cart!</div>";
			//echo "<script type= 'text/javascript'> opencart() </script>";


}else{
	$array_keys = array_keys($_SESSION["shopping_cart"]);
	if(in_array($code,$array_keys)) {
		$status = "<div class='box' style='color:red;'>Product is already added to your cart!</div>";	
		// echo "<script type= 'text/javascript'> opencart() </script>";

	} else {
	$_SESSION["shopping_cart"] = array_merge($_SESSION["shopping_cart"],$cartArray);
	$status = "<div class='box'>Product is added to your cart!</div>";
//	echo "<script type= 'text/javascript'> opencart() </script>";
	}

	}
}
?>
<html>
<head>

</head>
<body>
<style type="text/css"> 

 body { font-family: helvetica, sans-serif; }
* {  box-sizing: border-box;}
body {  margin: 0;}


.wrapper{ background:whitesmoke;  width:100%; border: 0px solid #a1a1a1;  margin:auto; resize: none;     overflow:hidden; }

.cart-a {  float: left;  width:100%;  height: 100%; background:white ;border-top: 2px solid whitesmoke;  overflow-x: auto; scrollbar-width: thin; }

.cart-b { float: left; width: 50%;  padding: 15px; overflow-x: auto; scrollbar-width: thin; height: 90%; }

.product-image{ float: left;  height: 2em; }

.btn-release{ background:none; border: 1px solid  #7CB9E8; font-weight: bold; color:#7CB9E8; padding: 4px 15px; font-size: x-small; border-radius: 3px;  }

.btn-release:hover{background:#dde; border:  1px solid none;  padding: 5px 15px; opacity: .6}




td {  padding:4px;border-bottom: 1px solid #ddd !important; font-size: xx-small; }

th {  padding:10px;border: 1px solid #ddd !important;  white; opacity: .8;text-align: left;  font-size: x-small;
 border-spacing: 1;}

 .cart-row { background: white; }

table { margin: auto; border-spacing: 1; width:100%;  overflow-x: auto;  background: white;}




</style>



<div class="cart-a"> 

<?php

$result = mysqli_query($con,"SELECT * FROM `products` where quantity > 0"); ?>

<?php
echo "<table >";
echo "<thead>";
echo "<tr>";
echo "<th>Image</th>";
//echo "<th>code</th>";
echo "<th>name</th>";
echo "<th>Size</th>";
echo "<th>Items in store</th>";
echo "<th>Price</th>";
echo "<th>Release</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch_array()){
echo "<tr>";
echo "<form method='post' action=''>";
echo "<td><input type='hidden' name='code' value=".$row['code']." />";
echo "<img class='product-image' src='../product-images/".$row['image']."' /></td>";
// echo "<td>" . $row['code'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['size'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>KSH. " . $row['price'] ."/= </td>";
echo "<td><button type='submit' class='btn-release'>Exchange</button> </td>";
echo "</form>";
echo "</tr>";
}

echo "</tbody>";                            
echo "</table>";
       
mysqli_close($con);
?>

</div>



</body>
</html>