<?php
$title = 'Удаление комментария';
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/head.php');
$_GET['id'] = abs(intval($_GET['id']));
$query = $db->query("SELECT * FROM `products_comments` WHERE `id`='".$_GET['id']."'");
if($query->num_rows == 0){
	header('location:/');
}
$comm = $query->fetch_assoc();
if($user['id'] != $comm['id_user'] && $user['admin']==0){
	header('location:/');
}
$product = $db->query("SELECT * FROM `products` WHERE `id`=".$comm['id_product']."")->fetch_assoc();
$cat = $db->query("SELECT * FROM `categories` WHERE `id`='".$product['id_categorie']."'")->fetch_assoc();
if(isset($_GET['yes'])){
	$db->query("DELETE FROM `products_comments` WHERE `id`='".$_GET['id']."'");
	$query = $db->query("SELECT * FROM `products_comments` WHERE `reply`=".$_GET['id']."");
	while($rcomm = $query->fetch_assoc()){
		$imgq = $db->query("SELECT * FROM `products_comments_images` WHERE `id_comm`=".$rcomm['id']."");
		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`=".$rcomm['id']."");
	}
	header('location:/product/'.$comm['id_product'].'/comments');
}
?>
<div class="container theme-showcase" role="main">
	<div class="jumbotron" style="background-color: white;">
  		<h1>Удаление комментария</h1>
  		<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><a href="/product/<?=$product['id']?>/comments">Комментарии</a></li>
			<li class="active">Удаление</li>
		</ol>
  		<p>Вы действительно хотите удалить комментарий пользователя <?=outnick($comm['id_user'])?> под товаром <?=outputNameProduct($comm['id_product'], 1)?>
  			<div class="well">
  				<?=output($comm['comm'])?>
  			</div>
  		</p>

  		<p><a class="btn btn-danger btn-lg" href="?yes" role="button">Удалить</a> <a class="btn btn-primary btn-lg" href="/product/<?=$comm['id_product']?>/comments" role="button">Отмена</a></p>
  	</div>
  </div>
 <?
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/foot.php');
?>