<?php
session_start();
include "control/activity.php";
$dd = new activity();

@$userKey = $_SESSION['loginKey'];
$gamenumber = $_REQUEST['gamenumber'];
$betAmount = $_REQUEST['betAmount'];
$betSelectedNumber = $_REQUEST['betSelectedNumber'];

if($gamenumber=="" || $betAmount=="" || $betSelectedNumber=="")
{
    print_r(json_encode(array("status" => "failed","remark" => "Sorry required fields..!!")));
}else{
    $checkBalance = $dd->check("userdetails"," where userKey='$userKey'");
    if($checkBalance['data']['wallet'] >= $betAmount){
        $update = $dd->updateData("userdetails","wallet=wallet-$betAmount"," userKey='$userKey'");
        if($update){
            
            
            if($betSelectedNumber >= 0 && $betSelectedNumber <= 9){
                $lossprofit = $betAmount*9;
            }
            
            
            
            $tax = 0;
            $date = date("H:i d/m/Y");
            $ref = mt_rand(1111111111,9999999999);
            $value = "(`userKey`, `bet_amount`, `amount`, `tax`, `action_date`,`refrence`, `gameNumber`, `periods`,`lossorprofit`,`selected_number`, `game_name`) 
            SELECT '$userKey','$betAmount','$betAmount','$tax','$date','$ref','$gamenumber',periods+1,'$lossprofit','$betSelectedNumber','TRX' FROM trx_runnning where game_name='$gamenumber'";
            $result = $dd->insertData("account_history",$value);
            if($result){
                print_r(json_encode(array("status" => "success","remark" => "Order placed..!!")));
            }
        }
    }else{
        $ww = $checkBalance->wallet;
        print_r(json_encode(array("status" => "failed","remark" => "Sorry balance low..!! $ww")));
    }
}



?>