<?php
	function getRegionHTML($regionName, $gridName, $regionBeschreibung, $regionLink, $regionID, $regionBild, $onlineUser)
	{
		return '<div class="col-lg-2 col-md-6 d-flex align-items-stretch"><div class="card" style="width: 18rem; min-height:300px"><img class="card-img-top" style="max-height: 150px;" src="./data/RegionImages/'.$regionBild.'" alt="'.$regionName.'"><div class="card-body"><h5 class="card-title">'.$regionName.'</h5><h6 class="card-subtitle mb-2 text-muted">'.$onlineUser.' Besucher Online</h6><p class="card-text">'.$regionBeschreibung.'</p></div><div class="card-footer text-muted"><a href="index.php?page=regioninfo&uuid='.$regionID.'" class="card-link">Mehr Infos</a><a href="'.$regionLink.'" class="card-link">Teleport</a></div></div></div>';
	}
		
	$HTML = new HTML();
	$HTML->setHTMLTitle("4Creative Region Guide");
	$HTML->importHTML("style/default/services.html");

	$statement = $RUNTIME['PDO']->prepare("SELECT * FROM Regions WHERE lastSeen > ? ORDER BY OnlineUser ASC LIMIT 0, 30");
	$statement->execute(array(time() - 320)); 

	if($statement->rowCount() == 0)
	{
		$HTML->ReplaceLayoutInhalt("%%RegionList%%", "Es sind keine Regionen Online!"); 
	}else{
		$elementNr = 0;
		$rawHTML = "";
		
		while($row = $statement->fetch()) 
		{
			if($elementNr == 0)
			{
				$rawHTML .= '<div class="row" style="padding-bottom:20px">';
			}

			$rawHTML .= getRegionHTML(substr($row['RegionName'], 0, 20), substr($row['GridName'], 0, 20), substr($row['RegionBeschreibung'], 0, 60), $row['RegionLink'], $row['LastSeen'], $row['ImageName'], $row['OnlineUser']);
			$elementNr++;

			if($elementNr == 6)
			{
				$rawHTML .= '</div>';
				$elementNr = 0;
			}
		}
	
		$HTML->ReplaceLayoutInhalt("%%RegionList%%", $rawHTML); 
	}

	$HTML->build();
	echo $HTML->ausgabe();
?>