<?php ob_start();?>
<link href="/css/carousel.css" rel="stylesheet">
<link rel="stylesheet" href="http://bootstraptema.ru/plugins/2015/bootstrap3/bootstrap.min.css" />

<link rel="stylesheet" href="http://bootstraptema.ru/plugins/font-awesome/4-4-0/font-awesome.min.css" />

<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;
}
#openFiler{
	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` WHERE `id`='".$_GET['id']."'");
if($query->num_rows == 0){
	header('location:/main');
}
$product = $query->fetch_assoc();
$cat = $db->query("SELECT * FROM `categories` WHERE `id`='".$product['id_categorie']."'")->fetch_assoc();
?>
<div class="container theme-showcase" role="main">
<div class="jumbotron">
  		<h1><?=output($product['name'])?></h1>
  		<div class="row">
				<?
				$query = $db->query("SELECT * FROM `products_images` WHERE `id_product`=".$_GET['id']."");
				while($img = $query->fetch_assoc()){
					?>
							<div class="col-xs-2 col-md-2">
		    					<div class="thumbnail">
		      						<a href="/files/images/products/<?=output($img['image'])?>">
										<img src="/files/images/products/<?=output($img['image'])?>">
									</a>
		    					</div>
		  					</div>					<?
				}?>
		</div>
  		<p>
  			<h3>Стоимость <?=$product['price']?> руб.</h3>
			<?if(isUser()){?>
				<a class="btn btn-primary btn-lg" href="/cart/add/<?=$product['id']?>" role="button">Добавить в корзину <span class="badge"><?=$product['in_stock']?> шт. в наличии</span></a>
			<?}?>
			<?if($user['admin'] >= 2){
				?>
				<a class="btn btn-warning btn-lg" href="/admin/products/edit/<?=$_GET['id']?>" role="button">Редактировать товар</a>
				<a class="btn btn-danger btn-lg" href="/admin/products/delete/<?=$_GET['id']?>" role="button">Удалить товар</a>
				<?
			}?>
  		</p>
  	</div>


	<div class="row">
		<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>
		<ul class="nav nav-pills nav-justified">
		  <li role="presentation" ><a href="/product/<?=$_GET['id']?>">Описание</a></li>
		  <li role="presentation" class="active"><a href="#" style="border-radius: 0px;">Комментарии <span class="badge"><?=$db->query("SELECT `id` FROM `products_comments` WHERE `id_product`=".$_GET['id']."")->num_rows?></span></a></li>
		  <li role="presentation"><a href="/product/<?=$_GET['id']?>/reviews">Отзывы <span class="badge"><?=$db->query("SELECT `id` FROM `products_reviews` WHERE `id_product`=".$product['id']."")->num_rows?></span></a></li>
		</ul>

		<div class="panel panel">
		  <div class="panel-body comments">
		  	<?php
			if(isset($_POST['send']) && isset($user['id'])){
				$_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/comments/'.$name);
							$db->query("INSERT INTO `products_comments` SET `id_user`='".$user['id']."', `id_product`='".$_GET['id']."', `comm`='".$_POST['text_comm']."', `date`='".time()."'");
							$last = $db->insert_id;
							$db->query("INSERT INTO `products_comments_images` SET `id_comm`='".$last."', `image`='".$name."'");
							header('location:/product/'.$product['id'].'/comments/?success#formcomm');
						}
					}else{
						$db->query("INSERT INTO `products_comments` SET `id_user`='".$user['id']."', `id_product`='".$_GET['id']."', `comm`='".$_POST['text_comm']."', `date`='".time()."'");
						header('location:/product/'.$product['id'].'/comments/?success#formcomm');
					}
					
				}
			}
			if(isset($_GET['success'])){
				?>
				<div class="alert alert-success" role="alert"><span class="glyphicon glyphicon-ok-sign" aria-hidden="true"></span> Комментарий успешно отправлен!</div>
			<?}?>
			<?if(isset($user['id'])){?>
			  	<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">
 
 	<?php
 	$Pagination = new Pagination("SELECT * FROM `products_comments` WHERE `id_product`='".$_GET['id']."' AND `reply`=0", $_GET['page']);
 	$query = $db->query("SELECT * FROM `products_comments` WHERE `id_product`='".$_GET['id']."' AND `reply`=0 LIMIT $Pagination->start, $Pagination->end");
 	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']?>/comments/<?=$comm['id']?>/edit"><small class="btn btn-primary btn-xs"><i class="glyphicon glyphicon-pencil"></i> Редактировать</small></a>
	 						<a href="/product/<?=$product['id']?>/comments/<?=$comm['id']?>/del"><small class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-remove"></i> Удалить</small></a>
 						<? } ?>
 					</span>
 					<p>
						<?=output($comm['comm'])?>
						<? $imgq = $db->query("SELECT * FROM `products_comments_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/comments/<?=output($img['image'])?>" class="thumbnail">
								      <img src="/files/images/comments/<?=output($img['image'])?>">
								    </a>
								</div>
								<?
							}
						}?>
					</p>
 				</div>
 				<div class="clearfix"></div>
 			</div>
 			<?
 			$qreply = $db->query("SELECT * FROM `products_comments` WHERE `reply`='".$comm['id']."'");
 			if($qreply->num_rows != 0){
 				?>
 				 <ul class="media-list">
 				 	<?
 				 	while($rcomm = $qreply->fetch_assoc()){
 				 		$rus = $db->query("SELECT * FROM `users` WHERE `id`='".$rcomm['id_user']."'")->fetch_assoc();
 				 		?>
 				 		<li class="media">
 							<div class="comment">
 								<a href="/user/<?=$rus['id']?>" class="pull-left">
 									<?if(file_exists($_SERVER["DOCUMENT_ROOT"].'/files/images/avatars/'.$rus['id'].'.jpg')){
 									?>
 										<img src="/files/images/avatars/<?=$rus['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/<?=$rus['id']?>" class="text-success" style="text-decoration: none;"><?=output($rus['name'].' '.$rus['surname'])?></a></strong>
 									<span class="text-muted">
 										<small class="text-muted"><?=datef($rcomm['date'])?></small>
 									</span>
 									<span class="text-muted pull-right">
 										<? if(isset($user['id'])){ 
 											if($user['id'] != $rcomm['id_user']){ ?>
	 											<a href="/product/<?=$product['id']?>/comments/<?=$rcomm['id']?>/reply"><small class="btn btn-default btn-xs"><i class="glyphicon glyphicon-comment"></i> Ответить</small></a>
	 										<? } ?>
	 										<a href="/product/<?=$product['id']?>/comments/<?=$rcomm['id']?>/edit"><small class="btn btn-primary btn-xs"><i class="glyphicon glyphicon-pencil"></i> Редактировать</small></a>
	 										<a href="/product/<?=$product['id']?>/comments/<?=$rcomm['id']?>/del"><small class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-remove"></i> Удалить</small></a>
	 									<? } ?>
 									</span>
 									<p>
										<?=output($rcomm['comm'])?>
										<? $imgq = $db->query("SELECT * FROM `products_comments_images` WHERE `id_comm`=".$rcomm['id']."");
										if($imgq->num_rows != 0){
											while($img = $imgq->fetch_assoc()){
												?>
												<div class="col-xs-6 col-md-3">
												    <a href="/files/images/comments/<?=output($img['image'])?>" class="thumbnail">
												      <img src="/files/images/comments/<?=output($img['image'])?>">
												    </a>
												</div>
												<?
											}
										}?>
 									</p>
 								</div>
 								<div class="clearfix"></div>
 							</div>
 						</li>
 				 		<?if(isset($_GET['reply']) AND ($_GET['reply'] == $comm['id'] OR $_GET['reply'] == $rcomm['id'])){
 				 			$reply_comm = $db->query("SELECT * FROM `products_comments` WHERE `id`='".$_GET['reply']."'")->fetch_assoc();
 				 			$reply_us = $db->query("SELECT * FROM `users` WHERE `id`='".$reply_comm['id_user']."'")->fetch_assoc();
 				 			if($reply_us['id'] != $user['id']){
 				 				if(isset($_POST['send_reply'])){
 				 					$_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($reply_comm['reply'] == 0){
											$replyc = $reply_comm['id'];
										}else{
											$replyc = $reply_comm['reply'];
										}
										if(file_exists($_FILES['filer']['tmp_name'])){
											$types = explode(',', 'png,jpg,jpeg,bmp,ico,JPG');
											$upfiletype = substr($_FILES['filer']['name'], strrpos($_FILES['filer']['name'], ".") + 1);
											if($_FILES['filer']['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['filer']['tmp_name'], $_SERVER["DOCUMENT_ROOT"].'/files/images/comments/'.$name);
												$db->query("INSERT INTO `products_comments` SET `id_user`='".$user['id']."', `id_product`='".$_GET['id']."', `comm`='".$_POST['text_comm']."', `date`='".time()."', `reply`='".$replyc."'");
												$last = $db->insert_id;
												$db->query("INSERT INTO `products_comments_images` SET `id_comm`='".$last."', `image`='".$name."'");
												header('location:/product/'.$product['id'].'/comments/?success#formcomm');
											}
										}else{
											$db->query("INSERT INTO `products_comments` SET `id_user`='".$user['id']."', `id_product`='".$product['id']."', `comm`='".$_POST['text_comm']."', `date`='".time()."', `reply`='".$replyc."'");
											header('location:/product/'.$product['id'].'/comments/?success#formcomm');
										}
									}
 				 				}else{
			 				 		?>
			 				 		<li class="media">
			 				 			<div class="media-body">
			 				 				<h4 class="media-heading">Ответ на комментарий</h4>
					 				 		<form action="" method="POST" enctype="multipart/form-data">
									  			<textarea class="form-control" placeholder="Введите свой ответ" rows="2" name="text_comm"><?=output($reply_us['name'])?>, </textarea>
									  			<br>
									  			<input type="file" name="filer" id="openFiler" accept="image/*,image/jpeg">
			    								<label for="openFiler"><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_reply">Отправить</button>
									  		</form>
									  	</div>
								  	</li>
			 				 		<?
			 				 	}
		 				 	}
 				 		}
 				 	}?>

 				 </ul>
 				<?
 			}?>
		</li>
 		<?
 	}?>
		  </div>
		</div>
	</div>
</div>

<?
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/foot.php');
?>