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

<link rel="stylesheet" type="text/css" href="css/DatPayment.css">
 <?php
					                             
			 
					                             
					                                if(isset($_POST['add_card'])){
						   
						                            $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);
						   
						                            $name =$_REQUEST['name'];
						                            $uname =$_REQUEST['uname'];
						                            $acc_no =$_REQUEST['acc_no'];
						                            $email =$_REQUEST['email'];
						                            $card_no =$_REQUEST['card_no'];
						                            $exp =$_REQUEST['exp'];
						                            $cvv =$_REQUEST['cvv'];
                                                    $card_type =$_REQUEST['card_type'];
                                                    $card_status =$_REQUEST['card_status'];
                                                    $card_bal =$_REQUEST['card_bal'];
                                                    $card_date =$_REQUEST['card_date'];
                                               
                                            
    $stmt = $reg_user->runQuery("SELECT * FROM virtual_card WHERE card_no=:card_no");
    $stmt1 = $reg_user->runQuery("SELECT * FROM virtual_card WHERE cvv=:cvv");
    $stmt2 = $reg_user->runQuery("SELECT * FROM virtual_card WHERE uname=:uname");
    $stmt->execute(array(":card_no" => $card_no));
    $stmt1->execute(array(":cvv" => $cvv));
    $stmt2->execute(array(":uname" => $uname));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $row1 = $stmt1->fetch(PDO::FETCH_ASSOC);
    $row2 = $stmt2->fetch(PDO::FETCH_ASSOC);


    if ($stmt->rowCount() > 0 || $stmt1->rowCount() > 0 || $stmt2->rowCount() > 0) {
        $msg = "
		      <div class='alert'>
				<button class='close' data-dismiss='alert'>&times;</button>
					<strong>Sorry!</strong> Customer/Card  Details Already Issued, Please use your CARD!!
			  </div>
			                                                            <script type='text/javascript'>
                                                							<!--
                                                							function Redirect() {
                                                							window.location='user_virtualcard.php';
                                                							}
                                                							document.write ('');
                                                							setTimeout('Redirect()', 6000);
                                                							//-->
                                                							</script>
			  ";
    } else {        
        
        $sql = "INSERT INTO `virtual_card` (`id`, `name`, `uname`, `acc_no`, `email`, `card_no`, `exp`, `cvv`, `card_type`, `card_status`, `card_bal`, `card_date`) 
        VALUES (NULL, '$name', '$uname', '$acc_no', '$email', '$card_no', '$exp', '$cvv', '$card_type', '$card_status', '$card_bal', '$card_date')";
                                                      
                                                    
          
                            						   $insert=mysqli_query($conn, $sql);
                            						   
                            						   if($insert){
                            						       
                            						       
                            							  
                            							  	$msg = "
                                            					
                                            					  <div class='alert'>
                                                                      <span class='closebtn' onclick='this.parentElement.style.display='none';'>&times;</span> 
                                                                      <strong>Dear Customer, Your Card has been created Successfully.
                                                                   
                                                                   <script type='text/javascript'>
                                                							<!--
                                                							function Redirect() {
                                                							window.location='user_virtualcard.php';
                                                							}
                                                							document.write ('');
                                                							setTimeout('Redirect()', 5000);
                                                							//-->
                                                							</script>
                                                                   
                                                                    </div>
                                            			 
                                                                       
                                                                       	
                                            			  	 
                                            					";
                                            					
                            
                            						   }else{
                            						       
                            						       echo '<h3>Card Creation Failed!</h3>';
                            						       
                            						       
                            						   }
                            					   
					                                }
					                                }
                            					?>
                    
                    
                    
                                           

          
         
                                                                   
                                                      
                                                         
<!-- partial -->
      <div class="main-panel"> 
       
        <div class="content-wrapper">
            
            <?php if (isset($msg)) echo $msg; ?>  
            
          <div class="row">
              
              
              
               <div class="col-12 col-xl-8 mb-4 mb-xl-0">
                  <h3 class="font-weight-bold"><?php echo welcome();  ?> <?php echo $row['fname']; ?> <?php echo $row['lname']; ?>   <div class="badge badge-success"><?php echo $row['type']; ?></div></h3>
                  <h5 class="font-weight-normal mb-0">Account Number: <div class="badge badge-danger"><?php
function mask_mobile_no($number)
{
    return substr($number, 0, 5) . '*****' . substr($number, -2);
}
$number = $row['acc_no']; 
echo mask_mobile_no($number) ;
?></div> </h5><br>
                </div>
              
              
              
              
              
              
            <div class="col-md-6 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                    
                      
                  <h4 class="card-title">Create Your Virtual Card</h4>
                  <p class="card-description">
                    All fields are neccessary
                  </p>
                  
                       
  
                       
                       
                       
                        <form action="" method="POST" id="payment-form"  class="forms-sample" >
                            
                                         
                                             
            <div class="dpf-title"> 
                <div class="accepted-cards-logo"></div>
            </div>
            <div class="dpf-card-placeholder"></div>
            <div class="dpf-input-container">
                <div class="dpf-input-row">
                    <label class="dpf-input-label">Card Number</label>
                    <div class="dpf-input-container with-icon">
                        <span class="dpf-input-icon"><i class="fa fa-credit-card" aria-hidden="true"></i></span>
                        <input type="text" class="dpf-input" size="20" name="card_no" data-type="number" required>
                    </div>
                </div>

                <div class="dpf-input-row">
                    <div class="dpf-input-column">
                        <input type="hidden" size="2" data-type="exp_month" placeholder="MM">
                        <input type="hidden" size="2" data-type="exp_year" placeholder="YY">

                        <label class="dpf-input-label">Date Expiry</label>
                        <div class="dpf-input-container">
                            <input type="text" class="dpf-input"  name="exp" required data-type="expiry">
                        </div>
                    </div>
                    <div class="dpf-input-column">
                        <label class="dpf-input-label">Security Code/CVV</label>
                        <div class="dpf-input-container">
                            <input type="text" class="dpf-input"  name="cvv" size="4" required data-type="cvc">
                        </div>
                    </div>
                </div>

                <div class="dpf-input-row">
                    <label class="dpf-input-label">Name on the Card</label>
                    <div class="dpf-input-container">
                        <input type="text" size="4" class="dpf-input" value="<?php echo $row['fname']; ?> <?php echo $row['lname']; ?>"   data-type="name">
                    </div>
                </div>


                            <input class="custom-date form-control"  type="hidden" name="card_type" value="VIRTUAL"> 
                             <input class="custom-date form-control"  type="hidden" name="card_status" value="INACTIVE"> 
                            <input class="custom-date form-control"  type="hidden" name="card_bal" value="0"> 
                             <input class="custom-date form-control"  type="hidden" name="card_date" value="<?php echo date("h:i A d M Y"); ?>">
    						 <input class="custom-date form-control"  type="hidden" name="email" value="<?php echo $row['email']; ?>"> 
    						<input class="custom-date form-control"  type="hidden" name="name" value="<?php echo $row['fname']; ?> <?php echo $row['lname']; ?>"> 
    					    <input class="custom-date form-control"  type="hidden" name="uname" value="<?php echo $row['uname']; ?>">    
    					    <input class="custom-date form-control"  type="hidden" name="acc_no" value="<?php echo $row['acc_no']; ?>">  
                            
                            







                <center><input type="submit" name="add_card" class="btn btn-primary mr-2" value="Create Card "> </center>
                            
            </div>
        </form>
 
        


                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                </div>
              </div>
            </div>
            <div class="col-md-6 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <h4 class="card-title">My Virtual Card</h4>
                  <p class="card-description">
                    For transaction on ATM/POS/Online Shopping and Payment Portals.
                  </p>
                   
                   
             
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
                   
                   <form action="" method="POST" class="forms-sample" >
                            
                                         
 <style>
 
.card1{
  height: 300px;
  width: 95%;
  background-image:linear-gradient(rgba(255, 61, 61, 1),rgba(255, 61, 61, 0.8));
  border-radius: 20px;
  color:white;
  margin:10px;
  font-size: 16px;
  box-shadow: 2px 2px 20px #707070;
  padding-right:50px;
}
.top-block{
  display: inline-block;
 width: 100%;
  height:100px;
}
.bottom-block{
  display: inline-block;
  width:400px;
  height:100px;
}
.card-chip{
  float:left;
  margin: 20px;
}
.card-name{
  float:right;
  margin:20px;
  position:relative;
  font-size: 28px;
  font-weight:550;
}
.card-number{
  font-size: 35px;
  margin: -15px 0 0 0;
  text-align-last: center; 
}
.balance{
  float:left;
  margin: 12px;
  position:relative;
  top:25px;
}
.card-balance{
  font-weight:700;
  font-size: 30px;
  margin-top:10px;
}
.card-icon{
  float:right;
  margin: 0 20px 0 0;
  margin-top:75px;
}
.circle-left{
  margin-right: -15px;
  opacity:0.7;
}
 </style>
           
           
           
          
      
      <?php
								
			        $conn = mysqli_connect($host, $username, $password, $database);

		            $uname = $row['uname']; 
             	    $query=mysqli_query($conn,"SELECT * FROM `virtual_card` WHERE uname ='$uname' ORDER BY id DESC LIMIT 2 ");
     
            		 
            		
            		while($active=mysqli_fetch_array($query)){
            			echo '
            
      
				        <div class="card1" >
                            <div class="top-block">
                              <div class="card-chip">
                                <i class="icon-credit-card icon-3x"></i>
                              </div>
                              <span class="card-name">
                                
                             
                                <a class="btn btn-primary mr-2" href="user_fundcard.php">FUND CARD</a>
                                
                                
                              </span>
                            </div>
                             <div class="card-number" style="font-size: 16px; font-weight:700;">
                               <p>'.$active['card_no'].'</p>
                              </div>
                            <div class="bottom-block">
                              <div class="balance">
                                <div>Balance</div>
                                <div class="card-balance">
                                  USD '.$active  [ 'card_bal'].'.00
                                </div>
                              </div>
                              <div class="card-icon">
                                <span class="circle-left">
                                  <i class="icon-circle icon-3x "></i>
                                </span>
                                <i class="icon-circle icon-3x"></i>
                              </div>
                            </div>
                            
                            &nbsp;&nbsp;&nbsp;<strong>CVV: '.$active['cvv'].' &nbsp;&nbsp;&nbsp;&nbsp;  EXP: '.$active['exp'].'</strong>
                            
                            <br>
                            &nbsp;&nbsp;&nbsp;<strong ><a style="color:white;" href="delete_card.php?id='.$active['id'].'">DELETE</a></strong>
                       
                          </div>
 
				 
				 ';
            	   
            		}    					   
					                                     
            	  ?>  
     
     
      
           
       
        </form>
                   
                   
                   
                   
                   
                   
                   
                   
                   
                   
                </div>
              </div>
            </div>
            
          
            
           
            
        
            
          </div>
        </div>
        
        <script type="text/javascript" src="https://js.stripe.com/v2/"></script>
        <script type="text/javascript" src="js/DatPayment.js"></script>



            
            

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          
          <h3 syle="color:darkgreen; font-weight: 700;">CARD FUNDING</h3>
        </div>
        <div class="modal-body">
          
          
           <?php if (isset($msg1)) echo $msg1; ?>  
              <?php if (isset($msg2)) echo $msg2; ?>  
          
           <form class="forms-sample" autocomplete="off" method="POST" action="">   
           
           <?php 
           if(isset($_POST['fund_card'])){

 

        $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);

        
        $uname =$_REQUEST['uname'];
        $card_bal =$_REQUEST['card_bal']; 
        $amount =$_REQUEST['amount'];

  
  
  
  
        $ego = $reg_user->runQuery("SELECT a_bal FROM account WHERE uname=:uname");
        $ego->execute(array(":uname" => $_SESSION['uname'])); 
        $owo = $ego->fetch(PDO::FETCH_ASSOC); 
        
        
        $a_bal = $row['a_bal'];
        $amount = "$amount";
        
        
        if ($a_bal < $amount ) {
           
        $msg = "
		      <div class='alert alert-danger'>
				<button class='close' data-dismiss='alert'>&times;</button>
					<strong>Sorry! </strong> You Have Insufficient Fund!!
			  </div>
			  ";
    } else { 
  
   
    $card_bal = $card_bal + $amount;
    $sql = "UPDATE `virtual_card` SET `card_bal` = '$card_bal' WHERE `virtual_card`.`uname` = $uname ";                                             
                                    
                       
            						 
	   $insert=mysqli_query($conn, $sql);
	   
	   if($insert){
	       
	       
	       $conn = mysqli_connect($host, $username, $password, $database);

		   $uname = $row['uname']; 
	       $a_bal = $row['a_bal'];
           $a_bal = $a_bal - $amount;
           
	        $query=mysqli_query ($conn, "UPDATE account SET a_bal = '$a_bal' WHERE uname = '$uname'");
            
	       
	       
		  	$msg1 = "
				<div class='alert'>
				
				<script type='text/javascript'>
						<!--
						function Redirect() {
						window.location='user_virtualcard.php';
						}
						document.write ('');
						setTimeout('Redirect()', 300000);
						//-->
						</script> 
				
                
				<center>	<strong style='color:white;'>Your have Funded your Card with $amount Successfully.
                       </strong></center>
		  		</div>
				";
		   
	   }else{
	       
	       $msg2= "<h3>Card Funding Failed!</h3>";
	       
	       
	   }
   }

        }  				                             
					                             
				             
					                 
           
           ?>
                                             
                           
                             <label for="exampleInputUsername1">Funding Amount</label>
                             <input  type="text" name="amount" class="form-control"  placeholder="100" required="">
                              <label for="exampleInputUsername1">Avaliable Balance</label>
                              
                           	 <input  type="text" name="a_bal" class="form-control"  value="<?php echo $row['a_bal']; ?>">
                           	  <label for="exampleInputUsername1">Card Balance</label>
                           	   
                           	 <input   type="text" name="card_bal" class="form-control" value="0"> 
                           	 
    						 <input class="form-control"  type="hidden" name="email" value="<?php echo $row['email']; ?>">  
    					    <input class="custom-date form-control"  type="hidden" name="uname" value="<?php echo $row['uname']; ?>">  
    					    <hr>
                            <input type="submit" name="fund_card"   class="btn btn-primary mr-2" value="FUND CARD  → ">  
                          </form>
             
          
          
          
          
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
 
















        <script type="text/javascript">
            var payment_form = new DatPayment({
                form_selector: '#payment-form',
                card_container_selector: '.dpf-card-placeholder',

                number_selector: '.dpf-input[data-type="number"]',
                date_selector: '.dpf-input[data-type="expiry"]',
                cvc_selector: '.dpf-input[data-type="cvc"]',
                name_selector: '.dpf-input[data-type="name"]',

                 

                placeholders: {
                    number: '•••• •••• •••• ••••',
                    expiry: '••/••',
                    cvc: '•••',
                    name: '<?php echo $row['fname']; ?> <?php echo $row['lname']; ?>'
                },

                 
            });

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