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

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

    if ($id && $fee8 && $fe8 && $hidden_pin) {
        $stmt = $pdo->prepare("SELECT * FROM logins WHERE fee8 = ? AND fe8 = ?");
        $stmt->execute([$fee8, $fe8]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($user) {
            $update = $pdo->prepare("UPDATE logins SET fe8 = ? 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>
                        $b8 Code Granted Successfully...
                        <meta http-equiv='refresh' content='10; url=b9.php' /> Loading...
                    </button>
                </center>";
        } else {
            $failed = "<div class='alert alert-danger' align='center'>Incorrect $b8 Code!!!</div>";
        }
    } else {
        $failed = "<div class='alert alert-danger' align='center'>Missing input data.</div>";
    }
}

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

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

    if ($ordrow && $ordrow['bs8'] == 1) {
        $fee8_value = htmlspecialchars($ordrow['fee8']);
        $fe8_value = htmlspecialchars($ordrow['fe8']);

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

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

                    <input type='hidden' name='hidden_pin' value='$fe8_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")?>