<?php
    $game_category_raw = str_replace("-", " ", $segment[2]);
    $game_category_url = htmlspecialchars($game_category_raw, ENT_QUOTES, 'UTF-8');

    // Gunakan prepared statement
    $sql_cat = $pdo->prepare("SELECT * FROM zon_games WHERE game_category = ?");
    $sql_cat->execute([$game_category_url]);
    $data_cat = $sql_cat->fetch(PDO::FETCH_ASSOC);


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

    <section class="featured">
      <h2><?php echo $game_category_name; ?> Games</h2>
      <div class="game-list">
          
          <?php $sql  = $pdo->query("SELECT * FROM zon_games WHERE game_category = '".$game_category_name."'")or die(print_r($pdo->errorInfo(), true)); ?>
          <?php 
          if($sql->rowCount() == 0)
          {
              echo "<div class='alert alert-warning'>No games found in this category!</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['game_image_url'])) ? "" . BASE_URL . "/img/default-bg.png" : "".$data['game_image_url']; ?>
          <img src="<?php echo $image_path; ?>" alt="" />
          <h3><?php echo htmlspecialchars($data['game_name'], ENT_QUOTES, 'UTF-8'); ?></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>