<?php
    $sql = $pdo->query("SELECT id FROM zon_games ORDER BY id DESC");
    $ids = $sql->fetchAll(PDO::FETCH_COLUMN);

    if (count($ids) > 0) {
        $random_id = $ids[array_rand($ids)];
        $game_sql = $pdo->prepare("SELECT * FROM zon_games WHERE id = ?");
        $game_sql->execute([$random_id]);

        $game = $game_sql->fetch();
    } else {
        $game = null;
    }
?>

<?php
    $src = htmlspecialchars($game['game_url'], ENT_QUOTES, 'UTF-8');
    $game_title = htmlspecialchars($game['game_name'], ENT_QUOTES, 'UTF-8');
    $game_category = htmlspecialchars($game['game_category'], ENT_QUOTES, 'UTF-8');
    $game_description = $game['game_description'];
?>
<main>
    
      <section class="game-player">
  <div class="game-frame">
    <center>
        <iframe src="<?php echo $src; ?>" width="100%" height="600" scrolling="none" frameborder="0"></iframe>
    </center>
  </div>
</section>

    <section class="featured">
      <h2>Newest Games</h2>
      <div class="game-list">
          
          <?php $sql  = $pdo->query("SELECT * FROM zon_games ORDER BY id DESC LIMIT 8")or die(print_r($pdo->errorInfo(), true)); ?>
                    <?php while($data = $sql->fetch(PDO::FETCH_ASSOC)){ ?>
                   
        <div class="game-card" onclick="location.href='<?php echo BASE_URL; ?>/games/<?php echo $data['game_slug']; ?>';" role="button">
            <?php $image_path = (empty($data['game_image_url'])) ? "" . BASE_URL . "/img/default-bg.png" : "".$data['game_image_url']; ?>
          <img src="<?php echo $image_path; ?>" alt="" />
          <h3><?php echo $data['game_name']; ?></h3>
        </div>
        
        <?php } ?>
        
      </div>
    </section>
    
    <section class="game-list-horizontal">
  <h2>Recommended Games</h2>
  <div class="game-row">
      
      <?php $sql  = $pdo->query("SELECT * FROM zon_games ORDER BY view DESC LIMIT 8")or die(print_r($pdo->errorInfo(), true)); ?>
                    <?php while($data = $sql->fetch(PDO::FETCH_ASSOC)){ ?>
      
    <div class="game-card-horizontal">
        <?php $image_path = (empty($data['game_image_url'])) ? "" . BASE_URL . "/img/default-bg.png" : "".$data['game_image_url']; ?>
      <img src="<?php echo $image_path; ?>" alt="" />
      <div class="game-info">
        <h3><?php echo $data['game_name']; ?></h3>
        <p><?php echo limit_word($data['game_description'],10); ?> ...</p>
        <a href="<?php echo BASE_URL; ?>/games/<?php echo $data['game_slug']; ?>" class="play-button">Play Now</a>
      </div>
    </div>

    <?php } ?>
    
  </div>
</section>

    
  </main>