<?php
$title = 'Просмотр разделов';
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/head.php');
$_GET['cat'] = abs(intval($_GET['cat']));
if(empty($_GET['cat']))
	$_GET['cat'] = 0;
$query = $db->query("SELECT * FROM `categories` WHERE `id`='".$_GET['cat']."'");
if($query->num_rows == 0 && $_GET['cat']!=0){
	header('location:/categories');
}
$categorie = $query->fetch_assoc();
?>
<div class="container theme-showcase" role="main">
	<div class="page-header">
		<h1><?=output($categorie['name'])?></h1>
		<?php if($_GET['cat'] != 0){?>
			<ol class="breadcrumb">
	  			<li><a href="/categories">Разделы</a></li>
	  			<?
	  			if($categorie['podkat'] != 0){
	  				$tcat = $categorie['podkat'];
	  				while($tcat != 0){
	  					$cat = $db->query("SELECT * FROM `categories` WHERE `id`='".$tcat."'")->fetch_assoc();
	  					?>
	  					<li><a href="/categories/<?=$cat['id']?>"><?=output($cat['name'])?></a></li>
	  					<?php
	  					$tcat = $cat['podkat'];
	  				}
	  			}
	  			?>
	  			<li class="active"><?=output($categorie['name'])?></li>
			</ol>
		<?}else{
			?>
			<h1>Разделы</h1>
			<?
		}?>
		<?
		if($db->query("SELECT * FROM `products` WHERE `id_categorie`=".$_GET['cat']."")->num_rows == 0 && $user['admin'] >= 3){
			?>
			<a href="/admin/categories/add/<?=$_GET['cat']?>"><button class="btn btn-primary">Создать раздел</button></a>
			<?
		}
		if($db->query("SELECT * FROM `categories` WHERE `podkat`=".$_GET['cat']."")->num_rows == 0 && $user['admin'] >= 3){
			?>
			<a href="/admin/products/add/<?=$_GET['cat']?>"><button class="btn btn-primary">Добавить товар</button></a>
			<?
		}
		?>
	</div>
<div class="row">
<?php
// Вывод категорий
$query = $db->query("SELECT * FROM `categories` WHERE `podkat`='".$_GET['cat']."'");
if($query->num_rows != 0){
	while($cat = $query->fetch_assoc()){
		$c = $db->query("SELECT `id` FROM `products` WHERE `id_categorie`=".$cat['id']."")->num_rows;
		?>
	  	<div class="col-sm-6 col-md-4">
	    	<div class="thumbnail">
	      		<img src="/files/images/categories/<?=$cat['id']?>.jpg?id=<?=rand(1,100)?>" alt="<?=output($cat['name'])?>">
	      		<div class="caption">
	        		<h3><?=output($cat['name'])?></h3>
	        		<p><?if($c != 0){ echo $c.' товаров';}?></p>
	        		<p>
	        			<a href="/categories/<?=$cat['id']?>" class="btn btn-primary" role="button">Перейти</a>
	        			<?if($user['admin'] >= 3){?>
	        				<a href="/admin/categories/<?=$cat['id']?>/delete/" class="btn btn-danger" role="button">Удалить</a>
	        				<a href="/admin/categories/<?=$cat['id']?>/edit/" class="btn btn-warning" role="button">Редактировать</a>
	        			<?}?>
	        		</p>
	      		</div>
	    	</div>
	  	</div>
		<?php
	}
}else{
	// Вывод товаров
	$Pagination = new Pagination("SELECT * FROM `products` WHERE `id_categorie`='".$categorie['id']."'", $_GET['page']);
	$query = $db->query("SELECT * FROM `products` WHERE `id_categorie`='".$categorie['id']."' LIMIT $Pagination->start, $Pagination->end");
	while ($product = $query->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>
		<?
	}
	//$Pagination->out('/categories/'.$categorie['id']); 
}
?>
</div>
</div>
<?php
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/foot.php');
?>