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

$userKey = $_SESSION['loginKey'];
$snumber = $_REQUEST['snumber'];
$totalAmount = $_REQUEST['amount'];
$qty = $_REQUEST['qty'];
$gameNumber = $_REQUEST['gameNumber'];
$periods = $_REQUEST['periods'];


 $table = "userdetails";
 $condition = " where userKey='$userKey'";
 $result = $dd->check($table,$condition);
 $userDetails = $result;
 if($result['data']['wallet'] >= $totalAmount)
 {


        $table = "admin_calculation";
        $condition = null;
        $getAdminDetails = $dd->single($table,$condition);
        $taxFrom = $totalAmount/100*$getAdminDetails['tax'];
        $finalAmount = $totalAmount-$taxFrom;
        
        
        if($snumber=="red")
        {
            $winAmount = $finalAmount*2;
        }elseif($snumber=="green")
        {
            $winAmount = $finalAmount*2;
        }elseif($snumber=="voilet"){
             $winAmount = $finalAmount*4.5;
        }elseif($snumber=="small")
        {
             $winAmount = $finalAmount*2;
        }elseif($snumber=="big"){
             $winAmount = $finalAmount*2;
        }else{
             $winAmount = $finalAmount*9;
        }


     $update = $dd->updateData("userdetails","wallet=wallet-$totalAmount,newBet=newBet+$totalAmount,totalBet=totalBet+$totalAmount"," userKey='$userKey'");
     if($update){
         $date = date("H:i d/m/Y");
         $value = "(`userKey`, `bet_amount`, `amount`, `tax`, `action_date`,`lossorprofit`, `refrence`, `gameNumber`, `periods`,`selected_number`, `game_name`, `status`) 
         VALUES ('$userKey','$finalAmount','$totalAmount','$taxFrom','$date','$winAmount','$ref','$gameNumber','$periods','$snumber','Wingo','pending')";
         $insert = $dd->insertData("account_history",$value);
         if($insert){
             print_r(json_encode(array("status" => "success","remark" => "Order Successfully..!!")));
         }
     }
}else{
    print_r(json_encode(array("status" => "failed","remark" => "Sorry! Balance Low..!!")));
}

?>