<?php
include 'anti.php';
include 'konak.php';
if(!isset($_POST['id']))
{
    $jsonarray = ["status"=> "error","message" => "you dont send id"];
    echo json_encode($jsonarray);
    die();
}
$ID = anti_injection($_POST['id']);
if(!isset($_POST['imei']))
{
    $jsonarray = ["status"=> "error","message" => "you dont send imei"];
    echo json_encode($jsonarray);
    die();
}
if(!isset($_POST['timestam']))
{
    $jsonarray = ["status"=> "error","message" => "you dont send timestam"];
    echo json_encode($jsonarray);
    die();
}
$IMEI = anti_injection($_POST['imei']);
$STAMP = anti_injection($_POST['timestam']);
$info = json_encode(["IMEI"  => $IMEI,"Stamp" => $STAMP]);
$data = mysqli_query($koneksi, "SELECT * FROM data_sucses WHERE info='".$info."'");
$cek = mysqli_num_rows($data);
if($cek > 0){ 
    $user = mysqli_fetch_array($data);
    $token =$user["token"];
    if ( $token == 'none'){
        $jsonarray = ["status"=> "OK","message" => "The token has been generated previously, please wait and check periodically."];
        echo json_encode($jsonarray);
        die();
    }
    else
    {  
        $jsonarray = ["status"=> "OK","message" => "The Token Has Been Generated.","token" => $token];
        echo json_encode($jsonarray);
        die();
    }
}
    
$priceQuery = mysqli_query($koneksi, "SELECT harga,servicename FROM price WHERE serviceId='15' LIMIT 1");
if (mysqli_num_rows($priceQuery) == 0) {
    echo json_encode(["status"  => "error","message" => "Service price not found"]);
    die();
}
$priceData = mysqli_fetch_assoc($priceQuery);
$service_price = intval($priceData["harga"]);
$service_name = $priceData["servicename"];
$serviceid = 15;
$data = mysqli_query($koneksi, "SELECT * FROM user_id WHERE ID='".$ID."'");
$cek = mysqli_num_rows($data);
if($cek > 0){ 
    $user = mysqli_fetch_array($data);
    $current_credit =$user["cr"];
    $usermail=$user["email"];
    if ($current_credit < $service_price) {
        echo json_encode(["status"  => "error","message" => "Not enough credit"]);
        die();
    }
    $username = $user["username"];
    $userid = $user["ID"];
    $sqls = "INSERT INTO data_sucses (username,userid,token,status,authid,info ) VALUES ('$username','$userid','none','PENDING','$serviceid','$info')";
    	if(mysqli_query($koneksi, $sqls))
    	{
    	        $message = "The device is request Oppo Token\n";
    	        $message .= " ⦁ Username : " .$username. "\n";
    	        $message .= " ⦁ Email : " . $user["email"]. "\n";
    	        $message .= " ⦁ Credit : " . $current_credit. "\n";
    	        $message .= " ⦁ Price : " . $service_price. "\n";
    	        $message .= " ⦁ Service Name : " . $service_name. "\n";
    	        $message .= " ⦁ ServiceId : " . $serviceid. "\n";
    	        $message .= " ⦁ IMEI : " . $IMEI. "\n";
    	        $message .= " ⦁ TIME Stamp : " . $STAMP. "\n";
    	        telebot($message);
    	        $new_credit = $current_credit - $service_price;
    	        $update = mysqli_query($koneksi,"UPDATE user_id SET cr='$new_credit' WHERE id='".$userid."'");
    	        if (!$update) {
    	            return json_encode(["status"  => "error","message" => "Failed to update credit"]);
    	        }
    	        $jsonarray = ["status"=> "OK","message" => "The token has been generated, please wait 1x12 hours.."];
    	        echo json_encode($jsonarray);
    	        die();
    	}
    	else
    	{
    		echo "gagal"; 
			echo "Could not able to execute $sql. " . mysqli_error($koneksi);
    	}
}
function telebot($message) {
$url = 'https://api.telegram.org/bot8527938197:AAEEn0qWL4g8ZIPC23RdlMW5csMVtskuRrw/sendMessage?chat_id=7066855724&text='.urlencode($message)  ;
$result = file_get_contents($url, false, $context);
if ($result === false) {

}
return $result;
}
?>