<?php ob_start();?>
<style>
#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_comments` WHERE `id`='".$_GET['id']."'");
if($query->num_rows == 0){
	header('location:/');
}
$comm = $query->fetch_assoc();
if($comm['id_user'] != $user['id'] && $user['admin'] < 1){
	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();
?>
<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']?>/comments">Комментарии</a></li>
			<li class="active">Редактирование</li>
		</ol>
		</div>
  			<?php
			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/comments/'.$name);
							$db->query("INSERT INTO `products_comments_images` SET `id_comm`='".$_GET['id']."', `image`='".$name."'");
							header('location:/product/'.$product['id'].'/comments/');
						}
					}
					$db->query("UPDATE `products_comments` SET `comm`='".$_POST['text_comm']."' WHERE `id`='".$comm['id']."'");
					header('location:/product/'.$comm['id_product'].'/comments');
				}
			}?>
	  		<form action="" method="POST" enctype="multipart/form-data">
	  			<textarea class="form-control" placeholder="Оставьте Ваш комментарий" rows="5" name="text_comm"><?=output($comm['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-primary btn-lg" name="send">Отправить</button> <a class="btn btn-danger btn-lg" href="/product/<?=$comm['id_product']?>/comments" role="button">Отмена</a></p>
	  		</form>
	  	
		  	<?
		  	$imgq = $db->query("SELECT * FROM `products_comments_images` WHERE `id_comm`=".$comm['id']."");
		  	if($imgq->num_rows != 0){
		  		?>
		  		<h3></h3>
		  		<div class="panel panel-default">
  					<div class="panel-heading">
    					<h3 class="panel-title">Прикреплённые изображения:</h3>
  					</div>
  					<div class="panel-body">
				  		<?
				  		while($img = $imgq->fetch_assoc()){
				  			?>
							<div class="col-xs-6 col-md-4">
		    					<div class="thumbnail">
		      						<a href="/files/images/comments/<?=output($img['image'])?>">
										<img src="/files/images/comments/<?=output($img['image'])?>">
									</a>
		      						<div class="caption">
		        						<p><a href="/reviews/comments/images/delete/<?=$img['id']?>" class="btn btn-danger" role="button">Удалить</a></p>
		      						</div>
		    					</div>
		  					</div>
				  			<?
				  		}?>
				  	</div>
				</div>
			<?}?>
	  	</p>	
  	</div>
  </div>
<?
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/foot.php');
?>