<?php
include 'function/connect.php'; 

$json = file_get_contents("php://input");
$data = json_decode($json, true);

$file = fopen("debug.json","w");
fwrite($file, json_encode($data, JSON_PRETTY_PRINT));
fclose($file);

if($data !== null){
    $i = 0;
    $dataCount = count($data); 
    
    while($i < $dataCount){
        $params = $data[$i];
        
        $username = $params['username'];
        $session = $params['session'];
        $game_code = $params['game_code'];
        $game_provider = $params['game_provider'];
        $bet = $params['bet'];
        $win = $params['win'];
        $turnover = $params['turnover'];
        $betdate = $params['betdate'];
        $game_vendor = $params['vendor'];
        
        $cek_to = mysqli_query($koneksi, "SELECT * FROM tb_history WHERE session = '$session'");
        if(mysqli_num_rows($cek_to) == 1){ 
            echo 'Data sudah ada';
        } else {
            $query = "INSERT INTO tb_history VALUES (NULL, '$username', '$session', '$game_code', '$game_provider', '$bet', '$win', '$turnover', '$betdate', '$game_vendor')";
            
            if(mysqli_query($koneksi, $query) == true){
                echo 'Sukses menyimpan TO';
            } else {
                echo 'Gagal Error Mysqli';
            }
        }
        
        $i++;
    }
} else {
    echo 'Data tidak valid atau kosong';
}
?>
