<?php
ob_start();
session_start();

include("config.php");

$amount = $_POST['amount'];
$userid= $_SESSION['frontuserid'];

$sqlus = mysqli_query($com,"select * from loginuser where id ='$userid'");
$data = mysqli_fetch_assoc($sqlus);
$code = $data['code'];
if($code !='')
{
$sqluc = mysqli_query($com,"select * from loginuser where owncode ='$code'");
$numc = mysqli_num_rows($sqluc);
if($numc > 0)
{
$datac = mysqli_fetch_assoc($sqluc);
$level1id = $datac['id'];
}
}

$sqlwallet = mysqli_query($com,"select * from wallet where userid ='$userid'");
	$wallert_row = mysqli_fetch_array($sqlwallet);
    $walletbalance = $wallert_row['amount'];
if($walletbalance >= $amount)
{
          if($amount == 699)
              {
                $sql = mysqli_query($com,"INSERT INTO `buy_1`(`userid`,`amount`) VALUES ('".$userid."','$amount')"); 
                
                $totalamount = $walletbalance - $amount;
                $invitebonus = 99;
                $purchesbonus = $totalamount + 50;
                
                $sqlwalletupdate = mysqli_query($com,"update withdraw_balance set amount= amount + 50 where userid ='$userid'"); 
                $sqlwalletsummery = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$userid','50','1','credit','bonus')");
                $sqlbonusupdate =  mysqli_query($com,"update bonus set amount= amount + 50 where userid ='$userid'"); 
                  
                 if($code !='')
                 {
                 $sqlwalletupdate1 = mysqli_query($com,"update withdraw_balance set amount = amount + '$invitebonus' where userid ='$level1id'"); 
                 $sqlwalletsummery1 = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$level1id','$invitebonus','1','credit','bonus')");
                 $sqlbonusupdate1 =  mysqli_query($com,"update bonus set amount= amount + $invitebonus,level1 = level1 + $invitebonus where userid ='$level1id'");
                 $sqlbonussummrry = mysqli_query($com,"insert into bonussummery(userid,level1id,level1amount)values('$userid','$level1id','$invitebonus')");
                   
                 }
              }
              if($amount == 2600)
              {
                  $sql = mysqli_query($com,"INSERT INTO `buy_2`(`userid`,`amount`) VALUES ('".$userid."','$amount')"); 
                   $totalamount = $walletbalance - $amount;
                   $invitebonus = 199;
                   $purchesbonus = $totalamount + 110;
                
                 $sqlwalletupdate = mysqli_query($com,"update withdraw_balance set amount= amount + 110 where userid ='$userid'"); 
                 $sqlwalletsummery = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$userid','110','1','credit','bonus')");
                 $sqlbonusupdate =  mysqli_query($com,"update bonus set amount= amount + 110 where userid ='$userid'"); 
                
                 if($code !='')
                 {
                 $sqlwalletupdate1 = mysqli_query($com,"update withdraw_balance set amount = amount + '$invitebonus' where userid ='$level1id'"); 
                 $sqlwalletsummery1 = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$level1id','$invitebonus','1','credit','bonus')");
                 $sqlbonusupdate1 =  mysqli_query($com,"update bonus set amount= amount + $invitebonus,level1 = level1 + $invitebonus where userid ='$level1id'");
                  $sqlbonussummrry = mysqli_query($com,"insert into bonussummery(userid,level1id,level1amount)values('$userid','$level1id','$invitebonus')");
                 
                 }
              }
              if($amount == 5100)
              {
                 $sql = mysqli_query($com,"INSERT INTO `buy_3`(`userid`,`amount`) VALUES ('".$userid."','$amount')");  
                  $totalamount = $walletbalance - $amount;
                  $invitebonus = 499;
                $purchesbonus = $totalamount + 110;
                
                $sqlwalletupdate = mysqli_query($com,"update withdraw_balance set amount=amount + 110 where userid ='$userid'"); 
                $sqlwalletsummery = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$userid','110','1','credit','bonus')");
                 $sqlbonusupdate =  mysqli_query($com,"update bonus set amount= amount + 110 where userid ='$userid'"); 
                
                 if($code !='')
                 {
                 $sqlwalletupdate1 = mysqli_query($com,"update withdraw_balance set amount = amount + '$invitebonus' where userid ='$level1id'"); 
                 $sqlwalletsummery1 = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$level1id','$invitebonus','1','credit','bonus')");
                 $sqlbonusupdate1 =  mysqli_query($com,"update bonus set amount= amount + $invitebonus,level1 = level1 + $invitebonus where userid ='$level1id'");
                  $sqlbonussummrry = mysqli_query($com,"insert into bonussummery(userid,level1id,level1amount)values('$userid','$level1id','$invitebonus')");
                    
                 }
              }
              if($amount == 13000)
              {
                 $sql = mysqli_query($com,"INSERT INTO `buy_4`(`userid`,`amount`) VALUES ('".$userid."','$amount')");  
                  $totalamount = $walletbalance - $amount;
                  $invitebonus = 999;
                $purchesbonus = $totalamount + 150;
                
                $sqlwalletupdate = mysqli_query($com,"update withdraw_balance set amount=amount + 150 where userid ='$userid'"); 
                $sqlwalletsummery = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$userid','150','1','credit','bonus')");
                 $sqlbonusupdate =  mysqli_query($com,"update bonus set amount= amount + 150 where userid ='$userid'"); 
                
                 if($code !='')
                 {
                 $sqlwalletupdate1 = mysqli_query($com,"update withdraw_balance set amount = amount + '$invitebonus' where userid ='$level1id'"); 
                 $sqlwalletsummery1 = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$level1id','$invitebonus','1','credit','bonus')");
                 $sqlbonusupdate1 =  mysqli_query($com,"update bonus set amount= amount + $invitebonus,level1 = level1 + $invitebonus where userid ='$level1id'");
                  $sqlbonussummrry = mysqli_query($com,"insert into bonussummery(userid,level1id,level1amount)values('$userid','$level1id','$invitebonus')");
                    
                 }
              }
              if($amount == 24000)
              {
                 $sql = mysqli_query($com,"INSERT INTO `buy_5`(`userid`,`amount`) VALUES ('".$userid."','$amount')");  
                  $totalamount = $walletbalance - $amount;
                  $invitebonus = 1499;
                $purchesbonus = $totalamount + 150;
                
                $sqlwalletupdate = mysqli_query($com,"update withdraw_balance set amount=amount + 150 where userid ='$userid'"); 
                $sqlwalletsummery = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$userid','150','1','credit','bonus')");
                 $sqlbonusupdate =  mysqli_query($com,"update bonus set amount= amount + 150 where userid ='$userid'"); 
                
                 if($code !='')
                 {
                 $sqlwalletupdate1 = mysqli_query($com,"update withdraw_balance set amount = amount + '$invitebonus' where userid ='$level1id'"); 
                 $sqlwalletsummery1 = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$level1id','$invitebonus','1','credit','bonus')");
                 $sqlbonusupdate1 =  mysqli_query($com,"update bonus set amount= amount + $invitebonus,level1 = level1 + $invitebonus where userid ='$level1id'");
                  $sqlbonussummrry = mysqli_query($com,"insert into bonussummery(userid,level1id,level1amount)values('$userid','$level1id','$invitebonus')");
                     
                 }
              }
              if($amount == 49000)
              {
                 $sql = mysqli_query($com,"INSERT INTO `buy_6`(`userid`,`amount`) VALUES ('".$userid."','$amount')");  
                  $totalamount = $walletbalance - $amount;
                  $invitebonus = 1999;
                $purchesbonus = $totalamount + 150;
                
                $sqlwalletupdate = mysqli_query($com,"update withdraw_balance set amount=amount + 150 where userid ='$userid'"); 
                $sqlwalletsummery = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$userid','150','1','credit','bonus')");
                 $sqlbonusupdate =  mysqli_query($com,"update bonus set amount= amount + 150 where userid ='$userid'"); 
                
                 if($code !='')
                 {
                 $sqlwalletupdate1 = mysqli_query($com,"update withdraw_balance set amount = amount + '$invitebonus' where userid ='$level1id'"); 
                 $sqlwalletsummery1 = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$level1id','$invitebonus','1','credit','bonus')");
                 $sqlbonusupdate1 =  mysqli_query($com,"update bonus set amount= amount + $invitebonus,level1 = level1 + $invitebonus where userid ='$level1id'");
                  $sqlbonussummrry = mysqli_query($com,"insert into bonussummery(userid,level1id,level1amount)values('$userid','$level1id','$invitebonus')");
                     
                 }
              }
              if($amount == 98000)
              {
                 $sql = mysqli_query($com,"INSERT INTO `buy_7`(`userid`,`amount`) VALUES ('".$userid."','$amount')");  
                  $totalamount = $walletbalance - $amount;
                  $invitebonus = 2999;
                $purchesbonus = $totalamount + 150;
                
                $sqlwalletupdate = mysqli_query($com,"update withdraw_balance set amount=amount + 150 where userid ='$userid'"); 
                $sqlwalletsummery = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$userid','150','1','credit','bonus')");
                 $sqlbonusupdate =  mysqli_query($com,"update bonus set amount= amount + 150 where userid ='$userid'"); 
                
                 if($code !='')
                 {
                 $sqlwalletupdate1 = mysqli_query($com,"update withdraw_balance set amount = amount + '$invitebonus' where userid ='$level1id'"); 
                 $sqlwalletsummery1 = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$level1id','$invitebonus','1','credit','bonus')");
                 $sqlbonusupdate1 =  mysqli_query($com,"update bonus set amount= amount + $invitebonus,level1 = level1 + $invitebonus where userid ='$level1id'");
                  $sqlbonussummrry = mysqli_query($com,"insert into bonussummery(userid,level1id,level1amount)values('$userid','$level1id','$invitebonus')");
                    
                 }
              }
              
             $sqlbuy = mysqli_query($com,"select * from total_buy where userid ='$userid'");
	         $buy_row = mysqli_fetch_array($sqlbuy);
             $buybalance = $buy_row['amount'] + $amount;
             $sqlbuyupdate = mysqli_query($com,"update total_buy set amount='$buybalance' where userid ='$userid'"); 
             $sqlwalletupdate = mysqli_query($com,"update wallet set amount=amount - $amount where userid ='$userid'"); 
             $sqlwalletsummery17 = mysqli_query($com,"insert into walletsummery(userid,amount,status,type,actiontype)values('$userid','$amount','1','debit','product')");
                
 echo "<script type='text/javascript'>alert('Buy Success'); window.location='myaccount.php';</script>";             
}else{
    	echo "<script type='text/javascript'>alert('Your balance is insufficient'); window.location='recharge.php';</script>";
}





?>