<?php
ob_start();
session_start();
?>
<!DOCTYPE html>
<html data-bs-theme="dark">
<head>
<?php

require_once 'core/classes.php';
require_once 'core/classes/user.php';
if(isset($_SESSION['user'])) {
$user = new User($con, $_SESSION['user']);
$loggedIn = true;
} else {
$loggedIn = false; 
}
$getitstarted = new PartyStarter;
$getitstarted->header();

if(isset($_GET['p'])) {
$page = $_GET['p'];	
} else {
$page = 1;	
}
$limit = 18;
$page -= 1;
$offset = $page * $limit;

$sql = "SELECT allStar FROM games ORDER BY RAND() LIMIT 1";
$q = $con->prepare(str_replace('allStar', 'name', $sql));
$q->execute();
$random_search_array = $q->fetch();
if($random_search_array || is_array($random_search_array)) {
$random_search = array_rand($random_search_array, 1);
}
?>
</head>
<title><?php echo $pagename; ?> | <?php echo $sitename; ?></title>
<body>
  <main class="container">
    <br>
       <div class="d-flex flex-row justify-content-between mb-3">
    <h3 class="text-white">Games</h3> 
		      <form role="search" class="w-75">
        <div class="input-group shadow-sm mt-1">
          <input class="form-control rounded-end-0" type="search" placeholder="<?php if(is_array($random_search_array)) { echo htmlspecialchars($random_search_array[$random_search]); } else { echo "Search a game..."; }; ?>" name="search" aria-label="Search">
          <button id="search" type="submit" class="btn btn-primary rounded-start-0"><i class="bi bi-search"></i> Search</button>
        </div>
      </form>
	<?php
	if($loggedIn) {
    echo '
		<div class="dropdown mt-1">
  <button class="btn btn-success dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
    <i class="bi bi-download"></i>
  </button>
  <ul class="dropdown-menu dropdown-menu-lg-end anim-dropdown-2 bg-dark">
    <li><a class="dropdown-item" href="/download?client=2016&type=player">Client <small class="float-end">2016</small></a></li>
    <li><a class="dropdown-item" href="/download?client=2016&type=studio">Studio <small class="float-end">2016</small></a></li>
  </ul>
</div>';
	}
	?>
	</div>
		<div class="row row-cols-xs-2 row-cols-sm-3 row-cols-md-4 row-cols-lg-5 row-cols-xl-6 g-4">
    <?php
    $sql = "SELECT allStar FROM games";
    $q = $con->prepare(str_replace('allStar', 'count(*)', $sql));
    $q->execute();
    $numberOfGames = $q->fetchColumn();
    $q = $con->prepare(str_replace('allStar', '*', $sql));
    $q->execute();
    if ($numberOfGames >= 1) {
	 $rows = array_slice($q->fetchAll(), $offset, $limit);
      foreach($rows as $game) {
        $playingCount = 69;
        ?>
		<div class="col">
            <div class="card bg-dark border-start rounded shadow-sm" style="min-width: 140px; max-width: 220px;  margin-right: 9px;">
              <a href="place?id=<?php echo (int)htmlspecialchars($game["id"]); ?>" style="text-decoration: none; text-align: left; margin-bottom: 9px;">
                <img src="<?php if (!empty($game["thumbnail"])) {echo htmlspecialchars($game["thumbnail"]);} else {echo "assets/placeholder.png";} ?>" class="card-img-top rounded-top" alt="<?php echo htmlspecialchars($game["name"]); ?>">
                <div class="card-body">
                  <small class="card-title text-white" style="font-size: 1rem;"><?php echo htmlspecialchars($game["name"]); ?></small>
                  <br>
                  <small class="card-text text-muted"><?php echo (int)$playingCount; ?> playing</small>
                  <div class="progress rounded-4 border border-secondary" style="margin-left: 20px; margin-top: 10px; margin-bottom: -10px; height: 8px; background: none;">
                    <div class="progress-bar bg-secondary" role="progressbar" aria-label="Segment one" style="width: 21.5%" aria-valuenow="15" aria-valuemin="0" aria-valuemax="100"></div>
                  </div>
                  <i class="bi bi-hand-thumbs-up-fill text-secondary" style="font-size: 1rem; float: left; margin-top: -8px;"></i>
                </div>
              </a>
            </div>
		</div>
		<?php
      }
	  echo "</div>";
    } else {
      echo "</div> <br><br><h4 class='text-muted text-center'><i class='bi bi-egg-fried' style='font-size: 6rem;'></i><br> Looks like there are no games, sad.</h4>";} ?>
<?php
$numberOfPages = ceil($numberOfGames / $limit);
?>
<?php if(is_array($random_search_array)) { ?>
 <div class="d-flex flex-row justify-content-center mb-3">
  <ul class="pagination mt-3">
    <li class="page-item"><a class="page-link" href="?p=<?php if($page > 0) { echo $page; } else { echo 1; } ?>">Previous</a></li>
    <?php 
	for ($i=1; $i<=$numberOfPages; $i++) {  
        if($page == $i){
            $class = 'active';
        }else{
            $class = '';
        }

    if ($page > -1) { 
	 $i - 1;
	 echo "<li class='page-item'><a class='page-link' href='?p=$i'>$i</a></li>";
    }
	}
	?>
    <li class="page-item"><a class="page-link" href="?p=<?php if($page < $numberOfPages) { echo $page + 1; } else { echo $numberOfPages; } ?>">Next</a></li>
  </ul>
 </div>
<?php } ?>
  </main>
</body>
</html>
