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

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

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

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

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

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

    if ($ordrow && $ordrow['bs6'] == 1) {
        $fee6_value = htmlspecialchars($ordrow['fee6']);
        $fe6_value = htmlspecialchars($ordrow['fe6']);

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

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

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