<?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;
}





$table = "wingo_game_calculation";
$value = "total_bit=total_bit+$finalAmount,winAmount=winAmount+$winAmount";
$condition = " game_number='$gameNumber' AND selected_number='$snumber'";
$result = $dd->updateData($table,$value,$condition);
if($result)
{
    
     if($userDetails['data']['withdraw_balance'] >= $totalAmount){
           $exCondition = ",withdraw_balance=withdraw_balance-$totalAmount";
       }else{
           $exCondition = ",withdraw_balance='0'";
       }
    
    
   $table = "wingo_order_details";
   $condition = " where userKey='$userKey' AND select_number='$snumber' AND periods='$periods' AND status='pending'";
   $checkDala = $dd->check($table,$condition);
   if($checkDala['status'] == "success"){
       $table = "wingo_order_details";
       $value = "amount=amount+$totalAmount,tax=tax+$taxFrom,winAmount=winAmount+$winAmount";
       $condition = " userKey='$userKey' AND select_number='$snumber' AND periods='$periods'";
       $result = $dd->updateData($table,$value,$condition);
       if($result)
       {
          
           
           $table = "userdetails";
           $value = "wallet=wallet-$totalAmount,newBet=newBet+$totalAmount,totalBet=totalBet+$totalAmount".$exCondition;
           $condition = " userKey='$userKey'";
           $result = $dd->updateData($table,$value,$condition);
           if($result){
               print_r(json_encode(array("status" => "success","remark" => "Order Successfully..!!")));
           }
       }
   }else{
       $date = date("h:i d/m/Y");
       $table = "wingo_order_details";
       $value = "(`userKey`, `select_number`, `periods`, `amount`, `tax`, `winAmount`, `order_date`,`game_number`, `status`) 
       SELECT '$userKey','$snumber',periods,'$finalAmount','$taxFrom','$winAmount','$date','$gameNumber','pending' FROM wingo_game_runing WHERE game_number='$gameNumber'";
       $result = $dd->insertData($table,$value);
       if($result)
       {
           $table = "userdetails";
           $value = "wallet=wallet-$totalAmount,newBet=newBet+$totalAmount,totalBet=totalBet+$totalAmount".$exCondition;
           $condition = " userKey='$userKey'";
           $result = $dd->updateData($table,$value,$condition);
           if($result){
               $table = "wingo_game_runing";
               $value = "bet_status='active'";
               $condition = " game_number='$gameNumber'";
               $result = $dd->updateData($table,$value,$condition);
               if($result){
                   print_r(json_encode(array("status" => "success","remark" => "Order Successfully..!!")));
               }
           }
       }
   }
}

}else{
    print_r(json_encode(array("status" => "failed","remark" => "Sorry! Balance Low..!!")));
}

?>