<?php
$title = 'Удаление комментария';
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/head.php');
$_GET['id'] = abs(intval($_GET['id']));
$query = $db->query("SELECT * FROM `products_reviews_comm` WHERE `id`='".$_GET['id']."'");
if($query->num_rows == 0){
	header('location:/');
	// Комментария не существует
}
$comm = $query->fetch_assoc();
$_GET['id_review'] = abs(intval($_GET['id_review']));
$query = $db->query("SELECT * FROM `products_reviews` WHERE `id`='".$_GET['id_review']."'");
if($query->num_rows == 0){
	header('location:/');
	// Отзыва не существует
}
$review = $query->fetch_assoc();
$_GET['id_product'] = abs(intval($_GET['id_product']));
$query = $db->query("SELECT * FROM `products` WHERE `id`=".$_GET['id_product']."");
if($query->num_rows == 0){
	header('location:/');
	// Товара не существует
}
$product = $query->fetch_assoc();
$cat = $db->query("SELECT * FROM `categories` WHERE `id`='".$product['id_categorie']."'")->fetch_assoc();
if($user['admin'] < 1 && $comm['id_user'] != $user['id']){
	header('location:/');
	// Нет прав
}
if(isset($_GET['yes'])){
	$query = $db->query("SELECT * FROM `products_reviews_comm_images` WHERE `id_comm`=".$_GET['id']."");
	while($img = $query->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']."");
	header('location:/product/'.$product['id'].'/reviews/'.$review['id'].'/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']?>/reviews">Отзывы</a></li>
      <li><a href="/product/<?=$product['id']?>/reviews/<?=$_GET['id_review']?>/comments">Комментарии</a></li>
      <li class="active">Удаление</li>
    </ol>
  		<p>Вы действительно хотите удалить комментарий пользователя <?=outnick($comm['id_user'])?> под товаром <?=outputNameProduct($review['id_product'], 1)?>
  			<div class="well">
  				<?=output($comm['text'])?>
  			</div>
  		</p>

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