<?php
$title = 'Оформление заказа';
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/head.php');
?>
<div class="container theme-showcase" role="main">
	<div class="page-header">
		<h1>Оформление заказа</h1>
	</div>
	<div class="row">
<?
$all_price = 0;
$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();
	$all_price += $product['price'] * $cart['col'];
}
if(isset($_POST['ok2'])){
	$_POST['number_phone'] = input($_POST['number_phone']);
	$_POST['email'] = input($_POST['email']);
	$_POST['name'] = input($_POST['name']);
	$_POST['city'] = input($_POST['city']);
	$_POST['street'] = input($_POST['street']);
	$_POST['house'] = input($_POST['house']);
	$_POST['kv'] = input($_POST['kv']);
	if(empty($_POST['number_phone'])){
		?>
		<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span> Введите номер телефона!</div>
		<?
	}elseif(empty($_POST['name'])){
		?>
		<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span> Введите имя!</div>
		<?
	}elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
		?>
		<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span> Введите E-Mail корректно!</div>
		<?
	}else{
		if($user['money'] < $all_price){
			?>
			<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span> У Вас недостаточно средств!</div>
			<?
		}else{
			$db->query("UPDATE `users` SET `money`=`money`-".$all_price." WHERE `id`='".$user['id']."'");
			$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();
				if($cart['col'] > $product['in_stock']){
					?>
					<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span> Кол-во в корзине больше чем есть по факту!</div>
					<?
				}else{
					$db->query("UPDATE `products` SET `in_stock`=`in_stock`-".$cart['col']." WHERE `id`='".$cart['id_product']."'");
					if(!empty($_POST['city'])){
						$db->query("INSERT INTO `purchased` SET `id_user`='".$user['id']."', `date`='".time()."', `id_product`='".$cart['id_product']."', `col`='".$cart['col']."', `number_phone`='".$_POST['number_phone']."', `email`='".$_POST['email']."', `name`='".$_POST['name']."', `delivery`=1, `city`='".$_POST['city']."', `street`='".$_POST['street']."', `house`='".$_POST['house']."', `kv`='".$_POST['kv']."'");
					}else{
						$db->query("INSERT INTO `purchased` SET `id_user`='".$user['id']."', `date`='".time()."', `id_product`='".$cart['id_product']."', `col`='".$cart['col']."', `number_phone`='".$_POST['number_phone']."', `email`='".$_POST['email']."', `name`='".$_POST['name']."'");
					}
					$db->query("DELETE FROM `cart` WHERE `id_user`=".$user['id']."");
					?>
					<div class="alert alert-success" role="alert"><span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span> Заказ успешно оформлен!</div>
					<?
				}
			}
		}
	}
}
if(isset($_POST['ok1'])){
	?>
	<div class="well">
		<div class="form-row">
			<div class="form-group">
				<h2>Финал</h2>
				<h3>Контактная информация:</h3>
				<form action="" method="POST">
					<label for="inputNumber">Сотовый телефон</label>
					<input type="tel" class="form-control" placeholder="Введите Ваш номер телефона" rows="5" name="number_phone" id="inputNumber">
					<label for="inputEmail">E-Mail</label>
					<input type="email" class="form-control" placeholder="Введите Ваш E-Mail" rows="5" name="email" id="inputEmail" value="<?=$user['email']?>">
					<label for="inputName">Имя</label>
					<input type="text" class="form-control" placeholder="Введите Ваше имя" rows="5" name="name" id="inputName" value="<?=$user['name']?>">
					<?
					if($_POST['method'] == "delivery"){
						?>
						<h3>Адрес доставки</h3>
						<label for="inputCity">Город</label>
						<input type="text" class="form-control" placeholder="Введите город для доставки" rows="5" name="city" id="inputCity">
						<label for="inputStreet">Улица</label>
						<input type="text" class="form-control" placeholder="Введите улицу для доставки" rows="5" name="street" id="inputStreet">
						<label for="inputHouse">Дом</label>
						<input type="text" class="form-control" placeholder="Введите дом для доставки" rows="5" name="house" id="inputHouse">
						<label for="inputKv">Квартира</label>
						<input type="text" class="form-control" placeholder="Введите квартиру для доставки" rows="5" name="kv" id="inputKv">

						<?
					}
					?>
					<br><button type="submit" class="btn btn-primary" name="ok2">Оплатить <?=$all_price?> RUB</button>
				</form>
				<?
}else{
	?>
	<div class="well">
		<div class="form-row">
			<div class="form-group">
				<h2>Выберите способ получения товара:</h2>
				<form action="" method="POST">
					<div class="form-check">
					  <input class="form-check-input" type="radio" name="method" id="methodRadio1" value="pickup" checked>
					  <label class="form-check-label" for="methodRadio1">
					    Самовывоз
					  </label>
					</div>
					<div class="form-check">
					  <input class="form-check-input" type="radio" name="method" id="methodRadio2" value="delivery">
					  <label class="form-check-label" for="methodRadio2">
					    Доставка
					  </label>
					</div>
					<button type="submit" class="btn btn-primary" name="ok1">Продолжить</button>
				</form>
			</div>
		</div>
	</div>
	<?
}
?>
</div>
<?
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/foot.php');
?>