<?php
$title = 'Корзина';
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/head.php');
$all_price = 0;
?>
<div class="container theme-showcase" role="main">
	<div class="page-header">
		<h1>Корзина</h1>
	</div>
	<div class="row">
		<?
		if(isset($_POST['send'])){
			$_GET['id'] = abs(intval($_GET['id']));
			$query = $db->query("SELECT  * FROM `cart` WHERE `id`=".$_GET['id']."");
			if($query->num_rows == 0){
				header('location:/');
			}
			$c = $query->fetch_assoc();
			if($user['id'] != $c['id_user']){
				header('location:/');
			}
			$prod = $db->query("SELECT * FROM `products` WHERE `id`=".$c['id_product']."")->fetch_assoc();
			if($prod['in_stock'] < $_POST['col']){
				header('location:/');
			}
			$db->query("UPDATE `cart` SET `col`='".$_POST['col']."' WHERE `id`='".$_GET['id']."'");
			header('location:/cart');
		}

		$query = $db->query("SELECT * FROM `cart` WHERE `id_user`=".$user['id']."");
		while ($cart = $query->fetch_assoc()) {
			$product = $db->query("SELECT * FROM `products` WHERE `id`=".$cart['id_product']."")->fetch_assoc();
			$img = $db->query("SELECT * FROM `products_images` WHERE `id_product`=".$product['id']." LIMIT 0, 1")->fetch_assoc();
			$all_price += $product['price'] * $cart['col'];
			?>
			<div class="col-sm-6 col-md-4">
		    	<div class="thumbnail">
		      		<img src="/files/images/products/<?=$img['image']?>" alt="<?=output($product['name'])?>" width="40%" height="60%">
		      		<div class="caption">
		        		<h3><a href="/product/<?=$product['id']?>"><?=output($product['name'])?></a></h3>
		        		<p>Стоимость: <b><font color="green"><?=$product['price']?> руб.</font></b></p>
		  				<p>Кол-во: <?=$cart['col']?></p>
		  				<form action="?id=<?=$cart['id']?>" method="POST">
		  					<div class="form-row">
								<div class="form-group">
									<label for="inputCol">Количество</label>
									<input type="text" class="form-control" placeholder="Введите название товара" rows="5" name="col" id="inputCol" value="<?=$cart['col']?>">
								</div>
							</div>
		        			<p>
		        				<button type="submit" class="btn btn-primary" name="send">Изменить</button>
		        				<a class="btn btn-danger" role="button" href="/cart/delete/<?=$cart['id']?>">Удалить</a>
		        			</p>
		        		</form>
		      		</div>
		    	</div>
		  	</div>
			<?
		}?>
		
	</div>
	<div class="well">
		<h3>Общая сумма покупок: <font color="green"><?=$all_price?></font></h3>
		<a href="/order/get_pay"><button class="btn btn-primary">Перейти к оплате</button></a>
	</div>
</div>


<?
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/foot.php');
?>