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

$userKey = $_SESSION['loginKey'];
$timeduration = $_REQUEST['timeduration'];
$gameAction = $_REQUEST['gameAction'];
$bigsmall = $_REQUEST['bigsmall'];
$selectNumber = $_REQUEST['selectNumber'];
$totalAmount = $_REQUEST['totalAmount'];

sendRequestForCalculation($dd,$userKey,$timeduration,$gameAction,$bigsmall,$selectNumber,$totalAmount);


function sendRequestForCalculation($dd,$userKey,$timeduration,$gameAction,$bigsmall,$selectNumber,$totalAmount)
{
  if($timeduration == "1")
  {
      $gamenumber = "1";
  }
  
  
  if($timeduration == "3")
  {
      $gamenumber = "2";
  }
  
   
  if($timeduration == "5")
  {
      $gamenumber = "3";
  }
  
  
  if($timeduration == "10")
  {
      $gamenumber = "4";
  }



      
   



      if($bigsmall == "small" || $bigsmall == "big" || $bigsmall == "odd" || $bigsmall == "even")
      {
      $winAmount = $totalAmount*1.92;
      $cond = "smallbig like '%$bigsmall%'";
      $type = "1";
      placeOrderFinnaly($dd,$userKey,$timeduration,$gameAction,$bigsmall,$selectNumber,$totalAmount,$cond,$type,$winAmount,$gamenumber);

      }else{


      if($gameAction == "sum")
      {
         
         if($selectNumber == 3)
         {
            $winAmount = $totalAmount*207.36;
         }

         if($selectNumber == 4)
         {
            $winAmount = $totalAmount*69.12;
         }


         if($selectNumber == 5)
         {
            $winAmount = $totalAmount*34.56;
         }

         if($selectNumber == 6)
         {
            $winAmount = $totalAmount*20.74;
         }


         if($selectNumber == 7)
         {
            $winAmount = $totalAmount*13.83;
         }

         if($selectNumber == 8)
         {
            $winAmount = $totalAmount*9.88;
         }

         if($selectNumber == 9)
         {
            $winAmount = $totalAmount*8.3;
         }

         if($selectNumber == 10)
         {
            $winAmount = $totalAmount*7.68;
         }

         if($selectNumber == 11)
         {
            $winAmount = $totalAmount*7.68;
         }


         if($selectNumber == 12)
         {
            $winAmount = $totalAmount*8.3;
         }


         if($selectNumber == 13)
         {
            $winAmount = $totalAmount*9.88;
         }


         if($selectNumber == 14)
         {
            $winAmount = $totalAmount*13.83;
         }


         if($selectNumber == 15)
         {
            $winAmount = $totalAmount*20.74;
         }


         if($selectNumber == 16)
         {
            $winAmount = $totalAmount*34.56;
         }


         if($selectNumber == 17)
         {
            $winAmount = $totalAmount*69.12;
         }


         if($selectNumber == 18)
         {
            $winAmount = $totalAmount*207.36;
         }
         
         $cond = " selected_number='$selectNumber'";
         $type = "1";
         placeOrderFinnaly($dd,$userKey,$timeduration,$gameAction,$bigsmall,$selectNumber,$totalAmount,$cond,$type,$winAmount,$gamenumber);



      }elseif($gameAction == "2same")
      {
         $getBothNum = substr($selectNumber,1);
         $getBothNum2 = substr($selectNumber,0,1);
         $totalSum = $getBothNum+$getBothNum2;

         $winAmount = $totalAmount*13.83;
         $cond = "selected_number='$totalSum' OR selected_number='$selectNumber' AND smallbig='$gameAction'";
         $type = "1";
         placeOrderFinnaly($dd,$userKey,$timeduration,$gameAction,$bigsmall,$selectNumber,$totalAmount,$cond,$type,$winAmount,$gamenumber);
         
      }elseif($gameAction == "3same"){
         $getBothNum = substr($selectNumber,0,2);
         $getBothNum1 = substr($getBothNum,1);
         $getBothNum2 = substr($getBothNum,0,1);
         $lastNumber = substr($selectNumber,-1);

         $totalSum = $getBothNum1+$getBothNum2+$lastNumber;
         $winAmount = $totalAmount*207.36;
         $cond = "selected_number='$totalSum' OR selected_number='$selectNumber' AND smallbig='$gameAction'";
         $type = "1";
         placeOrderFinnaly($dd,$userKey,$timeduration,$gameAction,$bigsmall,$selectNumber,$totalAmount,$cond,$type,$winAmount,$gamenumber);
      }elseif($gameAction == "diffrent")
      {
         if(strlen((string) $selectNumber) == 3)
         {
            $getBothNum = substr($selectNumber,0,2);
            $getBothNum1 = substr($getBothNum,1);
            $getBothNum2 = substr($getBothNum,0,1);
            $lastNumber = substr($selectNumber,-1);
            $totalSum = $getBothNum1+$getBothNum2+$lastNumber;
            $winAmount = $totalAmount*34.56;
            $cond = "selected_number='$totalSum' OR selected_number='$selectNumber' AND smallbig='$gameAction'";
            $type = "0";
            placeOrderFinnaly($dd,$userKey,$timeduration,$gameAction,$bigsmall,$selectNumber,$totalAmount,$cond,$type,$winAmount,$gamenumber);
         }else{

            $getBothNum = substr($selectNumber,1);
            $getBothNum2 = substr($selectNumber,0,1);
            $totalSum = $getBothNum+$getBothNum2;

            $winAmount = $totalAmount*6.91;
            $cond = "selected_number='$totalSum' OR selected_number='$selectNumber' AND smallbig='$gameAction'";
            $type = "0";
            placeOrderFinnaly($dd,$userKey,$timeduration,$gameAction,$bigsmall,$selectNumber,$totalAmount,$cond,$type,$winAmount,$gamenumber);
         }
         
      }

   }




}



function placeOrderFinnaly($dd,$userKey,$timeduration,$gameAction,$bigsmall,$selectNumber,$totalAmount,$cond,$type,$winAmount,$gamenumber)
{
    
    $table = "admin_calculation";
    $condition = null;
    $getAdminDetails = $dd->single($table,$condition);
    $tax = $totalAmount/100*$getAdminDetails['tax'];
    $totalAmount2 = $totalAmount-$tax;
    
    
    
    $table = "userdetails";
    $condition = " where userKey='$userKey'";
    $userDetails = $dd->check($table,$condition);
    
     $rstAmount = $userDetails['data']['wallet']-$userDetails['data']['withdraw_balance'];
    if($rstAmount >= $totalAmount)
    {
        $exCondition = null;
    }else{
        $extrAmount = $totalAmount-$rstAmount;
        $exCondition = ",withdraw_balance=withdraw_balance-$extrAmount";
    }
    

    
    
   if($type == "1")
   {
      $table = "userdetails";
      $condition = " where userKey='$userKey'";
      $result = $dd->check($table,$condition);
      if($result['status'] == "success")
      {
         if($result['data']['wallet'] == $totalAmount OR $result['data']['wallet'] >= $totalAmount)
         {
            $table = "userdetails";
            $value = "wallet=wallet-$totalAmount,newBet=newBet+$totalAmount,totalBet=totalBet+$totalAmount".$exCondition;
            $condition = " userKey='$userKey'";
            $debitBalance = $dd->updateData($table,$value,$condition);
            if($debitBalance)
            {
                  $table = "k3_game_calculation";
                  $value = "bitAmount=bitAmount+$totalAmount,winner_amount=winner_amount+$winAmount";
                  $condition = " $cond AND game_number='$gamenumber'";
                  $result2 = $dd->updateData($table,$value,$condition);
                  if($result2)
                  {
                      $table = "k3_game_runing";
                      $value ="bet_status='betActive'";
                      $condition = " game_number='$gamenumber'";
                      $result3 = $dd->updateData($table,$value,$condition);
                      if($result3){
                           $date = date("h:i d/m/Y");
                           $table = "k3_orders";
                           $value = "(`user_key`, `game_number`,`order_type`, `periods`, `selected_number`,`bigorsmall`, `amount`,`tax`,`winAmount`, `place_date`) 
                           SELECT '$userKey','$gamenumber','$gameAction',periods,'$selectNumber','$bigsmall','$totalAmount2','$tax','$winAmount','$date' FROM k3_game_runing WHERE game_number='$gamenumber'";
                           $result3 = $dd->insertData($table,$value);
                           if($result3)
                           {
                              print_r(json_encode(array("status" => "success","remark" => "Order Placed Successfully..!!")));
                           }
                      }
                  }
               
            }

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


   }else{

      $table = "userdetails";
      $condition = " where userKey='$userKey'";
      $result = $dd->check($table,$condition);
      if($result['status'] == "success")
      {
         if($result['data']['wallet'] == $totalAmount OR $result['data']['wallet'] >= $totalAmount)
         {
            $table = "userdetails";
            $value = "wallet=wallet-$totalAmount,newBet=newBet+$totalAmount,totalBet=totalBet+$totalAmount".$exCondition;
            $condition = " userKey='$userKey'";
            $debitBalance = $dd->updateData($table,$value,$condition);
            if($debitBalance)
            {
                  $table = "k3_game_calculation";
                  $value = "(`game_number`, `periods`, `selected_number`, `bitAmount`, `winner_amount`, `smallbig`) 
                  SELECT '$gamenumber',periods,'$selectNumber','$totalAmount','$winAmount','$gameAction' FROM k3_game_runing WHERE game_number='$gamenumber'";
                  $result = $dd->insertData($table,$value);
                  if($result){
                    $table = "k3_game_calculation";
                    $value = "bitAmount='$totalAmount',winner_amount='$winAmount'";
                    $condition = $cond;
                    $result = $dd->updateData($table,$value,$condition);
                    if($result){
                        $table = "k3_game_runing";
                        $value ="bet_status='betActive'";
                        $condition = " game_number='$gamenumber'";
                        $result3 = $dd->updateData($table,$value,$condition);
                        if($result3){
                              $date = date("h:i d/m/Y");
                              $table = "k3_orders";
                              $value = "(`user_key`, `game_number`,`order_type`, `periods`, `selected_number`,`bigorsmall`, `amount`,`tax`,`winAmount`, `place_date`) 
                              SELECT '$userKey','$gamenumber','$gameAction',periods,'$selectNumber','$bigsmall','$tax','$totalAmount2','$winAmount','$date' FROM k3_game_runing WHERE game_number='$gamenumber'";
                              $result3 = $dd->insertData($table,$value);
                              if($result3)
                              {
                                 print_r(json_encode(array("status" => "success","remark" => "Order Placed Successfully..!!")));
                              }
                        }
                    }
                  }
            }

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


   }
}
?>