<?php 
	session_start(); 
//datubāzes konfigurācija, saglabājot tos datus mainīgajos
$dbservername = "localhost";
$dbusername = "gamizoge_admins";
$dbpassword = "^uuem[j6kjJ#";
$dbname = "gamizoge_Forums";

	// pieslēgties datubāzei
	$db = new mysqli($dbservername, $dbusername, $dbpassword, $dbname);
	//saglabā esošās sesijas lietotājvārdu mainīgajā
	$username=$_SESSION['username'];

// atlasa kolumnu verified no tabulas users kur lietotājvārds ir saglabātais esošais mainīgais
	$sql = "SELECT verified FROM users WHERE username='$username'";
		$result = $db->query($sql);
		$row = $result->fetch_assoc();
		// ja tabulas dati no verified NAV 'Y', tad sesiju pamet, noņem nost esošās sesijas lietotājvārdu un aiziet atpakaļ uz login.php lapu
    if ($row["verified"] !== 'Y'){
		session_destroy();
		unset($_SESSION['username']);
		header("location: login.php");
			}// ja nav lietotājvārds, tad parāda ziņu un aiziet uz lapu login.php
		if (!isset($_SESSION['username'])) {
				
		$_SESSION['msg'] = "Jums no sākuma jāpieslēdzas.";
		header('location: login.php');
		}
		
// ja piespiesta poga logout, tad sesiju pamet, noņem nost esošās sesijas lietotājvārdu un aiziet atpakaļ uz login.php lapu
	if (isset($_GET['logout'])) {
		session_destroy();
		unset($_SESSION['username']);
		header("location: login.php");
	} 
	
?>
<!DOCTYPE html>
<html>
<title>GAMINGFOREVER</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<link rel="stylesheet" type="text/css" href="stilinns.css">

<style>
body,h1,h2,h3,h4,h5 {font-family: "Raleway", sans-serif}
</style>

<body class="w3-light-grey">
    <header class="w3-container w3-dark-grey w3-padding-32 w3-margin-top" style="margin-top:0px!important; text-align: center; background-color: black!important;padding-top: 0px!important;padding-bottom: 0px!important;">
 	<h1 style="font-size: 100px;margin: 0px 0px 0px 0px;">GAMINGFOREVER</h1>
</header>
<header class="w3-container w3-dark-grey w3-padding-32 w3-margin-top" style="margin-top:0px!important; text-align: center; background-color: black!important;padding-top: 0px!important;padding-bottom: 0px!important;">
 	<ul style="list-style-type: none;margin-bottom:10px!important;margin-top:0px!important;padding-left: 0px!important;">
  <li style="display: inline;font-size: 20px;padding-right: 20px!important;"><a href="index.php">SĀKUMLAPA</a></li>
  <li style="display: inline;font-size: 20px;padding-right: 20px!important;"><a href="forumi.php">FORUMI</a></li>
  <li style="display: inline;font-size: 20px;padding-right: 20px!important;"><a href="noteikumi.php">NOTEIKUMI</a></li>
  <li style="display: inline;font-size: 20px;padding-right: 20px!important;"><a href="parmums.php">PAR MUMS</a></li>
  <li style="display: inline;font-size: 20px;padding-right: 20px!important;"><a href="kontakti.php">KONTAKTI</a></li>
</ul>
</header>
<div class="w3-content" style="max-width:1400px">
<div class="w3-row">
<div class="w3-col l8 s12">
  <div class="w3-card-4 w3-margin w3-white">
    <div class="w3-container">
      <h3><b>Sveicināti GAMINGFOREVER Foruma mājaslapā !</b></h3>
    </div>
    <div class="w3-container">
      <p>Sveicināti GTA forumā. Esam lūdzu pieklājīgi un respektējam citu cilvēku izvēles un 'vārdu izvēli'.</p>
	  <a href="https://gamingforever.xyz/noteikumi.php"><p>LASĪT NOTEIKUMUS. SPIED ŠEIT!</p></a>
    </div>
	<div class="w3-container">
	    <!-- kad spiež uz pogas, tad iet uz jauns-raksts lapu -->
		<form action="https://gamingforever.xyz/jauns-raksts-gta.php" class="jaunsraksts">
			<button class="jaunsraksts">Taisīt jaunu rakstu</button>
		</form><br>
    </div>
	<div class="w3-container">
<?php
//iekļauj šajā php failā config.php failu.
include 'config.php';
//veidojam savienojumu
$conn = new mysqli($servername, $username, $password, $dbname);
	mysqli_select_db($conn, 'multoadr_Forums');
	// cik ierakstus grib lapā
	$results_per_page = 5;
	//cik ir ieraksti tabulā
	$sql='SELECT * FROM gtapost';
	$result = mysqli_query($conn, $sql);
	$number_of_results = mysqli_num_rows($result);
	//noteikt cik daudz lapas ir pieejamas
	$number_of_pages = ceil($number_of_results/$results_per_page);
	//noteikt uz kuras lapas tagad ir apmeklētājs
	//ja nav noteikta lapa, tad automātiski iet uz pirmo lapu, citādi iet/paliek tajā lapā kurā esiet
	if (!isset($_GET['page'])) {
	  $page = 1;
	} else {
	  $page = $_GET['page'];
	}
	
	//noteikt sql limita sākuma numuru priekš rezultātiem, kas rādīsies uz lapas
	$this_page_first_result = ($page-1)*$results_per_page;
	echo '<br><br>';
	for ($page=1;$page<=$number_of_pages;$page++) {
	     // izdrukāt lapas pogas augšā, virs paša pagination
		echo '<a href="gta.php?page='  . $page . '" class="button"><button>' . $page  . '</button></a>';
	}
	echo '<br><br>';
	// iegūt atlasītos rezultātus no datu bāzes un parādīt tos lapā
	$sql='SELECT * FROM gtapost ORDER BY created_at DESC LIMIT ' . $this_page_first_result . ',' .  $results_per_page;
	$result = mysqli_query($conn, $sql);
	while($row = mysqli_fetch_array($result)){
		echo '<div style="margin:5px; border-style: solid;border-width: 1px; border-radius:10px;-moz-border-radius:10px;-webkit-border-radius:10px;">
		
		<a href="gtapost.php?post='.$row["id"].'" style="text-decoration: none;"><div style="border: 1px solid #1C6EA4;
border-radius: 10px 10px 40px 0px; background-color:#f5f5f5;word-wrap:break-word; "><h4 style="margin:-1px 0px -2px 0px;">'.$row["title"].'</h4></a></div>

		<div style="margin:-10px 0px -10px 0px;"><p>Autors:<b> '.$row["author"].'</b> , '.$row["created_at"].'</p></div>'?>
		
<style>
.small {
    height: 60px;
    overflow:hidden;
}
</style>

    <div class="small" style="border: 1px solid #1C6EA4;border-radius: 10px 0px 0px 0px; background-color:#f5f5f5;">
		<p style="margin:0px 0px 0px 0px;"><?php echo ''.$row["content"].''?></p>
		
	</div>
<style>
.container {
  width: 100%;
  margin: auto;
  padding: 5px;
}

.one {
  width: 75%;
  float: left;
}

.two {
  margin-left: 25%;
}

</style>
		<section class="container">
			<div class="one">
	<?php echo ' <a href="gtapost.php?post='.$row["id"].'" style="margin:5px;">Spied šeit lai lasītu visu rakstu. </a>  ';
	?> </div> <div class="two"> <?php
	 // no gtapost paņem ID un paņem no gtakomenti to pašu id tikai no POSTID nevis no ID, lai dabūtu cik ir komentāri rakstam.
	$postid=$row["id"];
	$query = $conn->prepare("SELECT * FROM gtakomenti WHERE postid = '$postid' ");
		$query->execute();
		$query->store_result();
	$rows = $query->num_rows;
echo '<a href="gtapost.php?post='.$row["id"].'">Komentāri: <b>'.$rows.'</b></a>';
	?> </div>
		
		</section>
		<?php echo '
		</div>';
	}
	echo '<br>';
	    // izdrukāt lapas pogas apakšā, zem paša pagination
	for ($page=1;$page<=$number_of_pages;$page++) {
		echo '<a href="gta.php?page='  . $page . '" class="pagebutton"><button>' . $page  . '</button></a>';
	}
	echo '<br><br>';
?>
    </div>
  </div>
  <hr>
</div>

<div class="w3-col l4">
  <div class="w3-card w3-margin w3-margin-top">
  <a href="https://gamingforever.xyz/index.php"><img src="images/logo.png" style="width:100%"></a>
    <div class="w3-container w3-white">
        <!-- Ja lietotājs pieslēdzies pareizi, tad parāda ziņu, ka pieslēdzies ir -->
        	<?php if (isset($_SESSION['success'])) : ?>
      <div class="error success" >
      	<h3>
          <?php 
          	echo $_SESSION['success']; 
          	unset($_SESSION['success']);
          ?>
      	</h3>
      </div>
  	<?php endif ?>
    <!-- Lietotāja informācija -->
    <?php  if (isset($_SESSION['username'])) : ?>
    	<p>Sveicināts - <strong><?php echo $_SESSION['username']; ?></strong></p>
    	<!-- ja logout = 1, tad, tad atvieno sesiju un iet uz login.php lapu -->
    	<form action="index.php?logout='1'" class="lout">
		<button class="logout" name="logout">Izlogoties</button>
		</form>
    <?php endif ?>
	<form method="post" class="lout">
	<?php
// Šībrīža laiks Rīgā
date_default_timezone_set("Europe/Riga");
$today = date("H:i:s");
echo "Laiks Rīgā: ";
echo $today;
echo "<br>";
echo date_default_timezone_get();
?>
    </div>
  </div><hr>
  
  <!-- Posts -->
  <div class="w3-card w3-margin">
    <div class="w3-container w3-padding">
      <h4>Populārākie forumi</h4>
    </div>
    <a href="https://gamingforever.xyz/fortnite.php"><ul class="w3-ul w3-hoverable w3-white">
      <li class="w3-padding-16">
        <img src="ikonas/fortnite.png" alt="FT Image" class="w3-left w3-margin-right" style="width:50px">
        <span class="w3-large">Fortnite</span><br>
        <span>Forums tikai par un ap spēli Fortnite.</span>
      </li></a>
      <a href="https://gamingforever.xyz/counterstrike.php"><ul class="w3-ul w3-hoverable w3-white">
      <li class="w3-padding-16">
        <img src="ikonas/counterstrike.png" alt="CS Image" class="w3-left w3-margin-right" style="width:50px">
        <span class="w3-large">Counter-Strike</span><br>
        <span>Forums tikai par un ap spēli Counter-Strike.</span>
      </li></a>
       <a href="https://gamingforever.xyz/farcry.php"><ul class="w3-ul w3-hoverable w3-white">
      <li class="w3-padding-16">
        <img src="ikonas/farcry.png" alt="Far Cry Image" class="w3-left w3-margin-right" style="width:50px">
        <span class="w3-large">Far Cry</span><br>
        <span>Forums tikai par un ap spēli Far Cry.</span>
      </li></a>
    </ul>
  </div>
  <hr> 
 
  <!-- Labels / tags -->
  <div class="w3-card w3-margin">
    <div class="w3-container w3-padding">
      <h4>Lapas</h4>
    </div>
    <div class="w3-container w3-white">
    <a href="https://gamingforever.xyz/parmumslapa.php"><p>Par mums</p></a>
    </div>
  </div>
  <div class="w3-card w3-margin">
    <div class="w3-container w3-padding">
      <h4>Jaunākais GTA Foruma raksts</h4>
    </div>
    <div class="w3-container w3-white">
	<?php
	// Atlasa jaunāko VIENU rakstu.
	$sql='SELECT * FROM gtapost ORDER BY created_at DESC LIMIT 1';
	$result = mysqli_query($conn, $sql);
	while($row = mysqli_fetch_array($result)){
		echo '<div style="margin:5px; border-style: solid;border-width: 1px; border-radius:10px;-moz-border-radius:10px;-webkit-border-radius:10px;">
		
		<a href="gtapost.php?post='.$row["id"].'" style="text-decoration: none;"><div style="border: 1px solid #1C6EA4;
border-radius: 10px 10px 40px 0px; background-color:#f5f5f5;word-wrap:break-word; "><h4 style="margin:-1px 0px -2px 0px;">'.$row["title"].'</h4></a></div>

		<div style="margin:-10px 0px -10px 0px;"><p>Autors:<b> '.$row["author"].'</b> , '.$row["created_at"].'</p></div>'?>
		
<style>
.small {
    height: 60px;
    overflow:hidden;
}
</style>


    <div class="small" style="border: 1px solid #1C6EA4;border-radius: 10px 0px 0px 0px; background-color:#f5f5f5;">
		<p style="margin:0px 0px 0px 0px;"><?php echo ''.$row["content"].''?></p>
		
	</div>
	
<style>
.container {
  width: 100%;
  margin: auto;
  padding: 5px;
}

.one {
  width: 75%;
  float: left;
}

.two {
  margin-left: 25%;
}

</style>
		<section class="container">
			<div class="one">
	<?php echo ' <a href="gtapost.php?post='.$row["id"].'" style="margin:5px;">Spied šeit lai lasītu visu rakstu. </a>  ';
	?> </div> <div class="two"> <?php 
    // tas pats kods, kas nosaka, cik komentāri ir rakstam, kurš tika izdrukāts.
	$postid=$row["id"];
	$query = $conn->prepare("SELECT * FROM gtakomenti WHERE postid = '$postid' ");
		$query->execute();
		$query->store_result();
	$rows = $query->num_rows;
echo '<a href="gtapost.php?post='.$row["id"].'">Komentāri: <b>'.$rows.'</b></a>';
	}?> </div>
		
</section>
    </div>
      <div class="w3-card w3-margin" style=>
  <img src="images/tanks.png" style="width: 100%;height: 100%;object-fit: contain;">
  </div>
  </div>
  

  
<!-- END Introduction Menu -->
</div>
<!-- END GRID -->
</div><br>

<!-- END w3-content -->
</div>
</div>
<!-- Footer -->
<!-- Footer -->
<footer class="w3-container w3-dark-grey w3-padding-32 w3-margin-top" style="margin-top:0px!important; text-align: center; background-color: black!important;padding-top: 10px!important;padding-bottom: 30px!important;">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}

/* Create three equal columns that floats next to each other */
.column {
  float: left;
  width: 33.33%;
  padding: 10px;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}
</style>
</head>
<body>
<div class="row">
  <div class="column" style="position:relative;">

  </div>
  <div class="column" style="min-width:200px;position:relative;">
    <h1 style="margin: 30px 0px 0px 0px;">GAMINGFOREVER</h1>
  </div>
  <div class="column" style="text-align: right; min-width:200px;position:relative;">
    <p style="margin: 25px 0px 0px 0px;">Autortiesības – Kristaps Švīpis<br>
Jelgavas Tehnikums<br>
e-pasts - forums@gamingforever.xyz<br></p>
  </div>
</div>

</footer>

</body>
</html>
