<?php
    $game_category_url = $segment[3];
    $sql_cat  = $pdo->query("SELECT * FROM tb_game_categories WHERE game_category_url = '".$game_category_url."'")or die(print_r($pdo->errorInfo(), true));
    $data_cat = $sql_cat->fetch(PDO::FETCH_ASSOC);

    $game_category_id = $data_cat['id'];
    $game_category_name = $data_cat['game_category_name'];
?>
<main>

    <section class="featured">
      <h2>All Games</h2>
      <div class="game-list">
          
          <?php $sql  = $pdo->query("SELECT * FROM tb_games")or die(print_r($pdo->errorInfo(), true)); ?>
          <?php 
          if($sql->rowCount() == 0)
          {
              echo "<div class='alert alert-warning'>No games found!</div>";
          }
          ?>
        <?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['image'])) ? "" . BASE_URL . "/img/default-bg.png" : "" . BASE_URL . "/uploads/".$data['image']; ?>
          <img src="<?php echo $image_path; ?>" alt="" />
          <h3><?php echo $data['game_title']; ?></h3>
        </div>
        
        <?php } ?>
        
      </div>
    </section>
    
    <section class="game-list-horizontal">
  <h2>Recommended Games</h2>
  <div class="game-row">
      
      <?php $sql  = $pdo->query("SELECT * FROM tb_games ORDER BY view DESC LIMIT 8")or die(print_r($pdo->errorInfo(), true)); ?>
                    <?php while($data = $sql->fetch(PDO::FETCH_ASSOC)){ ?>
                    <?php 
                    $cat  = $pdo->query("SELECT * FROM tb_game_categories WHERE id = '".$data['game_category_id']."'")or die(print_r($pdo->errorInfo(), true)); 
                    $data_cat = $cat->fetch(PDO::FETCH_ASSOC)
                    ?>
      
    <div class="game-card-horizontal">
        <?php $image_path = (empty($data['image'])) ? "" . BASE_URL . "/img/default-bg.png" : "" . BASE_URL . "/uploads/".$data['image']; ?>
      <img src="<?php echo $image_path; ?>" alt="" />
      <div class="game-info">
        <h3><?php echo $data['game_title']; ?></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>