<?php
$qcal = mysqli_query($conn,"SELECT * FROM earnings WHERE email='$user'  ");
$elog = mysqli_fetch_array($qcal);



$qrysit = mysqli_query($conn,"SELECT * FROM settings");
$sit = mysqli_fetch_array($qrysit);
$sitename = $sit['sitename'];
$serName = $_SERVER['HTTP_HOST'];

	
$check=mysqli_query($conn,"SELECT * FROM bankinfo WHERE email='$user' ");
$num = mysqli_num_rows($check);
$bkinfo = mysqli_fetch_array($check);

if($num === 0){
	$btn = "Add Bank";
	}else{ $btn = "Save changes";}
	
	 // Bank information
  		
if(isset($_POST['bnk'])){

$bankname = $_POST['bankname'];
$accno = $_POST['accno'];
$accname = $_POST['accname'];

 if(!empty($bankname) && !empty($accno) && !empty($accname) ){


if ($num ==0){
	
$insertdat = mysqli_query($conn,"INSERT INTO bankinfo(bankname,accno,accname,email) VALUES('$bankname','$accno','$accname','$user') ") or die(mysqli_error());	

ok();

	 }else{

$updat = mysqli_query($conn,"UPDATE bankinfo SET bankname='$bankname',accno='$accno',accname='$accname',email='$user' WHERE email='$user'  ") or die(mysqli_error()); 


ok();

        
	 }
	 }else{
		echo '<div class="alert alert-danger">Enter required field</div> <script>setTimeout(function(){ window.location="earnings.php" }, 2000);</script>'; 
		 }
 
    } 


  		
if(isset($_POST['withdraw'])){
$amount = $_POST['amount'];
$method = $_POST['method'];

$amt = str_replace( array('+', ',', '--', '-', '/', '(', ')',   '_',   '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $amount);
$refbal = str_replace( array('+', ',', '--', '-', '/', '(', ')',   '_',   '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $refbal);
$descr = "Commission Withdrawal";

 if(!empty($_POST['method']) && !empty($_POST['amount'])){
	 
	
$add = 0;

if($amount >= 1){
    
if($amt <= $refbal){
	 
if($method === "towallet"){



$stat = "Completed";
$option = "Wallet";


$sendorder = mysqli_query($conn,"INSERT INTO withdraw(name,amount,email,description,status,method,action,del) VALUES('$fname $lname','$amt','$user','$descr','$stat','$option','$action','$del'); ");

$pro = mysqli_query($conn,"UPDATE users SET refwallet=refwallet-$amt WHERE email='$user' ");
$Dopro = mysqli_query($conn,"UPDATE users SET bal=bal+$amt WHERE email='$user' ");

$check=mysqli_query($conn,"SELECT * FROM earnings WHERE email='$user' ");
$num = mysqli_num_rows($check);
    
    
    if ($num ==0){
        
$sendorders = mysqli_query($conn,"INSERT INTO earnings(withdrawal,username,email,status,action,del,today,month,date,dateTime) VALUES('$amt','$fname $lname','$user','Active','$action','$del','$today','$month','$date','$dateTime'); ");
        
    }else{ 
        
$comm = mysqli_query($conn,"UPDATE earnings SET withdrawal=withdrawal+$amt,period='$date' WHERE email='$user' ");	

}

	
	xt2();
}else{

//if($num ==0){xt();}else{

if($method === "tobank"){
    



$stat = "Pending";
$option = "Bank";
$charge = 100;
$amt2 = $amt-$charge;	

  
$sendorder = mysqli_query($conn,"INSERT INTO withdraw(name,amount,email,description,status,method,action,del) VALUES ('$fname $lname','$amt2','$user','$descr','$stat','$option','$action','$del'); ");

$pro = mysqli_query($conn,"UPDATE users SET refwallet=refwallet-$amt WHERE email='$user' ");


$check=mysqli_query($conn,"SELECT * FROM earnings WHERE email='$user' ");
$num = mysqli_num_rows($check);
    
    
if ($num ==0){
        
$sendorders = mysqli_query($conn,"INSERT INTO earnings(withdrawal,username,email,status,action,del,today,month,date,dateTime) VALUES('$amt','$fname $lname','$user','Active','$action','$del','$today','$month','$date','$dateTime'); ");
}else{ 
$comm = mysqli_query($conn,"UPDATE earnings SET withdrawal=withdrawal+$amt,period='$date' WHERE email='$user' ");	
}






xt3();

}else{xt4();}} // }

//send email here see inc/walert.php

}else{
echo '<div class="alert alert-danger">Your Referal Wallet is Low</div> <script>setTimeout(function(){ window.location="earnings.php" }, 5000);</script>'; 
}


}else{
echo '<div class="alert alert-danger">You can not Withdraw Less Than N1</div> <script>setTimeout(function(){ window.location="earnings.php" }, 5000);</script>'; 
}

	 }else{
		echo '<div class="alert alert-danger">Enter required field</div> <script>setTimeout(function(){ window.location="earnings.php" }, 2000);</script>'; 
		 }
 
    } 
	 
?>