<?php
	try {
		include 'config.php';

		$conn = new PDO("mysql:dbname=$MYSQL_DBNAME;host=$MYSQL_HOST", $MYSQL_USERNAME, $MYSQL_PASSWORD);
		$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	} catch(PDOException $e) {
		echo "Webseite wird momentan überarbeitet";
	}

	function fetch_text($id) {
		global $conn;
		$stmt = $conn->prepare('select text from text where id = :id LIMIT 1;');
		$stmt->bindParam(':id', $id);
		$stmt->execute();
		$result = $stmt->fetch(PDO::FETCH_ASSOC);
		return $result['text'];
	}

	function timeformat($time) {
		$t = strtotime($time);
		if(date('i', $t) != 0) {
			return strftime("%d.%m (%H.%M h)", $t);
		}
		return strftime("%d.%m (%H h)", $t);
	}

	$stmt = $conn->prepare('select DISTINCT LPAD( month(time), 2, \'0\') as `nr` from events_onwebsite');
	$stmt->execute();

	$first = true;
	$timetitle = '';
	$sitetitle = '';

	foreach($stmt->fetchAll() as $month) {
		if(!$first){
			$timetitle .= '/';
			$sitetitle .= '/';
		}
		$timetitle .= '<span>'.$month['nr'].'</span>';
		$sitetitle .= $month['nr'];
		$first = false;
	}


?>
<!DOCTYPE html>
<html lang="en">
<head>

	<!-- Basic Page Needs
	–––––––––––––––––––––––––––––––––––––––––––––––––– -->
	<meta name="google-site-verification" content="<?=fetch_text('google-site-verification');?>" />

	<meta charset="utf-8">
	<title>Kartontage: <?=$sitetitle;?></title>
	<meta name="description" content="<?=fetch_text('meta_description');?>">
	<meta name="format-detection" content="telephone=no">
	<meta name="author" content="">

	<!-- Mobile Specific Metas
	–––––––––––––––––––––––––––––––––––––––––––––––––– -->
	<meta name="viewport" content="width=device-width, initial-scale=1">

	<!-- FONT
	–––––––––––––––––––––––––––––––––––––––––––––––––– -->
	<link rel="stylesheet" href="./static/css/fontawesome.min.css">

	<!-- CSS
	–––––––––––––––––––––––––––––––––––––––––––––––––– -->
	<link rel="stylesheet" href="./static/css/normalize.css">
	<link rel="stylesheet" href="./static/css/skeleton.css">
	<link rel="stylesheet" href="./static/css/custom.css">

	<!-- Scripts
	–––––––––––––––––––––––––––––––––––––––––––––––––– -->
	<script type="text/javascript" src="./static/jquery.min.js"></script>
	<script type="text/javascript" src="./static/app.js"></script>

	<!-- Favicon
	–––––––––––––––––––––––––––––––––––––––––––––––––– -->
	<link rel="icon" type="image/png" href="./static/img/favicon.png">

</head>
<body>

	<!-- Primary Page Layout
	–––––––––––––––––––––––––––––––––––––––––––––––––– -->

	<div class="section">
		<div class="container header">
			<div class="row">
				<?php
					$stmt = $conn->prepare('select * from places');
					$stmt->execute();

					$first = true;
					foreach($stmt->fetchAll() as $place) {
				?>
					<?php if(!$first){ ?>
						<div class="c-space"><a href="?"><img class="spacer" src="./static/img/spacer.png"/></a></div>
					<?php } $first = false;?>
				<div class="c"><a href="?places_id=<?=$place['id']?>"><img id="<?=$place['id']?>" src="./static/img/<?=$place['id']?>.png"/></a></div>
			<?php } ?>
			</div>
		</div>
	</div>
	<div class="section">
		<div class="container">
			<div class="row">
				<?php
					$stmt = $conn->prepare('select * from places');
					if(isset($_GET["places_id"])){
						$stmt = $conn->prepare('select * from places where id = :id');
						$stmt->bindParam(':id', $_GET["places_id"]);
					}
					$stmt->execute();
					// set the resulting array to associative
					foreach($stmt->fetchAll() as $place) {
				?>
					<div class="one-half column<?php if(isset($_GET["places_id"])){echo " one";} if($place["id"] % 2){ echo " odd"; }else{ echo " even"; } ?>">
					<h3 class="title"><?=$timetitle;?></h3>
						<?php
							$stmt = $conn->prepare('select * from events_onwebsite where places_id = :id');
							$stmt->bindParam(':id', $place["id"]);
							$stmt->execute();
							foreach($stmt->fetchAll() as $event) {
						?>
							<div class="seperator">
								<div class="time"><?=timeformat($event['time']);?></div>
								<div class="head">
									<span>
										<?php if(strlen($event['url']) > 1 ) { ?>
											<a href="<?=$event['url'];?>"><?=$event['title'];?>&nbsp;<i class="fas fa-link"></i></a>
										<?php
											} else {
												echo $event['title'];
											}
										?>
									</span>
										<?php if(strlen($event['ticket']) > 1 ) { ?>
											<a href="<?=$event['ticket'];?>"><i class="fas fa-ticket-alt" aria-hidden="true"></i></a>
										<?php } ?>
								</div>
								<div class="desc"><?=$event['description'];?></div>
							</div>
					<?php } ?>
					<div class="seperator">
						<?php if(strlen($place['time']) > 1 ) { ?>
							<div class="maintime"><?=$place['time'];?></div>
							<br/>
						<?php }
						if(strlen($place['mastodon']) > 1 ) { ?>
							&nbsp;
							<a href="<?=$place['mastodon'];?>" target="_blank">
								<i class="fab fa-mastodon fa-2x"></i>
							</a>
						<?php } 
						if(strlen($place['pixelfed']) > 1 ) { ?>
							&nbsp;
							<a href="<?=$place['pixelfed'];?>" target="_blank">
								<i class="fab fa-pixelfed fa-2x"></i>
							</a>
						<?php } 
						if(strlen($place['facebook']) > 1 ) { ?>
							&nbsp;
							<a href="<?=$place['facebook'];?>" target="_blank">
								<i class="fab fa-facebook fa-2x"></i>
							</a>
						<?php } 
						if(strlen($place['instagram']) > 1 ) { ?>
							&nbsp;
							<a href="<?=$place['instagram'];?>" target="_blank">
								<i class="fab fa-instagram fa-2x"></i>
							</a>
						<?php }  ?>
					</div>
					&nbsp;
				</div>
			<?php }  ?>
			</div>
		</div>
	</div>
	<div class="section">
		<div class="container footer">
			<div class="text">
				<?=fetch_text('impressum'); ?>
			</div>
			<div class="title">
			Impressum
			</div>
		</div>
	</div>
</body>
</html>
<?php
	$conn = null;
?>
