<?php 

include '../config.php';

$veri = file_get_contents("https://hupto.cntapi.com/senaryoapi.php");

$veriJson = json_decode($veri, true);


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

foreach($veriJson as $veri){

if($veri['user_id']==$_GET['id']){
	echo $veri['icerik']."\n";
}

}


}else if(isset($_GET['user'])){


$veri = file_get_contents("https://hupto.cntapi.com/senaryouser.php?userid=".$_GET['user']);

echo $veri;


}else if(isset($_GET['bot'])){


$query = $db->query("SELECT * FROM kullanicilar WHERE user_bot = 1 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);



foreach ($newArr as $veri) {


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

echo '<div class="grid-item"><img height="200" width="200" src="https://app.heysen.net/'.$queryx['user_picture'].'"><br /> '.$queryx['user_fullname'].', '.$queryx['user_yas'].'<br><button onclick="botverisi(\'https://app.heysen.net/'.$queryx['user_picture'].'\',\''.$queryx['user_fullname'].'\',\''.$queryx['user_yas'].'\',\''.$queryx['user_id'].'\')">Kullan</button></div>';


}


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

foreach($query as $veri){
	echo '<div class="grid-item"><img height="200" width="200" src="https://app.heysen.net/'.$veri['user_picture'].'"><br /> '.$veri['user_fullname'].', '.$veri['user_yas'].'<br><button onclick="botverisi(\'https://app.heysen.net/'.$veri['user_picture'].'\',\''.$veri['user_fullname'].'\',\''.$veri['user_yas'].'\',\''.$veri['user_id'].'\')">Kullan</button></div>';
}
*/
}else if(isset($_GET['senaryo'])){


   if(isset($_POST['mesajlist'])){ 
   	$botid=$_POST['botid'];
   	$oldid=$_POST['oldid'];
   	$mesajlist=$_POST['mesajlist'];

$parcalanmis = explode("\n", $mesajlist);
$i = 1;
foreach ($parcalanmis as $veri) {
	// code...
/*	echo $veri;
	echo " == EKLENDİ. <br>";*/


$query = $db->prepare("INSERT INTO sohbetler SET
user_id = ?,
old_user_id = ?,
sohbet_sira = ?,
icerik = ?");
$insert = $query->execute(array(
     $botid, $oldid, $i, $veri
));



	$i++;
}

$array = array("result" => "1");
  echo json_encode($array); 

}


}else{



$queryx = $db->query("SELECT * FROM sohbetler GROUP BY old_user_id", PDO::FETCH_ASSOC);

$old_arr = array();

foreach($queryx as $veri){
	$old_arr[] = $veri['old_user_id'];
	
}






     foreach( $veriJson as $row ){

$user_id_array[] = $row['user_id'];


     }


 $idlerx = array_unique($user_id_array);
 $idler = array_diff($idlerx, $old_arr);

?>
<title>Heysen Bot Senaryo Oluşturucu</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    

<textarea rows="25" cols="200" id="senaryovar"></textarea>


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

var secilen = $("#useridi option:selected").val();



    var url = "senaryocek.php?id="+secilen;
    var bilgi = {
      id: secilen
    }
    $.post(url,bilgi,function(response) {
      
        
$("#senaryovar").val(response);


   

})



    var url = "senaryocek.php?user="+secilen;
    var bilgi = {
      type: 'begeni',
      begenilen_id: secilen
    }
    $.post(url,bilgi,function(response) {
      var json = JSON.parse(response);

$("#isim").val(json.user_fullname)
$("#eski_id").val(json.user_id)
$("#yas").val(json.user_yas)
$("#foto").attr("src","https://hupto.cntapi.com/"+json.user_picture);
  


});





}

function botgetir(){

$(".grid-container").html('<center><img src="https://c.tenor.com/wfEN4Vd_GYsAAAAC/loading.gif"></center>');

var url = "senaryocek.php?bot=1";
    var bilgi = {
      id: "bos"
    }
    $.post(url,bilgi,function(response) {
      
        
$(".grid-container").html(response);


   

})


}

$( document ).ready(function() {
    botgetir();
});

function botverisi(resim,isim,yas,userid){

$("#yeni_isim").val(isim)
$("#yeni_yas").val(yas)
$("#yeni_foto").attr("src",resim);
$("#yeni_id").val(userid)

}

function senaryoEkle(){

var icerik = $("#senaryovar").val();


//alert(icerik);

    var url = "senaryocek.php?senaryo=1";
    var bilgi = {
      botid: $("#yeni_id").val(),
      oldid: $("#eski_id").val(),
      mesajlist: icerik
    }
    $.post(url,bilgi,function(response) {
      var json = JSON.parse(response);

      if(json.result==1){
alert('ekleme yapıldı');

location.reload();

}

});



}


function senaryoEkle2(){

var icerik = $("#senaryovar").val();

alert(icerik)

	}

</script>




<br><br><br>
<button onclick="senaryoEkle();">Yeni Senaryoyu Ekle</button><br><br>
<br>
Yeni Bot İsim: <input type="text" name="yeni_isim" id="yeni_isim"><br>
Yeni Bot Yaş: <input type="text" name="yeni_yas" id="yeni_yas"><br>
Yeni Bot ID: <input type="text" name="yeni_id" id="yeni_id"><br>
<img id="yeni_foto" height="250" width="250" /><br><br>


<button onclick="botgetir();">Botları Yenile</button><br><br>
<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>


<div class="grid-container">



</div>
<br><br>
<button onclick="botgetir();">Botları Yenile</button><br><br>







<?php 

}

?>