<?php
session_start();
include_once ('conn/session.php');

require_once 'conn/controller.php';
if (!isset($_SESSION['acc_no'])) {

    header("Location: auth_login.php");
    exit();
}

$reg_user = new USER();

$stmt = $reg_user->runQuery("SELECT * FROM account WHERE acc_no=:acc_no");
$stmt->execute(array(":acc_no"=>$_SESSION['acc_no']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stat = $row['status'];


        if(isset($_POST['btc_transfer'])){

        $conn = mysqli_connect($host, $username, $password, $database);
        
        // Check connection
        
        if (!$conn) {
              die("Connection failed: " . mysqli_connect_error());
        }
         
        echo ""; 
        
        $db_select = mysqli_select_db($con, $database);

        
        $acc_no =$_REQUEST['acc_no'];
        $email =$_REQUEST['email'];
        $rwallet =$_REQUEST['rwallet'];
        $swallet =$_REQUEST['swallet'];
        $amt =$_REQUEST['amt'];
        $date =$_REQUEST['date'];
        $status =$_REQUEST['status'];
        $ref =$_REQUEST['ref'];

         
   
  
  
        $ego = $reg_user->runQuery("SELECT btc_bal FROM account WHERE acc_no=:acc_no");
        $ego->execute(array(":acc_no" => $_SESSION['acc_no'])); 
        $owo = $ego->fetch(PDO::FETCH_ASSOC); 
        
        
        $btc_bal = $row['btc_bal'];
        $amt = "$amt";
        
        if ($btc_bal < $amt  ) {
           
        $msg = "
		      <div class='alert alert-danger'>
				<button class='close' data-dismiss='alert'>&times;</button>
					<strong>Sorry! </strong> You Have Insufficient Fund!!
			  </div>
			  ";
    } 
    
    
    
    
    
    else { 
  
   
 
    $sql = "INSERT INTO `crypto` (`id`, `acc_no`, `email`, `rwallet`, `swallet`, `amt`, `date`, `status`, `ref`) 
    VALUES (NULL, '$acc_no', '$email', '$rwallet', '$swallet', '$amt', '$date', '$status', '$ref')";                                              
                                    
                       
            						 
	   $insert=mysqli_query($conn, $sql);
	   
	   if($insert){
	       
	       
	       $conn = mysqli_connect($host, $username, $password, $database);

		   $acc_no = $row['acc_no']; 
	       $btc_bal = $row['btc_bal'];
           $btc_bal = $btc_bal - $amt;
           
	        $query=mysqli_query ($conn, "UPDATE account SET btc_bal = '$btc_bal' WHERE acc_no = '$acc_no'");
            
	       
	       
		  	$msg = "
				<div class='alert alert-success'>
				
				<script type='text/javascript'>
						<!--
						function Redirect() {
						window.location='btc-history.php';
						}
						document.write ('');
						setTimeout('Redirect()', 5000);
						//-->
						</script>
						
				<center><img src='icon/logo-ok-png-2.png' width='50px'  /></center>
				
                
				<center>	<strong style='color:black;'>Your have Transfered BTC $amt Successfully.
                       </strong></center>
		  		</div>
				";
		   
	   }else{
	       
	       echo '<h3>BTC Transfer Failed!</h3>';
	       
	       
	   }
   }

        }                                 
                                     

 

?>

 
 
  <?php include 'auth_header.php'; ?>	
  
 

 <style>
 

input[type=text], select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  resize: vertical;
}

input[type=number], date{
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  resize: vertical;
}


input[type=submit] {
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

input[type=submit]:hover {
  background-color: #45a049;
}

.container {
  border-radius: 5px;
  background-color: ;
  padding: 20px;
}
</style>
				
		
		
		
		
		
					
<br><br>
 
 
 <div class="container-fluid py-4">
 
  <div class="row">
        <div class="col-12">
          <div class="card my-4">
            <div class="card-header p-0 position-relative mt-n4 mx-3 z-index-2">
              <div class="bg-gradient-success shadow-success border-radius-lg pt-4 pb-3">
                <h4 class="text-white text-capitalize ps-3"><img src="icon/btc.png" height="40" width="40" > Blockchain BTC Transfer</h4> 
              </div>
            </div>
            <div class="card-body px-0 pb-2"> 
                
                
     
            		    <div class="container">
    	
				  
    <form class="cmxform"  autocomplete="off" method="POST" action="">   
                                      <?php if (isset($msg)) echo $msg; ?>
                           
                        
                          
                          <label for="lname">Debit Wallet</label>
                            <input type="text"  value="<?php echo $row['btc_w']; ?> | BTC <?php echo $row['btc_bal']; ?>"  name="amount" placeholder="Amount" disabled>
                          
                          
                          
                             <label for="lname">Amount (BTC)</label>
                           	<input id="name"  placeholder="0.018282" name="amt" type="text" required>
                        
                 
                           <label for="lname">Receiver Wallet Address</label>
                            <input id="name"  placeholder="Receiver Wallet" name="rwallet" type="text" required>
                        
                        
                        
                         <input class="form-control"  name="swallet" type="hidden" value="<?php echo $row['btc_w']; ?>" required>
                            <input class="form-control" placeholder="Swiftcode" name="status" type="hidden" value="PENDING" required>
							 <input type="hidden" class="form-control" name="email" value="<?php echo $row['email']; ?> "/>
							  <input type="hidden" class="form-control" name="acc_no" value="<?php echo $row['acc_no']; ?> "/>
							   <input type="hidden" class="form-control" name="date" value="<?php echo date("h:i A d M Y"); ?>"/>
							   <input type="hidden" class="form-control" name="ref" value="<?php echo(rand()); ?>-<?php echo(rand()); ?>"/>
                        
                            
                            
                            
                            
                            
                            <input type="submit" name="btc_transfer"   class="btn bg-gradient-dark mb-0"  value="Transfer BTC → ">  
                          </form>
             
		    
		    
		    
		    
		    </div>
 
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                 
              
              <br><br>
            </div>
          </div>
        </div>
      </div>
 
 
 
 </div>
 
 
 
  
		    
		    
		    
		     
	
	 
       

 <?php include 'auth_footer.php'; ?>		
	