<!-- Author  Raivis Rampāns
Created 29.02.2024
Editor Raivis
Modified 29.02.2024 -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Minigame</title>
    <link rel="stylesheet" href="minigame.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
    <?php include 'minigameheader.php';?>
    <link rel="stylesheet" href="../css/minigame.css">
    <script src="../js/minigame.js"></script>
</head>
<body>
<?php require "../conn.php" ?> <!-- Savieno ar conn.php, kur datubāzes savienojums tiek izdarīts -->

<?php
  $id = isset($_GET['id']) ? $_GET['id'] : 1; // Iegūt anime ID no URL vai noklusējuma vērtība ir 1
  $sql = "SELECT * FROM anime WHERE id = $id"; // Dabūn tikai 1 anime pēc ID
  $res = $conn->query($sql);

  if ($res->num_rows > 0) {
    $row = $res->fetch_assoc(); // Dabūn anime
    $correctTitle = $row["title"]; // Saglabā pareizo anime nosaukumu

    $sqlIncorrect = "SELECT title FROM anime WHERE id != $id ORDER BY RAND() LIMIT 3"; // Dabūn 3 random anime nosaukumus priekš nepareizajām pogām
    $resIncorrect = $conn->query($sqlIncorrect);
    $incorrectTitles = array();
    while ($rowIncorrect = $resIncorrect->fetch_assoc()) {
        $incorrectTitles[] = $rowIncorrect["title"];
    }

    $incorrectTitles[] = $correctTitle; // Pievieno pareizo pogu nepareizo pogu masīvā

    shuffle($incorrectTitles); // Samaisa pareizo un nepareizās pogas
    ?>
    <div class="container text-center">
        <div id="correctTitle" class="correct-title"></div>
        <img src="https://cdn.myanimelist.net/images/anime/<?php echo $row["poster"] ?>" alt="Anime Title" class="img-fluid" width=300 height=400> <!-- Plakātu izvada -->
    </div>
    <div class="container text-center">
        <?php foreach ($incorrectTitles as $incorrectTitle) { ?>
            <button type="button" class="incorrectbutton" onclick="checkAnswer(this)"><?php echo $incorrectTitle ?></button> <!-- Izveido nepareizās pogas uz ekrāna -->
        <?php } ?>
    </div>
    <input type="hidden" id="correctTitleValue" value="<?php echo $correctTitle ?>"> <!-- Saglabā pareizo anime nosaukumu -->
    <input type="hidden" id="nextAnimeId" value="<?php echo $id + 1 ?>"> <!-- Saglabāt nākamā anime id -->
    <input type="hidden" id="lastAnimeId" value="<?php echo $id ?>"> <!-- Saglabāt iepriekšējā anime id -->
    <?php
  } else {
    $sqlMaxId = "SELECT MAX(id) AS max_id FROM anime"; // Pārbauda vai datubāzē ir vēl nākamais anime pēc id
    $resMaxId = $conn->query($sqlMaxId);
    $rowMaxId = $resMaxId->fetch_assoc();
    $maxId = $rowMaxId["max_id"];
    if ($id < $maxId) {
        echo "結果がありません";
    } else {
        header("Location: minigameend.php"); // Aizsūta uz beigu ekrānu lapu
        exit();
    }
  }
?>
</body>

<footer>
  <?php include 'footer.php';?>
</footer>

</html>
