<?php
$title = 'Поиск по сайту';
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/head.php');
?>
<div class="container theme-showcase" role="main">
	<div class="jumbotron">
		<h1>Поиск по сайту</h1>
		<p>Ниже представлены результаты поиска по запросу "<?=output($_POST['text'])?>"</p>
	</div>
	<?
	if(isset($_POST['text']) AND !empty($_POST['text'])){
	$_POST['text'] = input($_POST['text']);
	$categories = $db->query("SELECT * FROM `categories` WHERE `name` LIKE '%".$_POST['text']."%'");
	if($categories->num_rows != 0){?>
		<div class="page-header">
			<h1>Разделы</h1>
		</div>
		<div class="row">
			<?
			while($cat = $categories->fetch_assoc()){
				?>
				
					<div class="col-sm-6 col-md-4">
				    	<div class="thumbnail">
				      		<img src="/files/images/categories/<?=$cat['id']?>.jpg" alt="<?=$cat['name']?>">
				      		<div class="caption">
				        		<h3><?=output($cat['name'])?></h3>
				        		<p><a href="/categories/<?=$cat['id']?>" class="btn btn-primary" role="button">Перейти</a>
				      		</div>
				    	</div>
				  	</div>
				<?
			}
			?>
		</div>
		<?
	}
	$products = $db->query("SELECT * FROM `products` WHERE `name` LIKE '%".$_POST['text']."%' OR `description` LIKE '%".$_POST['text']."%'");
	if($products->num_rows != 0){
		?>
		<div class="page-header">
			<h2>Товары</h2>
		</div>
		<div class="row">
			<?
			while($product = $products->fetch_assoc()){
				$img = $db->query("SELECT * FROM `products_images` WHERE `id_product`=".$product['id']." LIMIT 0, 1")->fetch_assoc();
				?>
					<div class="col-sm-6 col-md-4">
				    	<div class="thumbnail">
	      					<img src="/files/images/products/<?=$img['image']?>" alt="<?=output($product['name'])?>" width="40%" height="60%">
				      		<div class="caption">
				        		<h3><a href="/product/<?=$product['id']?>"><?=output($product['name'])?></a></h3>
				        		<p>Стоимость: <?=$product['price']?></p>
				  				<p>В наличии: <?=$product['in_stock']?></p>
				        		<p><a href="/product/<?=$product['id']?>" class="btn btn-primary" role="button">Перейти</a>
				      		</div>
				    	</div>
				  	</div>
				<?
			}?>
		</div>
		<?
	}
	$users = $db->query("SELECT * FROM `users` WHERE `name` LIKE '%".$_POST['text']."%' OR `surname` LIKE '%".$_POST['text']."%'");
	if($users->num_rows != 0){
		?>
		<div class="page-header">
			<h2>Пользователи</h2>
		</div>
		<div class="row">
			<?
			while($us = $users->fetch_assoc()){
				?>
					<div class="well well-lg"><a href="/user/<?=$us['id']?>"><?=output($us['name'])?> <?=output($us['surname'])?></a></div>
				<?
			}?>
		</div>
		<?
	}
	if($categories->num_rows == 0 AND $products->num_rows == 0 AND $users->num_rows == 0){
		?>
		<div class="row">
			<div class="alert alert-danger" role="alert">
				<p>По Вашему запросу ничего не найдено!</p>
			</div>
		</div>
		<?
	}
}else{
	?>
	<div class="row">
		<div class="alert alert-danger" role="alert">
			<p>Введите текст запроса</p>
		</div>
	</div>
	<?
}
?>
</div>
<?
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/foot.php');
?>