<?php
include '../config.php';

if(isset($_GET['edit'])){

$userid = $_POST['user_id'];
$yas = $_POST['user_yas'];

$query = $db->prepare("UPDATE kullanicilar SET
user_yas = :yeni_yas,
user_yas_edit = :edit
WHERE user_id = :user_id");
$update = $query->execute(array(
     "yeni_yas" => $yas,
     "edit" => 1,
     "user_id" => $userid
));
if ( $update ){
     echo '{"result": "1"}';
}

}else{

?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">


	<title>Heysen Bot Yaş Düzenle</title>
	<style>
.grid-container {
  display: grid;
  gap: 50px;
  grid-template-columns: auto auto auto auto auto auto auto auto auto;
  background-color: #2196F3;
  padding: 10px;
}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.8);
  padding: 10px;
  font-size: 20px;
  text-align: center;
}
</style>

</head>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js" integrity="sha512-n/4gHW3atM3QqRcbCn6ewmpxcLAHGaDjpEBu4xZd47N0W2oQ+6q7oc3PXstrJYXcbNU1OHdQ1T7pAP+gi5Yu8g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script type="text/javascript">
	
	  function yasEdit(value){

var yeni_yas = $("#id_"+value).val();


    var url = "yas_edit.php?edit=1";
    var bilgi = {
      user_id: value,
      user_yas: yeni_yas
    }

console.log(yeni_yas)

    $.post(url,bilgi,function(response) {
      var json = JSON.parse(response);

      if(json.result==1){
        $("#div_"+value).remove();

    }

})

}

</script>

<body>
<div class="grid-container">
<?php


$query = $db->query("SELECT * FROM kullanicilar WHERE user_bot = 1 AND user_yas_edit = 0 ORDER BY RAND() LIMIT 180", PDO::FETCH_ASSOC);

$userArr = array();
$userArr2 = array();

foreach ($query as $veri) {

$userArr[] = $veri['user_id'];

}


$query2 = $db->query("SELECT * FROM sohbetler", PDO::FETCH_ASSOC);


foreach ($query2 as $veri) {

$userArr2[] = $veri['user_id'];

}

$userArr3 = array_unique($userArr2);


$newArr = array_diff($userArr, $userArr3);

//print_r($newArr);

/*shuffle($newArr);*/



//$query = $db->query("SELECT * FROM kullanicilar WHERE user_bot = 1 AND user_yas_edit = 0 LIMIT 100", PDO::FETCH_ASSOC);

foreach ($newArr as $veri){

	$queryx = $db->query("SELECT * FROM kullanicilar WHERE user_id = '{$veri}'")->fetch(PDO::FETCH_ASSOC);

echo '<div id="div_'.$queryx['user_id'].'" class="grid-item"><img height="200" width="200" src="https://app.heysen.net/'.$queryx['user_picture'].'"><br /> '.$queryx['user_fullname'].' <input id="id_'.$queryx['user_id'].'" type="text" value="'.$queryx['user_yas'].'" /><br><button onclick="$(\'#id_'.$queryx['user_id'].'\').val('.($queryx['user_yas']-10).');">'.($queryx['user_yas']-10).'</button><button onclick="$(\'#id_'.$queryx['user_id'].'\').val('.($queryx['user_yas']+10).');">'.($queryx['user_yas']+10).'</button><button onclick="yasEdit('.$queryx['user_id'].')">Güncelle</button></div>';

}

?>
</div>
</body>
</html>


<?php 

}

?>