<!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>scoreboard</title>
</head>
<body>

<div class="choose">
  <div class="back-title">
    <a href="gamechoice.html">
      <button class="scoreback">Back</button>
    </a>
    <div class="title">
      <h1>Scoreboard</h1>
    </div>
  </div>
</div>
	   <table class="scoretable">
		<!-- izveido tabulas galvu -->
		   <thead>
			   <tr class="scoretableHead">
				   <th id="st1">ID</th>
				   <th id="st2"> Nickname</th>
				   <th id="st3">Score</th>
			   </tr>
		   </thead>
		   <tbody>
		   <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $database = "raimondsedvins_4110";

    // Izveidot savienojumu
    $connection = new mysqli($servername, $username, $password, $database);
    //parbaude
    if ($connection->connect_error) {
      die("Connection failed: " . $connection->connect_error);
    }
    //panem friend list tabulas datus
	//sql select izmaina id pret lietotaju un drauga vardu
    $sql = "
	SELECT
		*
	 FROM
	   games s
	   left join
	   friend_list_data f
	   ON s.id_friend_list_data=f.id";
    $result = $connection->query($sql);
    //parbauda ielasisanu
    if (!$result) {
      die("Invalid query: " . $connection->error);
    }
 //lasa datus kartai rindai ja vajag bouncing ball datus
	if(isset($_POST['bouncing_filter'])){ 
  while($row = $result->fetch_assoc()){
    echo " <tr class=''>
    <td>" . $row["id"] . "</td>
	 <td>" . $row["friends"] . "</td>
	<td>" . $row["bouncing"] . "</td>
  </tr>";
  }
}

//lasa datus ja pieprasa minesweeper easy
if(isset($_POST['easy_minesweeper_filter'])){
	//lasa datus kartai rindai
while($row = $result->fetch_assoc()){
echo " <tr class=''>
<td>" . $row["id"] . "</td>
<td>" . $row["friends"] . "</td>
<td>" . $row["minesweeper_easy"] . "</td>
</tr>";
}
}

//lasa datus ja pieprasa minesweeper medium
if(isset($_POST['medium_minesweeper_filter'])){
	//lasa datus kartai rindai
while($row = $result->fetch_assoc()){
echo " <tr class=''>
<td>" . $row["id"] . "</td>
<td>" . $row["friends"] . "</td>
<td>" . $row["minesweeper"] . "</td>
</tr>";
}
}

//lasa datus ja pieprasa minesweeper hard
if(isset($_POST['hard_minesweeper_filter'])){
	//lasa datus kartai rindai
while($row = $result->fetch_assoc()){
echo " <tr class=''>
<td>" . $row["id"] . "</td>
<td>" . $row["friends"] . "</td>
<td>" . $row["minesweeper_hard"] . "</td>
</tr>";
}
}
    ?>

<div class="sfilter">
		<h2 id="sfilter">Filter:</h2>
		<div class="sgamec">
			<button class="sgame" id="sminesweeper" onclick="showdiff()">Minesweeper</button>
			<form method="post"><button class="sgame" type="submit" id="sbauncingball" name="bouncing_filter" onclick="hidediff()" >Bouncing ball</button></form>
			<button class="sgame" id="ssolitare" onclick="showdiff()">Solitare</button>
			<button class="sgame" id="ssnake" onclick="showdiff()">Snake</button>

		</div>
		<div class="sdiffc" >
			<form method="post"><button class="sdiff" type="submit" id="seasy" name="easy_minesweeper_filter" >Easy</button></form>
			<form method="post"><button class="sdiff" type="submit" id="smedium" name="medium_minesweeper_filter" >Medium</button></form>
			<form method="post"><button class="sdiff" type="submit" id="shard" name="hard_minesweeper_filter" >hard</button></form>
		</div>
	   </div>
		   </tbody>
	   </table>
</body>
</html>
<script>
	//paslepj grutibas pakapes
	function hidediff(){
		document.getElementById("seasy").style.display="none"
		document.getElementById("smedium").style.display="none"
		document.getElementById("shard").style.display="none"
	}
//parada grutibas pakapes
	function showdiff(){
		document.getElementById("seasy").style.display="inline"
		document.getElementById("smedium").style.display="inline"
		document.getElementById("shard").style.display="inline"
	}

const sgameList = document.querySelectorAll('.sgame');
sgameList.forEach(sgame => {
	sgame.addEventListener('click', () =>{
		document.querySelector('.sgamespecial')?.classList.remove('sgamespecial');
		sgame.classList.add('sgamespecial');
	});
});

const sdiffList = document.querySelectorAll('.sdiff');
sdiffList.forEach(sdiff => {
	sdiff.addEventListener('click', () =>{
		document.querySelector('.sgamespecial1')?.classList.remove('sgamespecial1');
		sdiff.classList.add('sgamespecial1');
	});
});
</script>