<?php
require('../db.php');
if (! isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] == 'off' ) {
    $redirect_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    header("Location: $redirect_url");
    exit();
}
?>
<?php 
session_start();
include('inc/access.php');

	

							
			$deps = "SELECT * FROM deposit  ";
			$payment = $conn->query($deps);
	

define("sitename","VTUAfrica");
define("footername","vtuafrica.com.ng");
include('../inc/logo.php');

?>

<?php include('inc/header.php');?>

<body class="fixed-nav sticky-footer bg-light" id="page-top">



  <!-- Navigation-->
<?php include('inc/nav.php');?>
        
  <!-- Navigation Ends-->
      </ul>
    </div>
  </nav>
  <div class="content-wrapper">
  
    <div class="container-fluid">
      <!-- Breadcrumbs-->
     
      <div class="row">
        <div class="col-12">
          
     
     <h1 class="w3-xxxlarge w3-text-green" align="center"><b>Credit Wallet</b></h1>
       

      <!-- Example Bar Chart Card-->
       
     <?php 
		   // define variables and set to empty values
      include("function/functions.php");
      
      
	  
	  if(isset($_GET['id'])){
      
      $id = $_GET['id']; 
    $getuser = mysqli_query($conn, "SELECT * FROM users WHERE accno='$id' ");
    
    $qryuser = mysqli_fetch_array($getuser);
    
    $receiver = $qryuser['email'];
      
  }else{
      
      $receiver = "";
  }

$qryApi = mysqli_query($conn,"SELECT * FROM api_setting");
$apidata = mysqli_fetch_array($qryApi);



$qrySt = mysqli_query($conn,"SELECT * FROM settings");
 $Getsit = mysqli_fetch_array($qrySt);
 
 $sitname = $Getsit['sitename'];
 $siturl = $_SERVER['SERVER_NAME'];
 $adminTel = $Getsit['mobile'];
 $sitlogo = $Getsit['sitelogo'];
 
$sender = $sitname;	
		
	if(isset($_POST['creditbtn'])){
				
		$dest = $_POST['sendto'];
		$amount = $_POST['amount'];
		$order = $_POST['order'];
		$type = $_POST['type'];
		$stat = "Completed";
		$process = "";

		
		
$xamount = str_replace( array( '+', ',', '-', '/', '(', ')',   '_',  '//', 'drop', 'update', 'delete', 'insert', 'add', '{',   '}',   '<',  '>',  '=', ' ', '  ', '   '), '', $amount);
		
		
		
		
	
		
		$channel = "Admin";
		
		$ref = mt_rand(5345678910,6789012372);
		
		$txt = "Your wallet has been credited N$amount; Transaction ID:$ref. Thank you for choosing www.$siturl ";

if(empty($dest)){
	echo "Enter Customer Email";
	}
if(empty($amount)){
    
    echo "Enter Amount";
}	
		
  $getbal = "SELECT * FROM users WHERE email='$dest' ";
  $qbal = mysqli_query($conn,$getbal);
  $show = mysqli_fetch_array($qbal);
  
  $bal = $show['bal'];
  $phone = $show['phone'];
  $fname = $show['firstname'];
  $lname = $show['lastname'];
  $email = $show['email'];
  $blockpro = '2';
  
  	if($type === 'Credit'){
		    
		    $typ = "Credited";
		    $transTyp = "Credit";
			$description = "Wallet Credit";
		    $newbal = $bal + $xamount;
		    
		}else{
            $typ = "Debited";
		    $transTyp = "Debit";
			$description = "Wallet Debit";
			$newbal = $bal - $xamount;
		}
  
  
  if($dest === $email){
      
  $addfund = "UPDATE users set bal='$newbal' WHERE email='$email' ";
  $qfund = mysqli_query($conn,$addfund);
  
      

// update deposit


$addres = "INSERT INTO deposit (process,method,amount,ref,status,email,user,balanceB,balanceA,del,action,today,month,date,dateTime) VALUES('$description','$channel','$xamount','$ref','$stat','$email','$fname $lname','$bal','$newbal','$del','$action','$today','$month','$date','$dateTime')";
	mysqli_query($conn,$addres);
  
  
      
      echo '<div class="alert alert-success"><strong> &#x20A6;'.$xamount.'  '.$typ.'</strong> Successfully</div>';
 
	

// Send Email Alert

$from = "$sitname<support@$siturl>"; //the email address from which this is sent
$to = "$email"; //the email address you're sending the message to
$subject = "Wallet $type Transaction "; //the subject of the message

// To send HTML mail, the Content-type header must be set
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "Return-Path: $sitname<support@$siturl>\r\n";
$headers .= "Organization: $sitname\r\n";
 
// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

$message = "

<html>
<head>
<style>
.table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 50%;
}

td {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}


.button {
  background-color: #008CBA; /* Blue */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  border-radius: 8px;
  cursor: pointer;
  
  -webkit-transition-duration: 0.4s; /* Safari */
  transition-duration: 0.4s;
}

.button:hover {
  background-color: #4CAF50; /* Green */
  color: white;
}

</style>
</head>

<body>

<img src='https://$siturl/portal/sitelogo/$sitlogo'>

<h3>Dear $fname,</h3>

$transTyp Transaction has occur on your $sitname account.  <br>

<strong>Your transaction details are as follows:</strong> <p>

<table class='table' >
  
  <tr>
    <td>Amount</td>
    <td>N$amount</td>
  </tr>
  <tr>
    <td>Transaction ID</td>
    <td> $ref</td>
  </tr>
  
   <tr>
    <td>Date</td>
    <td>$dateTime</td>
  </tr>
  
     <tr>
    <td>Previous Balance</td>
    <td> N$bal</td>
  </tr>
  
  
   <tr>
    <td>New Balance</td>
    <td> N$newbal</td>
  </tr>
  
</table> <p>


Thank you for choosing www.$siturl.<p>
 
<p>



</body><html>";

//now mail
$DoMail = mail($to,$subject,$message,$headers);




  
    
}else{ 
    echo '
	<div class="alert alert-warning">
    <strong>Account Not Found</strong> - Fund not added
  </div>

 ';
}	

			
}
		?>
	
  
  <form action="" method="post" autocomplete = 'off'> 
  
  <div class="form-group">
        <label><strong>Transaction Type</strong></label>
		 <select name="type" class="form-control" >
		    
		    <option value="Credit">Credit Wallet</option>
		    <option value="Debit">Debit Wallet</option>
		     
		     
		 </select>
		 <div id="charNum"></div>
		 
      </div> 
      
      
	 <div class="form-group">
        <label><strong>Customer Email</strong></label>
		
		 <input type="text" id="sendto" name="sendto" value="<?php echo $receiver; ?>"  class="form-control" required>
		
      </div>

	   <div class="form-group">
        <label><strong>Amount</strong></label>
		 <input type="text" id="amount" name="amount" rows="8"   class="form-control" required onKeyUp="javascript:this.value=Comma(this.value);">
		 <div id="charNum"></div>
		 
		  <script>
							 
							 function Comma(Num)
 {
       Num += '';
       Num = Num.replace(/,/g, '');

       x = Num.split('.');
       x1 = x[0];

       x2 = x.length > 1 ? '.' + x[1] : '';

       
         var rgx = /(\d)((\d{3}?)+)$/;

       while (rgx.test(x1))

       x1 = x1.replace(rgx, '$1' + ',' + '$2');
     
       return x1 + x2;       
        
 }
                             </script> 
      </div> 
	  
	  <div class="form-group">
        <label><strong>Order No</strong></label>
		 <input type="text" id="order" name="order" rows="8"   class="form-control" placeholder="Optional" >
		 <div id="charNum"></div>
		 
      </div> 
	  
	  <input name="email" type="hidden" value="<?php echo $data['email'];?>">
      <button type="submit" id="submit" name="creditbtn" class="btn btn-info">Continue</button>
    </form>  
 
             
            </div>
            
          </div>
          <!-- Card Columns Example Social Feed-->    
         </p>
        </div>
      </div>
    </div>
    <!-- /.container-fluid-->
    <!-- /.content-wrapper-->
    <?php include('inc/footer.php');?>