<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Buy And Sell</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
<!--Less styles -->
   <!-- Other Less css file //different less files has different color scheam
	<link rel="stylesheet/less" type="text/css" href="themes/less/simplex.less">
	<link rel="stylesheet/less" type="text/css" href="themes/less/classified.less">
	<link rel="stylesheet/less" type="text/css" href="themes/less/amelia.less">  MOVE DOWN TO activate
	-->
	<!--<link rel="stylesheet/less" type="text/css" href="themes/less/bootshop.less">
	<script src="themes/js/less.js" type="text/javascript"></script> -->
	<link href="style.css" type="text/css" rel="stylesheet" />
<!-- Bootstrap style -->
    <link id="callCss" rel="stylesheet" href="themes/bootshop/bootstrap.min.css" media="screen"/>
    <link href="themes/css/base.css" rel="stylesheet" media="screen"/>
<!-- Bootstrap style responsive -->
	<link href="themes/css/bootstrap-responsive.min.css" rel="stylesheet"/>
	<link href="themes/css/font-awesome.css" rel="stylesheet" type="text/css">
<!-- Google-code-prettify -->
	<link href="themes/js/google-code-prettify/prettify.css" rel="stylesheet"/>
<!-- fav and touch icons -->
    <link rel="shortcut icon" href="themes/images/ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="themes/images/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="themes/images/ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="themes/images/ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="themes/images/ico/apple-touch-icon-57-precomposed.png">
	<style type="text/css" id="enject"></style>

	<script src="html2canvas.js" type="text/javascript"></script>
	<script src="html2canvas.min.js" type="text/javascript"></script>
  </head>
<body>
<div id="header">

<div class="container" style="width:100%; margin:0 auto;">
<!-- Navbar ================================================== -->
<div id="logoArea" class="navbar">
<a id="smallScreen" data-target="#topMenu" data-toggle="collapse" class="btn btn-navbar">

	<span class="icon-bar"></span>
	<span class="icon-bar"></span>
	<span class="icon-bar"></span>
</a>
  <div class="navbar-inner">

    <a class="brand" href="home.php"><img style="width:100px;height:50px;" src="themes/images/buyandsell.png" alt="BuyAndSell"/></a>
		<form class="form-inline navbar-search" method="post" action="products.php" >
		<input id="srchFld" class="srchTxt" type="text" />
		  <select class="srchTxt">
        <option>Category</option>
        <option>Cameras </option>
        <option>Computers</option>
        <option>Women's Clothing</option>
        <option>Women's Shoes</option>
        <option>Men's Clothings</option>
        <option>Men's Shoes</option>
        <option>Stationery</option>
        <option>Books</option>
		</select>
		  <button type="submit" id="submitButton" class="btn btn-primary">Go</button>
    </form>
		<ul id="topMenu" class="nav pull-right">
			<li class=""><a href="sellitems.php">SELL ITEMS</a></li>
			<li class=""><a href="product.php">BROWSE</a></li>
			<li class=""><a href="userP.php">PROFILE</a></li>
			<li class="">
			<a href="logoutPro.php" style="padding-right:0"><span class="btn btn-large btn-success">Logout</span></a>
			</li>
		</ul>
  </div>
</div>
</div>
</div>


<body style="width:60%; margin:0 auto;">
<div id="shopping-cart">
<div class="txt-heading" style="background-color:#7B7B7B;">Shopping Cart <a id="btnEmpty" href="shopCart.php?action=empty" style=" color:black;">Empty Cart</a></div>

<?php

if (!isset($_SESSION)) {session_start();}
if (isset($_SESSION['seller'])){}
else { $_SESSION["seller"] = array();}

require_once("dbcontroller.php");
$db_handle = new DBController();

if(!empty($_GET["action"])) {
switch($_GET["action"]) {
	case "checkout":
	if(isset($_SESSION["cart_item"])){
		$item_total = 0;

		echo '<div id="capture" style="padding: 10px; background: #000000">
		<div >
		<table cellpadding="10" cellspacing="1">
		<tbody>
		<tr>
		<th style="text-align:left;"><strong>Image</strong></th>
		<th style="text-align:left;"><strong>Name</strong></th>
		<th style="text-align:left;"><strong>Info</strong></th>
		<th style="text-align:left;"><strong>Condition</strong></th>
		<th style="text-align:left;"><strong>Quantity</strong></th>
		<th style="text-align:left;padding-right:5%"><strong>Price</strong></th>
		<th style="text-align:center;"><strong>Action</strong></th>
		</tr>';
		
		foreach ($_SESSION["seller"] as $sellr)
		{

			$sellerName = $db_handle->runQuery("SELECT * FROM buyer WHERE buyer_id=" . $sellr . ";");
			echo '<th style="text-align:center"><strong>Seller : '.$sellerName[0]["first_name"]. ' ' .$sellerName[0]["last_name"]. '</strong></th>';
			
			foreach ($_SESSION["cart_item"] as $item){
			
				if($item['buyer_id'] == $sellr)
				{
					echo '<tr>';
					echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;width:25%"><strong><img style="width:50%;margin: 0px 0px 0px 30%"  src="data:image/jpeg;base64,'.base64_encode($item["image"]).'"/></a></strong></td>';
					echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><strong>'.$item["items_name"].'</strong></td>';
					echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;">'.$item["items_info"].'</td>';
					echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;">'.$item["size_items"].'</td>';
					echo '<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">'.$item["quantity"].'</td>';
					echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;padding-right:5%"> $'.$item["price"].'</td>';
					echo '<td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><a href="shopCart.php?action=remove&itemCode='.$item["itemCode"].'" class="btnRemoveAction">Remove Item</a></td>';
					echo '</tr>';
				}
				$item_total += ($item["price"]*$item["quantity"]);
			}
		}
	}

	if(!isset($item_total))
	{
		$item_total = 0;
	}
	
	echo '<tr>';
	if(isset($_SESSION["cart_item"]))
	{
		echo '<td colspan="10" align=right><strong>Total:</strong> $'. $item_total .'</td>';
	}
	echo '</tr>';

	echo '</tbody>';
	echo '</table>';
	echo '</div>';
	echo '</div>';
				
	break;
	case "add":
		if(!empty($_POST["quantity"])) {
			$productByCode = $db_handle->runQuery("SELECT * FROM items WHERE itemCode='" . $_GET["itemCode"] . "';");
			$itemArray = array($productByCode[0]["itemCode"]=>array('items_name'=>$productByCode[0]["items_name"], 'size_items'=>$productByCode[0]["size_items"], 'items_info'=>$productByCode[0]["items_info"], 'itemCode'=>$productByCode[0]["itemCode"], 'image'=>$productByCode[0]["image"], 'buyer_id'=>$productByCode[0]["buyer_id"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));
			
			if(!in_array($productByCode[0]["buyer_id"],$_SESSION["seller"])){array_push($_SESSION["seller"],$productByCode[0]["buyer_id"]);}
			
			if(!empty($_SESSION["cart_item"])) {
				if(in_array($productByCode[0]["itemCode"],array_keys($_SESSION["cart_item"]))) {
					foreach($_SESSION["cart_item"] as $k => $v) {
							if($productByCode[0]["itemCode"] == $k) {
								if(empty($_SESSION["cart_item"][$k]["quantity"])) {
									$_SESSION["cart_item"][$k]["quantity"] = 0;
								}
								$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
							}
					}
				} else {
					$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
					echo "test";
				}
			} else {
				$_SESSION["cart_item"] = $itemArray;
			}
		}
	break;
	case "remove":
		if(!empty($_SESSION["cart_item"])) {
			foreach($_SESSION["cart_item"] as $k => $v) {
					if($_GET["itemCode"] == $k)
						unset($_SESSION["cart_item"][$k]);				
					if(empty($_SESSION["cart_item"]))
						unset($_SESSION["cart_item"]);
			}
		}

		if(isset($_SESSION["cart_item"])){
			$item_total = 0;
	
			echo '<div id="capture" style="padding: 10px; background: #000000">
			<div >
			<table cellpadding="10" cellspacing="1">
			<tbody>
			<tr>
			<th style="text-align:left;"><strong>Image</strong></th>
			<th style="text-align:left;"><strong>Name</strong></th>
			<th style="text-align:left;"><strong>Info</strong></th>
			<th style="text-align:left;"><strong>Condition</strong></th>
			<th style="text-align:left;"><strong>Quantity</strong></th>
			<th style="text-align:left;padding-right:5%"><strong>Price</strong></th>
			<th style="text-align:left;"><strong>Action</strong></th>
			</tr>';
			
			foreach ($_SESSION["seller"] as $sellr)
			{
	
				$sellerName = $db_handle->runQuery("SELECT * FROM buyer WHERE buyer_id=" . $sellr . ";");
				echo '<th style="text-align:center;"><strong>Seller : '.$sellerName[0]["first_name"]. ' ' .$sellerName[0]["last_name"]. '</strong></th>';
				
				foreach ($_SESSION["cart_item"] as $item){
				
					if($item['buyer_id'] == $sellr)
					{
						echo '<tr>';
						echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;width:25%"><strong><img style="width:50%;margin: 0px 0px 0px 30%"  src="data:image/jpeg;base64,'.base64_encode($item["image"]).'"/></a></strong></td>';
						echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><strong>'.$item["items_name"].'</strong></td>';
						echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;">'.$item["items_info"].'</td>';
						echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;">'.$item["size_items"].'</td>';
						echo '<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">'.$item["quantity"].'</td>';
						echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;padding-right:5%"> $'.$item["price"].'</td>';
						echo '<td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><a href="shopCart.php?action=remove&itemCode='.$item["itemCode"].'" class="btnRemoveAction">Remove Item</a></td>';
						echo '</tr>';
					}
					$item_total += ($item["price"]*$item["quantity"]);
				}
			}
		}
	
		if(!isset($item_total))
		{
			$item_total = 0;
		}
		
		echo '<tr>';
		if(isset($_SESSION["cart_item"]))
		{
		echo '<td colspan="10" align=right><strong>Total:</strong> $'. $item_total .'</td>';
		}
		echo '</tr>';
	
		echo '</tbody>';
		echo '</table>';
		echo '</div>';
		echo '</div>';
	break;

	case "pay":
		echo '<script>alert("Buyer has made payment. Receipt will be generated in the next tab.")</script>';

		echo '<div id="smallSS" style="background-color: white">';
				
		if(isset($_SESSION["cart_item"])){
			$item_total = 0;
	
			echo '<div id="capture" style="padding: 5px; background: #000000">
			<div>';
			$buyerName = $db_handle->runQuery("SELECT * FROM buyer WHERE buyer_id=" . $_SESSION['byrID'] . ";");
			echo '<div style="text-align:center"><font size="3" color="white">Buyer Name : '.$buyerName[0]["first_name"]. ' ' .$buyerName[0]["last_name"].'</font></div>';
			echo '<div style="text-align:center"><font size="3" color="white">Receipt Date/Time : '.(new \DateTime())->format('Y-m-d H:i:s').'</font></div>';
			echo '<table cellpadding="10" cellspacing="1">
			<tbody>
			<tr>
			<th style="text-align:left;"><strong>Image</strong></th>
			<th style="text-align:left;"><strong>Name</strong></th>
			<th style="text-align:left;"><strong>Info</strong></th>
			<th style="text-align:left;"><strong>Condition</strong></th>
			<th style="text-align:left;"><strong>Quantity</strong></th>
			<th style="text-align:left;padding-right:5%"><strong>Price</strong></th>
			</tr>';
			
			foreach ($_SESSION["seller"] as $sellr)
			{
	
				$sellerName = $db_handle->runQuery("SELECT * FROM buyer WHERE buyer_id=" . $sellr . ";");
				echo '<th style="text-align:left;"><strong>Seller : '.$sellerName[0]["first_name"]. ' ' .$sellerName[0]["last_name"]. '</strong></th>';
				
				foreach ($_SESSION["cart_item"] as $item){
				
					if($item['buyer_id'] == $sellr)
					{
						echo '<tr>';
						echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;width:25%"><strong><img style="width:50%;margin: 0px 0px 0px 30%"  src="data:image/jpeg;base64,'.base64_encode($item["image"]).'"/></a></strong></td>';
						echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;"><strong>'.$item["items_name"].'</strong></td>';
						echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;">'.$item["items_info"].'</td>';
						echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;">'.$item["size_items"].'</td>';
						echo '<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">'.$item["quantity"].'</td>';
						echo '<td style="text-align:left;border-bottom:#F0F0F0 1px solid;padding-right:5%"> $'.$item["price"].'</td>';
						echo '</tr>';
					}
					$item_total += ($item["price"]*$item["quantity"]);
				}
			}
		}
	
		if(!isset($item_total))
		{
			$item_total = 0;
		}
		
		echo '<tr>';
		echo '<td colspan="10" align=right><strong>Total:</strong> $'. $item_total .'</td>';
		echo '</tr>';
	
		echo '</tbody>';
		echo '</table>';
		echo '</div>';
		echo '</div>';

		echo '</div>';

		echo '<script>
				html2canvas(document.querySelector("#smallSS")).then(canvas => {
					document.body.appendChild(canvas);
					var image = new Image();
					image.src = canvas.toDataURL();

					var w = window.open("");
					w.document.write(image.outerHTML);
				});
			</script>';

		unset($_SESSION["cart_item"]);
		unset($_SESSION["seller"]);
		echo "<script type='text/javascript'>
				window.location.replace('HTTP://localhost/fyp_draft/shopCart.php');
			</script>";
	break;
	case "empty":
		unset($_SESSION["cart_item"]);
		unset($_SESSION["seller"]);
	break;
	}
}
?>

<div id="product-grid">
	<div class="txt-heading" style="background-color:#7B7B7B;">BuyAndSell.com
		<?php if(isset($_SESSION["cart_item"]))
		{
			echo '<a id="btnEmpty" href="shopCart.php?action=pay" style="color:black;">Check Out</a>
				  <a id="btnEmpty" href="https://www.cimbclicks.com.my/clicks/#/" style="margin-right: 10px; margin-bottom: 10px; color:black;">CimbClick</a>
				  <a id="btnEmpty" href="https://www.maybank2u.com.my/mbb/m2u/common/M2ULogin.do?action=Login" style="margin-right: 10px; margin-bottom: 10px; color:black;">Maybank</a>';
		}?>
	</div>
</div>


<!-- Placed at the end of the document so the pages load faster ============================================= -->

<script src="themes/js/jquery.js" type="text/javascript"></script>
<script src="themes/js/bootstrap.min.js" type="text/javascript"></script>
<script src="themes/js/google-code-prettify/prettify.js"></script>

<script src="themes/js/bootshop.js"></script>
<script src="themes/js/jquery.lightbox-0.5.js"></script>

<span id="themesBtn"></span>
</body>
</html>