<?php
include "delete_invites.php";

if (isset($_GET['id'])) {
  $id = $_GET['id']; 
  $delete = mysqli_query($connection, "DELETE FROM `friend_invites` WHERE `id`='$id'");
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="css.css" rel="stylesheet" type="text/css">
  <title>Friend list</title>
</head>
<body>
<div class="fl_title">
<div class="fl_back"><a href="gamechoice.html"><button id="flback">Back</button></a></div>
<div class="fl_name"><h1 id="fltitle">Friend list</h1>
</div>
</div>


  <table class="frequest">
    <h2 id="frtitle">Friend requests</h2>
    <thead>
      <tr>
        <th class="flt1h">Name</th>
        <th class="flt1h0"></th>
        <th class="flt1h00"></th>
      </tr>
    </thead>
    <tbody>
      <?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $database = "raimondsedvins_4110";

        // Create connection
        $connection = new mysqli($servername, $username, $password, $database);

        // Check connection
        if ($connection->connect_error) {
          die("Connection failed: " . $connection->connect_error);
        }

        // Get friend invites data
        $sql = "SELECT * FROM friend_invites";
        $result = $connection->query($sql);

        // Check if there are friend invites
        if ($result->num_rows > 0) {
          while($row = $result->fetch_assoc()) {
            echo "<tr class='invitecontent'>
              <td class='flname'>" . $row["invites"] . "</td>
              <td><button class='flaccept' onclick='acceptInvite(" . $row["id"] . ")'>+</button></td>
              <td><a href='friendlist.php?id=" . $row["id"] . "'><button class='flremove'>-</button></a></td>
            </tr>";
          }
        } else {
          echo "<tr><td colspan='3'>No friend invites.</td></tr>";
        }
      ?>
    </tbody>
  </table>

  <div class="flcadd">
    <button id="fladd" onclick="addfriend()">Add Friend</button>
  </div>

<table class="flist">
  <h2 id="fltable2h">Friend list</h2>
  <thead>
    <tr class="fltable2head">
      <th class="flistnr">Nr.</th>
      <th class="flistname">Name</th>
      <th class="fmore">empty</th>
    </tr>
  </thead>
  <tbody>
    <?php
    //panem friend list tabulas datus
    $sql = "SELECT * FROM  friend_list_data";
    $result = $connection->query($sql);

    //parbauda ielasisanu
    if (!$result) {
      die("Invalid query: " . $connection->error);
    }

    //lasa datus kartai rindai
    while($row = $result->fetch_assoc()){
      echo " <tr class='fltable2content'>
      <td>" . $row["id"] . "</td>
      <td>" . $row["friends"] . "</td>
      <td><button class='f3points'>...</button></td>
    </tr>";
    }
    ?>

  </tbody>
</table>
<script>
    function acceptInvite(inviteId) {
      // Send AJAX request to accept the invite
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          // Reload the page to update the friend invites section
          location.reload();
        }
      };
      xhttp.open("GET", "accept_invite.php?id=" + inviteId, true);
      xhttp.send();
    }
    function addfriend(){
      let person = prompt("Please enter your friends name", "");
     // alert(person);
     var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          // Reload the page to update the friend invites section
          location.reload();
        }
      };
      xhttp.open("GET", "add_friend.php?id=" + person, true);
      xhttp.send();
    }
  </script>
</body>
</html>
