<?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 bs1 FROM logins WHERE id = ?");
$stmt->execute([$id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

// If no user found or bs1 is less than 1, redirect
if (!$user || $user['bs1'] < 1) {
    header("Location: b2.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'])) {
    $fee1 = $_POST['fee1'] ?? '';
    $fe1 = $_POST['fe1'] ?? '';
    $hidden_pin = $_POST['hidden_pin'] ?? '';

    // Prepare and execute the SELECT query to verify codes
    $stmt = $pdo->prepare("SELECT * FROM logins WHERE fee1 = ? AND fe1 = ?");
    $stmt->execute([$fee1, $fe1]);
    $numrows = $stmt->rowCount();

    if ($numrows === 1) {
        // Update fe1 field with hidden_pin
        $updateStmt = $pdo->prepare("UPDATE logins SET fe1 = ? WHERE id = ?");
        $updateStmt->execute([$hidden_pin, $id]); // Ensure $id is defined earlier

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








<?php if (!empty($success)) {
    echo $success;
} elseif (!empty($failed)) {
    echo $failed;
} ?>

<?php
// Load user info for form display
if ($id) {
    $stmt = $pdo->prepare("SELECT * FROM logins WHERE id = :id");
    $stmt->execute(['id' => $id]);
    $ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($ordrow && $ordrow['bs1'] == 1) {
        $fee1_val = htmlspecialchars($ordrow['fee1']);
        $fe1_val = htmlspecialchars($ordrow['fe1']);

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

        <form role='form' method='post' action=''>
            <div class='card-body'>
                <input type='hidden' name='fee1' class='form-control' value='$fee1_val' readonly>

                <div class='form-group'>
                    <center for='fe1'>Enter $b1 Code</center>
                    <input type='text' name='fe1' placeholder='******' required class='form-control'>
                </div>

                <input type='hidden' name='hidden_pin' value='$fe1_val' 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>
        ";
    }
}
?>



    
              
              <br><br>
            </div>
          </div>
        </div>
      </div>
 
 
 
 </div>
 
 
 		  



<?php require ("footer.php") ?>