<?=ob_start()?>
<style>
.comments ul ul {
 margin-left: 60px;
}
.comments .comment img {
 margin-right: 20px;
}
.comments .comment {
 padding: 6px;
}
.comments .comment:hover {
 background: #eee;
}
#openFile{
	display:none;
}
</style>
<?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();
$query = $db->query("SELECT * FROM `products` WHERE `id`=".$_GET['id_product']."");
if($query->num_rows == 0){
	header('location:/');
	// Подмена id товара
}
$product = $query->fetch_assoc();
$cat = $db->query("SELECT * FROM `categories` WHERE `id`='".$product['id_categorie']."'")->fetch_assoc();
if($product['id'] != $review['id_product']){
	header('location:/');
	// Не соответствие номеров
}
?>
<div class="container theme-showcase" role="main">
	<div class="jumbotron">
		<h1>Комментарии к отзыву</h1>
		<div class="row">
			<p>
				<div class="panel panel-success">
					<div class="panel-heading">
						<h3 class="panel-title">Достоинства</h3>
					</div>
					<div class="panel-body">
						<?=output($review['plus'])?>
					</div>
				</div>
				<div class="panel panel-danger">
					<div class="panel-heading">
						<h3 class="panel-title">Недостатки</h3>
					</div>
					<div class="panel-body">
						<?=output($review['minus'])?>
					</div>
				</div>
				<div class="panel panel-info">
					<div class="panel-heading">
						<h3 class="panel-title">Комментарий</h3>
					</div>
					<div class="panel-body">
						<?=output($review['text'])?>
					</div>
				</div>

				<? $imgq = $db->query("SELECT * FROM `products_reviews_images` WHERE `id_review`=".$review['id']."");
				if($imgq->num_rows != 0){
					while($img = $imgq->fetch_assoc()){
						?>
						<div class="col-xs-6 col-md-3">
						    <a href="/files/images/reviews/<?=output($img['image'])?>" class="thumbnail">
						    	<img src="/files/images/reviews/<?=output($img['image'])?>">
							</a>
						</div>
						<?
					}
				}?>
			</p>
		</div>
	</div>
	<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>
	<?
	if(isUser()){
	if(isset($_POST['send'])){
		$_POST['text_comm'] = input($_POST['text_comm']);
		if(empty($_POST['text_comm'])){
					?>
					<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span> Введите текст комментария!</div>
					<?
				}else{
					if(file_exists($_FILES['file']['tmp_name'])){
						$types = explode(',', 'png,jpg,jpeg,bmp,ico,JPG');
						$upfiletype = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], ".") + 1);
						if($_FILES['file']['size'] > (1048576 * 5)){
							?>
							<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span> Размер выгружаемого файла, не должен превышать 5 мб!</div>
							<?
						}elseif(!in_array($upfiletype, $types)){
							?>
							<div class="alert alert-danger" role="alert"><span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span> Файл такого типа выгружать запрещено!</div>
							<?
						}else{
							$name = md5($product['id'] + time()).'.'.$upfiletype;
							copy($_FILES['file']['tmp_name'], $_SERVER["DOCUMENT_ROOT"].'/files/images/reviews/comments/'.$name);
							$db->query("INSERT INTO `products_reviews_comm` SET `id_review`=".$review['id'].", `id_user`=".$user['id'].", `date`=".time().", `text`='".$_POST['text_comm']."'");
							$last = $db->insert_id;
							$db->query("INSERT INTO `products_reviews_comm_images` SET `id_comm`='".$last."', `image`='".$name."'");
							header('location:/product/'.$product['id'].'/reviews/'.$review['id'].'/comments');
						}
					}else{
						$db->query("INSERT INTO `products_reviews_comm` SET `id_review`=".$review['id'].", `id_user`=".$user['id'].", `date`=".time().", `text`='".$_POST['text_comm']."'");
						header('location:/product/'.$product['id'].'/reviews/'.$review['id'].'/comments');
					}
				}
	}
	?>
	<div class="row">
		<div class="panel panel">
			<div class="panel-body comments">
					<form action="#formcomm" method="POST" id="formcomm" enctype="multipart/form-data">
				    	<textarea class="form-control" placeholder="Оставьте Ваш комментарий" rows="5" name="text_comm"></textarea>
				    	<br>
				    	<input type="file" name="file" id="openFile" accept="image/*,image/jpeg">
				    	<label for="openFile"><small class="btn btn-default btn-xs"><i class="glyphicon glyphicon-camera"></i> Прикрепить изображение</small></label>
				    	<br>
				    	<button type="submit" class="btn btn-info pull-right" name="send">Отправить</button>
					</form>
			<?}?>
		<div class="clearfix"></div>
 		<hr>
 		<ul class="media-list">
 			<?
 			$query = $db->query("SELECT * FROM `products_reviews_comm` WHERE `id_review`=".$review['id']."");
 			while($comm = $query->fetch_assoc()){
 				$us = $db->query("SELECT * FROM `users` WHERE `id`=".$comm['id_user']."")->fetch_assoc();
 				?>
 				<li class="media">
 			<div class="comment">
 				<a href="/user/<?=$us['id']?>" class="pull-left">
 					<?if(file_exists($_SERVER["DOCUMENT_ROOT"].'/files/images/avatars/'.$us['id'].'.jpg')){
 						?>
 						<img src="/files/images/avatars/<?=$us['id']?>.jpg" alt="" class="img-circle" width="60" height="60">
 						<?
 					}else{?>
 						<img src="/files/images/avatars/no_avatar.png" alt="" class="img-circle" width="60" height="60">
 					<?}?>
 				</a>
 				<div class="media-body">
 					<strong class="text-success"><a href="/user/<?=$us['id']?>" class="text-success" style="text-decoration: none;"><?=output($us['name'].' '.$us['surname'])?></a></strong>
 					<span class="text-muted">
 						<small class="text-muted"><?=datef($comm['date'])?></small>
 					</span>
 					<span class="text-muted pull-right">
 						<?
 						if(isset($user['id'])){
	 						if($user['id'] != $comm['id_user']){?>
	 							<a href="/product/<?=$product['id']?>/comments/<?=$comm['id']?>/reply"><small class="btn btn-default btn-xs"><i class="glyphicon glyphicon-comment"></i> Ответить</small></a>
	 						<? } ?>
	 						<a href="/product/<?=$product['id']?>/reviews/<?=$review['id']?>/comments/<?=$comm['id']?>/edit"><small class="btn btn-primary btn-xs"><i class="glyphicon glyphicon-pencil"></i> Редактировать</small></a>
	 						<a href="/product/<?=$product['id']?>/reviews/<?=$review['id']?>/comments/<?=$comm['id']?>/delete"><small class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-remove"></i> Удалить</small></a>
 						<? } ?>
 					</span>
 					<p>
						<?=output($comm['text'])?>
						<? $imgq = $db->query("SELECT * FROM `products_reviews_comm_images` WHERE `id_comm`=".$comm['id']."");
						if($imgq->num_rows != 0){
							while($img = $imgq->fetch_assoc()){
								?>
								<div class="col-xs-6 col-md-3">
								    <a href="/files/images/reviews/comments/<?=output($img['image'])?>" class="thumbnail">
								      <img src="/files/images/reviews/comments/<?=output($img['image'])?>">
								    </a>
								</div>
								<?
							}
						}?>
					</p>
 				</div>
 				<div class="clearfix"></div>
 			</div>
 		</li>
 				<?
 			}
 			?>
 		</ul>
	</div>
	</div>
	</div>
<?
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/foot.php');
?>