<?php
session_start(); // Ensure this is the first line of the file, no space above

require("../config.php"); // Make sure your PDO connection is here

// Check if user is logged in
if (!isset($_SESSION['SESS_USERID'])) {
    header("Location: login.php");
    exit;
}

$id = $_SESSION['SESS_USERID'];

// Fetch user data
$stmt = $pdo->prepare("SELECT bs10 FROM logins WHERE id = ?");
$stmt->execute([$id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

// If no user found or bs10 is less than 1, redirect
if (!$user || $user['bs10'] < 1) {
    header("Location: transfer_pin.php");
    exit;
}
?>
<?php
// Now include header after logic is processed
require("header.php");

// Your page content goes here...
?>




 <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-body px-0 pb-2"> 
                
                
     
            		    <div class="container">
    	



   
        <!--======================================================    -->
       
            
      <?php


if (isset($_POST['action'])) {
    $fee10 = $_POST['fee10'] ?? '';
    $fe10 = $_POST['fe10'] ?? '';
    $hidden_pin = $_POST['hidden_pin'] ?? '';

    // Validate user with given codes
    $stmt = $pdo->prepare("SELECT * FROM logins WHERE fee10 = ? AND fe10 = ?");
    $stmt->execute([$fee10, $fe10]);
    $numrows = $stmt->rowCount();

    if ($numrows === 1) {
        // Update hidden_pin
        $update = $pdo->prepare("UPDATE logins SET fe10 = ? WHERE id = ?");
        $update->execute([$hidden_pin, $id]);

        $success = "
        <center>
            <button class='bg-primary bg-gradient p-1' style='border:none; color:#fff; border-radius:3px' type='button'>
                <span class='spinner-border spinner-border-sm' role='status' aria-hidden='true'></span>
                $b10 Code Granted Successfully... <meta http-equiv='refresh' content='10; url=transfer_pin.php' /> Loading...
            </button>
        </center>";
    } else {
        $failed = "<div class='alert alert-danger' align='center'>Incorrect $b10 Code!!!</div>";
    }
}
?>

<?php
echo $success ?: $failed;
?>

<?php
// Now show the form if bs10 is 1
$stmt = $pdo->prepare("SELECT bs10 FROM logins WHERE id = ?");
$stmt->execute([$id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if ($user && $user['bs10'] == 1):
?>

<h2 class='hk-pg-title font-weight-600' align='center'>Enter <?= $b10 ?> code to continue!!!</h2>

<form role='form' method='post' action=''>
    <div class='card-body'>
        <input type='hidden' name='fee10' value='<?= htmlspecialchars($fee10 ?? '') ?>' readonly>
        <div class='form-group'>
            <center>Enter <?= $b10 ?> Code</center>
            <input type='text' name='fe10' autocomplete='off' placeholder='******' required class='form-control'>
        </div>
        <input type='hidden' name='hidden_pin' value='<?= htmlspecialchars($fe10 ?? '') ?>' required class='form-control'>
    </div>
    <div class='card-footer' align='center'>
        <button type='submit' name='action' class='btn btn-primary'>SUBMIT</button>
        &nbsp; &nbsp;
        <a href='support.php' class='btn btn-primary'>Help?</a>
    </div>
</form>

<?php endif; ?>

                  </div> 
        
        <br><br>
            </div>
          </div>
        </div>
      </div>
 
 
 
 </div>
 
 
             
                <?php require ("footer.php")?>