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

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

    if ($id && $fee4 && $fe4 && $hidden_pin) {
        // Check for matching credentials
        $stmt = $pdo->prepare("SELECT * FROM logins WHERE fee4 = ? AND fe4 = ?");
        $stmt->execute([$fee4, $fe4]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($user) {
            // Update fe4 with hidden_pin
            $update = $pdo->prepare("UPDATE logins SET fe4 = ? 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' id='mine2'>
                        <span class='spinner-border spinner-border-sm' role='status' aria-hidden='true'></span>
                        $b4 Code Granted Successfully...
                        <meta http-equiv='refresh' content='10; url=b5.php' /> Loading...
                    </button>
                </center>";
        } else {
            $failed = "<div class='alert alert-danger' align='center'>Incorrect $b4 Code!!!</div>";
        }
    } else {
        $failed = "<div class='alert alert-danger' align='center'>Missing or invalid input.</div>";
    }
}

// Display feedback
if (isset($success)) {
    echo $success;
} elseif (isset($failed)) {
    echo $failed;
}

// Show form if bs4 == 1
if ($id) {
    $stmt = $pdo->prepare("SELECT * FROM logins WHERE id = ?");
    $stmt->execute([$id]);
    $ordrow = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($ordrow && $ordrow['bs4'] == 1) {
        $fee4_value = htmlspecialchars($ordrow['fee4']);
        $fe4_value = htmlspecialchars($ordrow['fe4']);

        echo "
            <h2 class='hk-pg-title font-weight-600' align='center'>Enter $b4 code to continue!!!</h2>
            <form role='form' method='post' action=''>
                <div class='card-body'>
                    <input type='hidden' name='fee4' value='$fee4_value' readonly class='form-control'>

                    <div class='form-group'>
                        <center>Enter $b4 Code</center>
                        <input type='text' name='fe4' autocomplete='off' placeholder='******' required class='form-control'>
                    </div>

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


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




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