<?php
$title = 'Удаление отзыва';
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/head.php');
$_GET['id'] = abs(intval($_GET['id']));
$query = $db->query("SELECT * FROM `products_reviews` WHERE `id`='".$_GET['id']."'");
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($review['id_product'] != $product['id']){
	header('location:/');
	// Несоответсвие ид товара и отзыва
}
if($user['admin'] < 1 && $user['id']!=$review['id_user']){
	header('location:/');
	// Недостаточно прав
}
if(isset($_GET['yes'])){
	$query = $db->query("SELECT * FROM `products_reviews_images` WHERE `id_review`=".$review['id']."");
	while($img = $query->fetch_assoc()){
		unlink($_SERVER["DOCUMENT_ROOT"].'/files/images/reviews/'.$img['image']);
		$db->query("DELETE FROM `products_reviews_images` WHERE `id`=".$img['id']."");
	}
	$db->query("DELETE FROM `products_reviews` WHERE `id`=".$review['id']."");
	header('location:/product/'.$review['id_product'].'/reviews');
}
?>
<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><a href="/product/<?=$product['id']?>/reviews">Отзывы</a></li>
      <li class="active">Удаление</li>
    </ol>
    </div>
  		<p>Вы действительно хотите удалить отзыв пользователя <?=outnick($review['id_user'])?> под товаром <?=outputNameProduct($review['id_product'], 1)?>
  			<h3>Достоинства:</h3>
  			<div class="well">
  				<?=output($review['plus'])?>
  			</div>
  			<h3>Недостатки:</h3>
  			<div class="well">
  				<?=output($review['minus'])?>
  			</div>
  			<h3>Комментарий:</h3>
  			<div class="well">
  				<?=output($review['text'])?>
  			</div>
  			<?
  			$query = $db->query("SELECT * FROM `products_reviews_images` WHERE `id_review`=".$review['id']."");
  			if($query->num_rows!=0){?>
  				<div class="row"><h3>Прикреплённые изображения:</h3>
  				<?
  				while($img = $query->fetch_assoc()){
  					?>
					<div class="col-xs-6 col-md-3">
		    			<div class="thumbnail">
		      				<a href="/files/images/reviews/<?=output($img['image'])?>">
								<img src="/files/images/reviews/<?=output($img['image'])?>">
							</a>
		    			</div>
		  			</div>  					
		  			<?
  				}?></div>
  			<?}?>
  			

  		</p>

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