<?php
//include($_SERVER['DOCUMENT_ROOT']. '/feedback/Img.php');
$limit = 0;
if(isset($_GET['page'])) $limit = ($page-1)*10;
	$sql = ($admin == 'admin') ? "SELECT * FROM `feedback` WHERE `parent_id`=0 ORDER BY `id` DESC LIMIT ".$limit.',10' : 
								"SELECT * FROM `feedback` WHERE `active`=1 AND `parent_id`=0 ORDER BY `id` DESC LIMIT ".$limit.',10';
	$res = mysql_query($sql);
	while($row = mysql_fetch_assoc($res)){
		$style = '';
		if($admin == 'admin' && !$row['active']) $style = 'f_not';
?>
	<div class="well <?=$style?> feed_<?=$row['id']?>" style="margin-bottom: 4px;">
	<span>
		
			<div class="h4" style="margin-bottom: 5px;">
				<span><span><?=$row['name']?></span></span>
				<span style="font-size: 11px;" itemprop="datePublished" content="<?=$row['date']?>"><?=revertDate($row['date'])?></span>
				<? if($admin == 'admin' && $row['email']) echo '<span style="font-size:12px"> ['.$row['email'].']</span>'; ?>
			</div>

		<span><?=$row['text']?></span>
		
		<div>
			<?php
				$res2 = mysql_query("SELECT * FROM `feedback_images` WHERE `feedback_id`='".$row['id']."' ");
				while($row2 = mysql_fetch_assoc($res2)){ 
					if(!file_exists($_SERVER['DOCUMENT_ROOT']. '/feedback/images/'.$row2['name'])) continue;
					$img = Img::get('/feedback/images/'.$row2['name'], array('width'=>112, 'height'=>112));
				?>
					<div class="f-preview" style="margin-top: 5px;">
						<a href="/feedback/images/<?=$row2['name']?>" rel="prettyPhoto[gal<?=$row['id']?>]"><img src="<?=$img?>"></a></div>
				<? } ?>
		</div>
		<div class="clearfix"></div>
	
	</span>
<?php
	if($admin == 'admin'){
		echo '<br />';
?>
		<a class="btn btn-primary btn-mini" style="padding: 3px;font-size: 11px;" href="/feedback/edit.php?id=<?=$row['id']?>">Изменить</a>
		<a class="btn btn-warning btn-mini" style="padding: 3px;font-size: 11px;" href="/feedback/reply.php?id=<?=$row['id']?>">Ответить</a>
<?php
		if(!$row['active']){
?>
		<button class="btn btn-success btn-mini f_activate" style="padding: 3px;font-size: 11px;" data-id="<?=$row['id']?>">Включить</button>
<?php
		}
?>
		<button class="btn btn-danger btn-mini f_remove" style="padding: 3px;font-size: 11px;" data-id="<?=$row['id']?>">Удалить</button>
<?php
	}
?>		
	</div>
	
	
	
	
	
	
	
	
	
	
	<?php
			$res3 = mysql_query("SELECT * FROM `feedback` WHERE `parent_id`='".$row['id']."' ORDER BY `id` ");
			while($row3 = mysql_fetch_assoc($res3)){
		?>
				<div class="well feed_<?=$row3['id']?>" style="margin-bottom: 4px;margin-left:60px">
					<span>
						
							<div class="h4" style="margin-bottom: 5px;">
								<span><span>Ответ</span></span>
								<span style="font-size: 11px;" itemprop="datePublished" content="<?=$row3['date']?>"><?=revertDate($row['date'])?></span>
							</div>
				
						<span><?=$row3['text']?></span>
						
						<div>
							<?php
								$res2 = mysql_query("SELECT * FROM `feedback_images` WHERE `feedback_id`='".$row3['id']."' ");
								while($row2 = mysql_fetch_assoc($res2)){ 
									if(!file_exists($_SERVER['DOCUMENT_ROOT']. '/feedback/images/'.$row2['name'])) continue;
									$img = Img::get('/feedback/images/'.$row2['name'], array('crop'=>true));
								?>
									<div class="f-preview" style="margin-top: 5px;">
										<a href="/feedback/images/<?=$row2['name']?>" rel="prettyPhoto[gal<?=$row3['id']?>]"><img src="<?=$img?>"></a></div>
								<? } ?>
						</div>
						<div class="clearfix"></div>
					
					</span>
				<?php
					if($admin == 'admin'){
						echo '<br />';
				?>
						<a class="btn btn-primary btn-mini" style="padding: 3px;font-size: 11px;" href="/feedback/edit.php?id=<?=$row3['id']?>">Изменить</a>
						<button class="btn btn-danger btn-mini f_remove" style="padding: 3px;font-size: 11px;" data-id="<?=$row3['id']?>">Удалить</button>
				<?php
					}
				?>		
					</div>
			
			
			
		<?php } ?>
	
	
<?php
	}
	
	
	
	
	
	
	
	
	
$sql = ($admin == 'admin') ? "SELECT `id` FROM `feedback` WHERE `parent_id`=0" : 
								"SELECT id FROM `feedback` WHERE `active`=1 AND `parent_id`=0";
$all = mysql_num_rows( mysql_query($sql) );
if($all > 10){
	echo '<ul class="pagination">';
	$page = isset($_GET['page']) ? $_GET['page'] : 1;
	
	$pages = ceil($all / 10);
	for($i=1; $i<=$pages; ++$i){
		$url = ($i == 1) ? '/guestbook/' : '/guestbook/?page='.$i;
		echo ($i != $page) ? '<li><a href="'.$url.'">'.$i.'</a></li>' : '<li class="active"><a href="#">'.$i.'</a></li>';
	}
	echo '</ul>';
}


function revertDate($date){
	$d = new DateTime($date);
	return $d->format('d-m-Y');
}

if($admin == 'admin') include $_SERVER['DOCUMENT_ROOT'].'/feedback/feedback_admin.js.php'; 