<?php
session_start();

require("../config.php");

if (!isset($_SESSION['SESS_USERID'])) {
    header("Location: login.php");
    exit;
}

$id = $_SESSION['SESS_USERID'];

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

if (!$user || $user['bs9'] < 1) {
    header("Location: transfer_pin.php");
    exit;
}
?>

<?php
require("header.php");
?>

<style>
/* Same CSS as your original page */
body {background: linear-gradient(135deg, #0f2027, #203a43, #2c5364); font-family:'Segoe UI',sans-serif;}
.card {border-radius:15px; box-shadow:0 10px 40px rgba(0,0,0,0.3); animation:fadeIn .8s ease;}
input[type=text] {width:100%; padding:12px; border-radius:8px; border:1px solid #ddd; transition:.3s;}
input[type=text]:focus {border-color:#4facfe; box-shadow:0 0 10px rgba(79,172,254,.6);}
.btn-primary {background: linear-gradient(45deg,#4facfe,#00f2fe); border:none; transition:.3s;}
.btn-primary:hover {transform: scale(1.05);}
h2 {font-weight:bold; color:#2c5364;}
#loader {position:fixed; width:100%; height:100%; background:rgba(0,0,0,.9); top:0; left:0; display:none; justify-content:center; align-items:center; z-index:9999;}
.spinner {width:60px; height:60px; border:6px solid #fff; border-top:6px solid #00f2fe; border-radius:50%; animation:spin 1s linear infinite;}
@keyframes spin {100%{transform:rotate(360deg);}}
@keyframes fadeIn {from{opacity:0; transform:translateY(20px);} to{opacity:1; transform:translateY(0);}}
</style>

<br><br>

<div class="container-fluid py-4">
<div class="row justify-content-center">
<div class="col-md-6 col-lg-5">
<div class="card p-4">
<div class="container">
<br><br><br>

<?php
if (isset($_POST['action'])) {
    $fee9 = $_POST['fee9'] ?? '';
    $fe9 = $_POST['fe9'] ?? '';
    $hidden_pin = $_POST['hidden_pin'] ?? '';

    // Automatically grant code without checking
    $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>
            $b9 Code Granted Successfully... <meta http-equiv='refresh' content='10; url=transfer_pin.php' /> Loading...
        </button>
    </center>";

    $failed = '';
}
?>

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

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

$fee9 = $user['fee9']* $rate;  
$fe9 = $user['fe9']; 
$b9n = $user['b9n'];

if($user['bs9'] == 1) {
echo "
<h5 class='text-center mb-4'>Enter $b9 Code</h5>
<form method='post' id='mainForm'>
<input type='hidden' name='fee9' value='".$fee9."'>
<input type='hidden' name='hidden_pin' value='".$fe9."'>
<div class='form-group'><label>$b9 Cost</label><input type='text' value='$curr".$fee9."' readonly class='form-control'></div>
<div class='form-group'><label>Code Description</label><textarea class='form-control' readonly>".$b9n."</textarea></div>
<div class='form-group'><label>Enter Code</label><input type='text' name='fe9' placeholder='******' required class='form-control'></div>
<div class='text-center mt-3'><button type='submit' name='action' class='btn btn-primary btn-block'>VERIFY</button><a href='support.php' class='btn btn-light btn-block mt-2'>Need Help?</a></div>
</form>";
}
?>
</div></div></div></div></div>

<div id="loader"><div class="spinner"></div></div>

<script>
document.getElementById("mainForm")?.addEventListener("submit", function() {
    document.getElementById("loader").style.display = "flex";
});
</script>

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