<?php
$title = 'Удаление товара';
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/head.php');
admin(3);
$_GET['id'] = abs(intval($_GET['id']));
$query = $db->query("SELECT * FROM `products` WHERE `id`='".$_GET['id']."'");
if($query->num_rows==0){
	header('location:/admin');
}
$product = $query->fetch_assoc();
$cat = $db->query("SELECT * FROM `categories` WHERE `id`='".$product['id_categorie']."'")->fetch_assoc();
if(isset($_GET['yes'])){
	$query = $db->query("SELECT * FROM `products_comments` WHERE `id_product`=".$_GET['id']."");
	while($comm = $query->fetch_assoc()){
		$imgq = $db->query("SELECT * FROM `products_comments_images` WHERE `id_comm`=".$comm['id']."")->fetch_assoc();
		while($img = $imgq->fetch_assoc()){
			unlink($_SERVER["DOCUMENT_ROOT"].'/files/images/comments/'.$img['image']);
			$db->query("DELETE FROM `products_comments_images` WHERE `id`=".$img['id']."");
		}
		$db->query("DELETE FROM `products_comments` WHERE `id`=".$comm['id']."");
	}


	$query = $db->query("SELECT * FROM `products_reviews` WHERE `id_product`=".$product['id']."");
	while($review = $query->fetch_assoc()){
		$commq = $db->query("SELECT * FROM `products_reviews_comm` WHERE `id_review`=".$review['id']."");
		while($comm = $commq->fetch_assoc()){
			$imgq = $db->query("SELECT * FROM `products_reviews_comm_images` WHERE `id_comm`=".$comm['id']."");
			while($img = $imgq->fetch_assoc()){
				unlink($_SERVER["DOCUMENT_ROOT"].'/files/images/reviews/comments/'.$img['image']);
				$db->query("DELETE FROM `products_reviews_comm_images` WHERE `id`=".$img['id']."");
			}
			$db->query("DELETE FROM `products_reviews_comm` WHERE `id`=".$comm['id']."");
		}
		$imgqq = $db->query("SELECT * FROM `products_reviews_images` WHERE `id_review`=".$review['id']."");
		while($imgg = $imgqq->fetch_assoc()){
			unlink($_SERVER["DOCUMENT_ROOT"].'/files/images/reviews'.$imgg['image']);
			$db->query("DELETE FROM `products_reviews_images` WHERE `id`=".$imgg['id']."");
		}
		$db->query("DELETE FROM `products_reviews_rating` WHERE `id_review`=".$review['id']."");
		$db->query("DELETE FROM `products_reviews` WHERE `id`=".$review['id']."");
	}
	$db->query("DELETE FROM `products_images` WHERE `id_product`='".$_GET['id']."'");
	$db->query("DELETE FROM `products` WHERE `id`='".$_GET['id']."'");
	header('location:/categorie/'.$product['id_categorie']);
}elseif(isset($_POST['no'])){
	header('location:/product/'.$_GET['id']);
}
?>
<div class="container theme-showcase" role="main">
	<div class="jumbotron" style="background-color: white;">
		<h1>Удаление товара</h1>
		<div class="page-header">
			<ol class="breadcrumb">
				<li><a href="/categories">Разделы</a></li>
				<?
		  			if($cat['podkat'] != 0){
		  				$tcat = $cat['podkat'];
		  				while($tcat != 0){
		  					$kcat = $db->query("SELECT * FROM `categories` WHERE `id`='".$tcat."'")->fetch_assoc();
		  					?>
		  					<li><a href="/categories/<?=$kcat['id']?>"><?=output($kcat['name'])?></a></li>
		  					<?php
		  					$tcat = $kcat['podkat'];
		  				}
		  			}
		  			?>
		  		<li><a href="/categories/<?=$cat['id']?>"><?=output($cat['name'])?></a></li>
				<li class><a href="/product/<?=$product['id']?>"><?=output($product['name'])?></a></li>
				<li class="active">Удаление</li>
			</ol>
		</div>
		<p>
			Вы действительно хотите удалить товар "<b><?=outputNameProduct($product['id'], 1)?></b>"?<br>
			<p><a class="btn btn-danger btn-lg" href="?yes" role="button">Удалить</a> <a class="btn btn-primary btn-lg" href="/product/<?=$product['id']?>" role="button">Отмена</a></p>
		</p>
	</div>
</div>
<?
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/foot.php');
?>