<?php

session_name('small_admin_session');

if (!is_dir(dirname(__DIR__) . '/session_data/small')) { mkdir(dirname(__DIR__) . '/session_data/small', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/small');

session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);

session_start();

error_reporting(0);

include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];




if (empty($_SESSION['sid'])) {

    header('location:logoutsmall.php');

    exit;

}



if (!isset($_SESSION['pending_officer_transfer'])) {

    header('Location: small_transfer_funds.php?error=' . urlencode('No pending transfer found. Please start again.'));

    exit;

}



$transfer    = $_SESSION['pending_officer_transfer'];

$amount      = (float)$transfer['amount'];

$to_id       = (int)$transfer['to_student_id'];

$from_id     = (int)$transfer['from_student_id'];



// Fetch recipient name

$stmt = $con->prepare("SELECT studentName FROM students WHERE id = ?");

$stmt->bind_param("i", $to_id);

$stmt->execute();

$recipientRow = $stmt->get_result()->fetch_assoc();

$stmt->close();

$recipientName = $recipientRow['studentName'] ?? 'Unknown Student';



// Fetch sender name

$stmt2 = $con->prepare("SELECT studentName FROM students WHERE id = ?");

$stmt2->bind_param("i", $from_id);

$stmt2->execute();

$senderRow = $stmt2->get_result()->fetch_assoc();

$stmt2->close();

$senderName = $senderRow['studentName'] ?? 'Unknown Student';



$error = isset($_GET['error']) ? htmlspecialchars($_GET['error']) : '';

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <?php @include("includes/head.php"); ?>



</head>

<body class="hold-transition sidebar-mini layout-fixed bg-slate-50">

<div class="wrapper">

    <?php @include("includes/header3.php"); ?>

    <?php @include("includes/sidebar3.php"); ?>



    <div class="content-wrapper">

<section class="content-header premium-header">

    <div class="container-fluid">

        <div class="header-glass-card">

            <div class="row align-items-center">

                <div class="col-md-8">

                    <div class="title-section">

                        <div class="icon-box bg-primary-gradient shadow-sm">

                            <i class="fas fa-wallet"></i>

                        </div>

                        <div>

                            <h1 class="premium-title text-primary">

                                Wallet Management

                                <span class="premium-subtitle text-secondary">Verify Transfer OTP</span>

                            </h1>
                            <div class="mt-1">
                              <?php if ($currentSessionName): ?>
                                <span class="badge badge-primary mr-1">
                                  <i class="fas fa-calendar-alt"></i>
                                  <?= htmlspecialchars($currentSessionName) ?>
                                </span>
                              <?php endif; ?>
                              <?php if ($currentTermName): ?>
                                <span class="badge badge-success">
                                  <i class="fas fa-bookmark"></i>
                                  <?= htmlspecialchars($currentTermName) ?>
                                </span>
                              <?php else: ?>
                                <span class="badge badge-warning">
                                  <i class="fas fa-exclamation-triangle"></i> No active term
                                </span>
                              <?php endif; ?>
                            </div>

                        </div>

                    </div>

                </div>

                <div class="col-md-4">

                    <nav aria-label="breadcrumb">

                        <ol class="breadcrumb premium-breadcrumb">

                            <li class="breadcrumb-item"><a href="dashboard2.php" class="text-primary">Home</a></li>

                            <li class="breadcrumb-item"><a href="small_transfer_funds.php">Transfer Funds</a></li>

                            <li class="breadcrumb-item active" aria-current="page">Verify OTP</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section> 

    <section class="content">

        <div class="container-fluid">

                        <div class="row">

              <div class="col-md-12">

                <!-- general form elements -->

                <div class="card card-primary">

                  <div class="card-header">

                    <h5 class="card-title font-weight-bold">

                      <i class="fas fa-wallet"></i> Wallet Details & Transaction History

                    </h5>

                  </div>

                  <div class="card-body">

        <section class="content">

            <div class="container-fluid">

                <div class="row justify-content-center">

                    <div class="col-md-6 col-lg-5">



                        <?php if ($error): ?>

                        <div class="alert alert-danger alert-dismissible rounded-xl mb-4">

                            <button type="button" class="close" data-dismiss="alert">&times;</button>

                            <i class="fas fa-exclamation-triangle mr-2"></i><?php echo $error; ?>

                        </div>

                        <?php endif; ?>



                        <div class="bg-white rounded-3xl shadow-xl border border-slate-100 overflow-hidden">

                            <!-- Header -->

                            <div class="bg-slate-900 p-8 text-center text-white">

                                <div class="w-16 h-16 bg-blue-500/20 rounded-full flex items-center justify-center mx-auto mb-4">

                                    <i class="fas fa-shield-alt text-blue-400 text-2xl"></i>

                                </div>

                                <h2 class="text-xl font-black tracking-tight">Security Verification</h2>

                                <p class="text-slate-400 text-xs uppercase font-bold tracking-widest mt-1">OTP Confirmation Required</p>

                            </div>



                            <!-- Transfer Summary -->

                            <div class="bg-slate-50 border-b border-slate-100 px-8 py-5">

                                <p class="text-xs font-bold text-slate-400 uppercase tracking-widest mb-3">Transfer Summary</p>

                                <div class="flex justify-between items-center mb-2">

                                    <span class="text-sm text-slate-500">From</span>

                                    <span class="text-sm font-bold text-slate-800"><?php echo htmlspecialchars($senderName); ?></span>

                                </div>

                                <div class="flex justify-between items-center mb-2">

                                    <span class="text-sm text-slate-500">To</span>

                                    <span class="text-sm font-bold text-slate-800"><?php echo htmlspecialchars($recipientName); ?></span>

                                </div>

                                <div class="flex justify-between items-center">

                                    <span class="text-sm text-slate-500">Amount</span>

                                    <span class="text-lg font-black text-emerald-600">₦<?php echo number_format($amount, 2); ?></span>

                                </div>

                            </div>



                            <!-- OTP Form -->

                            <div class="p-8">

                                <p class="text-sm text-slate-500 text-center mb-6">

                                    An OTP has been sent to <strong><?php echo htmlspecialchars($senderName); ?></strong>'s registered email address. Enter it below to authorize this transfer.

                                </p>



                                <form action="small_complete_transfer.php" method="POST" id="otpForm">

                                    <div class="mb-6">

                                        <label class="text-[10px] font-bold text-slate-400 uppercase tracking-widest block mb-2">

                                            6-Digit OTP Code

                                        </label>

                                        <input type="text"

                                               name="otp_code"

                                               required

                                               maxlength="6"

                                               pattern="\d{6}"

                                               inputmode="numeric"

                                               autocomplete="one-time-code"

                                               class="w-full border-2 border-slate-100 p-4 rounded-2xl text-center text-3xl tracking-[0.6em] font-black focus:border-blue-500 outline-none transition-colors"

                                               placeholder="000000">

                                        <p class="text-xs text-slate-400 text-center mt-2">Code expires in 10 minutes</p>

                                    </div>



                                    <button type="submit"

                                            id="btnConfirm"

                                            class="w-full bg-slate-900 text-white py-4 rounded-2xl font-black text-sm uppercase tracking-widest hover:bg-blue-600 transition-all shadow-lg active:scale-[0.97] flex items-center justify-center gap-2">

                                        <i class="fas fa-lock text-blue-400"></i> Confirm Transfer

                                    </button>

                                </form>



                                <div class="text-center mt-6">

                                    <a href="small_transfer_funds.php" class="text-xs font-bold text-slate-400 hover:text-rose-500 uppercase tracking-widest transition-colors">

                                        <i class="fas fa-times mr-1"></i> Cancel Transfer

                                    </a>

                                </div>

                            </div>

                        </div>



                    </div>

                </div>

            </div>

        </section>

                </div><!-- /.wpf-wrapper -->

            </div>

        </section>

    </div><!-- /.content-wrapper -->



    <?php @include("includes/footer.php"); ?>

    <?php @include("includes/foot.php"); ?>

</div><!-- /.wrapper -->



<script>

    document.getElementById('otpForm').addEventListener('submit', function() {

        const btn = document.getElementById('btnConfirm');

        btn.disabled = true;

        btn.innerHTML = '<i class="fas fa-circle-notch fa-spin mr-2"></i> Authorizing...';

        btn.className = btn.className.replace('hover:bg-blue-600', '') + ' opacity-70 cursor-not-allowed';

    });

</script>

</body>

</html>

