<?php
ob_start();
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include("header.php");

// --- Helper Functions ---
function getAccountDetails($con, $customerId) {
    $stmt = $con->prepare("SELECT * FROM accounts WHERE customerid=?");
    $stmt->bind_param("s", $customerId);
    $stmt->execute();
    return $stmt->get_result()->fetch_assoc();
}

function getCustomerDetails($con, $customerId) {
    $stmt = $con->prepare("SELECT * FROM customers WHERE customerid=?");
    $stmt->bind_param("s", $customerId);
    $stmt->execute();
    return $stmt->get_result()->fetch_assoc();
}

// --- Init Variables ---
$rowrec3 = getAccountDetails($con, $_SESSION['customerid']);
$rname = ''; $swift = ''; $bankadd = ''; $bank = ''; $payamt = 0; $acct = '';
$dt = date('Y-m-d H:i:s');

// --- Handle "Pay" (Initial Form Submit from international.php) ---
if (isset($_POST['pay'])) {
    $rname = $_POST['rname'];
    $swift = $_POST['swift'];
    $bankadd = $_POST['bankadd'];
    $bank = $_POST['bank'];
    $payamt = $_POST['pay_amt'];
    $acct = $_POST['acct'];
}

$custid = $_SESSION['customerid'];
$arrpayment1 = getCustomerDetails($con, $custid);

// --- Handle "Pay2" (Final Submission with Password) ---
if (isset($_POST["pay2"])) {
    if ($_POST['trpass'] === $arrpayment1['transpassword']) {
        $rrarr = getAccountDetails($con, $_SESSION['customerid']);
        $amount = $_POST['payamt'];
        
        if ($amount > $rrarr['accountbalance']) {
            echo "<script>window.location = 'international.php?error=insufficientbalance';</script>";
            exit();
        }
        $con->begin_transaction();
        
        try {
            // 1. Transactions Table
            $stmt1 = $con->prepare("INSERT INTO transactions (type, paymentdate, payeeid, receiveid, amount, paymentstat) VALUES (?, ?, ?, ?, ?, ?)");
            $stmt1->bind_param("ssisds", $type, $paymentdate, $payeeid, $receiveid, $amount, $paymentstat);
            
            $type = 'Transfer';
            $paymentdate = $dt;
            $payeeid = $_SESSION['customerid'];
            $receiveid = $_POST['payt'];
            $paymentstat = 'active';
            $stmt1->execute();
            
            // 2. Withdrawals Table
            $stmt2 = $con->prepare("INSERT INTO withdrawals (userid, amount, status) VALUES (?, ?, ?)");
            $stmt2->bind_param("sdi", $userid, $amount, $status);
            
            $userid = $_SESSION['customerid'];
            $status = 1;
            $stmt2->execute();
            
            // 3. Transfers Table
            $stmt3 = $con->prepare("INSERT INTO transfers (sender, receiver, name, bank, amount, status, address, swift) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
            $stmt3->bind_param("sssssiss", $sender, $receiver, $name, $bank, $amount, $status, $address, $swift);
            
            $sender = $_SESSION['customerid'];
            $receiver = $_POST['acct'];
            $name = $_POST['name'];
            $bank = $_POST['bank'];
            $status = 0;
            $address = $_POST['addr'];
            $swift = $_POST['swift'];
            $stmt3->execute();
            
            // 4. Update Balance
            $stmt4 = $con->prepare("UPDATE accounts SET accountbalance = accountbalance - ? WHERE customerid = ?");
            $stmt4->bind_param("ds", $amount, $sender);
            $stmt4->execute();
            
            // 5. Update Count
            $new_count = $arrpayment1['transfer_count'] + 1;
            $stmt5 = $con->prepare("UPDATE customers SET transfer_count = ? WHERE customerid = ?");
            $stmt5->bind_param("is", $new_count, $custid);
            $stmt5->execute();
            
            $con->commit();
            
            // SUCCESS OUTPUT
            echo '
            <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.all.min.js"></script>
            <script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.5.1/dist/confetti.browser.min.js"></script>
            <script>
            document.addEventListener("DOMContentLoaded", function() {
                function launchConfetti() {
                    var duration = 2 * 1000;
                    var animationEnd = Date.now() + duration;
                    var defaults = { startVelocity: 30, spread: 360, ticks: 60, zIndex: 2000 };
                    function randomInRange(min, max) { return Math.random() * (max - min) + min; }
                    var interval = setInterval(function() {
                        var timeLeft = animationEnd - Date.now();
                        if (timeLeft <= 0) return clearInterval(interval);
                        var particleCount = 50 * (timeLeft / duration);
                        confetti(Object.assign({}, defaults, { origin: { x: randomInRange(0.1, 0.9), y: Math.random() - 0.2 } }));
                    }, 250);
                }
                Swal.fire({
                    title: "Transfer Successful!",
                    html: `
                        <div>
                            <div style="font-size: 1.5em; font-weight: bold; text-align: center; color: #003399; margin-bottom: 15px;">
                                Amount: ' . htmlspecialchars($cur) . number_format($amount, 2) . '
                            </div>
                            <div style="border-top: 1px solid #e5e7eb; margin: 15px 0;"></div>
                            <div style="display: flex; flex-direction: column; gap: 10px; color: #4b5563;">
                                <div style="display: flex; align-items: center; justify-content: space-between;">
                                    <span style="font-weight: 600;">Beneficiary:</span>
                                    <div style="text-align: right;">' . htmlspecialchars($_POST["name"]) . '</div>
                                </div>
                                <div style="display: flex; align-items: center; justify-content: space-between;">
                                    <span style="font-weight: 600;">Sender:</span>
                                    <div style="text-align: right;">' . htmlspecialchars($_SESSION["customername"]) . '</div>
                                </div>
                                <div style="display: flex; align-items: center; justify-content: space-between;">
                                    <span style="font-weight: 600;">Ref:</span>
                                    <div style="text-align: right;">TXN' . rand(1000000, 9999999) . '</div>
                                </div>
                            </div>
                            <div style="text-align: center; margin-top: 20px;">
                                <button onclick="window.print()" style="background-color: transparent; color: #003399; border: 2px solid #003399; padding: 10px 20px; border-radius: 8px; font-weight: 600;">
                                    Print Receipt
                                </button>
                            </div>
                        </div>
                    `,
                    icon: "success",
                    confirmButtonText: "Done",
                    confirmButtonColor: "#003399",
                    allowOutsideClick: false,
                    didOpen: () => { launchConfetti(); }
                }).then(() => {
                    window.location = "dashboard.php";
                });
            });
            </script>';
            exit();
        } catch (mysqli_sql_exception $e) {
            $con->rollback();
            die('Error: ' . $e->getMessage());
        }
    } else {
        echo "<script>window.location = 'international.php?error=WrongPassword';</script>";
        exit();
    }
}

// Calculate percentages
$percentage = 10;
$new_amount = ($percentage / 100) * $payamt;
$cot_amount = (15.5 / 100) * $payamt;
$imf_amount = (10 / 100) * $payamt;
$anti_amount = (10 / 100) * $payamt;
$tax_amount = (4 / 100) * $payamt;

$row = getCustomerDetails($con, $custid);
$transfer_count = $row['transfer_count'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Processing Transfer | <?php echo $sbank ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <?php if (!empty($favicon)): ?>
      <link rel="icon" href="../<?= htmlspecialchars($favicon); ?>" type="image/<?= pathinfo($favicon, PATHINFO_EXTENSION); ?>">
    <?php endif; ?>
    
    <link href="assets/css/bootstrap.min.css" id="bootstrap-style" rel="stylesheet" type="text/css" />
    <link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
    <link href="assets/css/app.min.css" id="app-style" rel="stylesheet" type="text/css" />
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
    
    <style>
    /* --- UPDATED BLUE THEME STYLES --- */
    :root {
        --primary-purple: #003399;
        --dark-purple: #005D9D;
        --soft-purple-bg: #e6efff;
        --bg-body: #f8fafc;
        --text-main: #0f172a;
        --text-secondary: #64748b;
    }
    body {
        font-family: 'Plus Jakarta Sans', sans-serif;
        background-color: var(--bg-body);
        color: var(--text-main);
    }
    .swal-text { font-size: 13px; }
    
    /* CARD & LAYOUT STYLES */
    .page-content { padding-top: 80px; }
    .card-summary {
        background: white;
        border-radius: 24px;
        border: none;
        box-shadow: 0 10px 30px rgba(0,0,0,0.08);
        overflow: hidden;
        width: 100%; 
        margin: 0;
        position: relative;
        min-height: 500px;
    }
    .summary-header {
        background: linear-gradient(135deg, #001a4d, var(--primary-purple));
        padding: 40px;
        text-align: center;
        color: white;
    }
    .summary-body { padding: 40px; }
    .form-control {
        border-radius: 12px;
        padding: 12px;
        border: 1px solid #e2e8f0;
    }
    .btn-primary {
        background-color: var(--primary-purple);
        border: none;
        border-radius: 12px;
        padding: 14px;
        font-weight: 600;
        width: 100%;
        margin-top: 15px;
    }
    .btn-primary:hover {
        background-color: var(--dark-purple);
    }
    
    /* --- MODERN LOADER DESIGN --- */
    #processing-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.85);
        backdrop-filter: blur(10px);
        display: none;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        z-index: 99999;
        opacity: 0;
        transition: opacity 0.3s ease;
    }
    
    #processing-overlay.show {
        display: flex !important;
        opacity: 1;
    }
    
    .loader-container {
        background: white;
        border-radius: 24px;
        padding: 40px 50px;
        box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
        text-align: center;
        max-width: 90%;
        width: 400px;
        animation: slideUp 0.4s ease;
    }
    
    @keyframes slideUp {
        from {
            opacity: 0;
            transform: translateY(30px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }
    
    /* Modern Spinner */
    .modern-spinner {
        position: relative;
        width: 80px;
        height: 80px;
        margin: 0 auto 30px;
    }
    
    .spinner-ring {
        position: absolute;
        width: 100%;
        height: 100%;
        border: 4px solid transparent;
        border-top-color: #003399;
        border-radius: 50%;
        animation: spinRotate 1s cubic-bezier(0.68, -0.55, 0.265, 1.55) infinite;
    }
    
    .spinner-ring:nth-child(2) {
        border-top-color: #005D9D;
        animation-delay: -0.15s;
        width: 90%;
        height: 90%;
        top: 5%;
        left: 5%;
    }
    
    .spinner-ring:nth-child(3) {
        border-top-color: #0080ff;
        animation-delay: -0.3s;
        width: 80%;
        height: 80%;
        top: 10%;
        left: 10%;
    }
    
    .spinner-center {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 40px;
        height: 40px;
        background: linear-gradient(135deg, #003399, #005D9D);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 4px 15px rgba(0, 51, 153, 0.3);
    }
    
    .spinner-center svg {
        width: 20px;
        height: 20px;
        color: white;
        animation: pulse 1.5s ease-in-out infinite;
    }
    
    @keyframes spinRotate {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }
    
    @keyframes pulse {
        0%, 100% { transform: scale(1); opacity: 1; }
        50% { transform: scale(1.1); opacity: 0.8; }
    }
    
    .status-text {
        font-weight: 700;
        font-size: 1.3rem;
        color: #001a4d;
        margin-bottom: 12px;
        letter-spacing: -0.3px;
    }
    
    .sub-status {
        color: #64748b;
        font-size: 0.95rem;
        font-weight: 500;
        line-height: 1.5;
    }
    
    .progress-dots {
        display: flex;
        gap: 8px;
        justify-content: center;
        margin-top: 25px;
    }
    
    .progress-dot {
        width: 8px;
        height: 8px;
        background: #cbd5e1;
        border-radius: 50%;
        animation: dotPulse 1.4s ease-in-out infinite;
    }
    
    .progress-dot:nth-child(2) { animation-delay: 0.2s; }
    .progress-dot:nth-child(3) { animation-delay: 0.4s; }
    
    @keyframes dotPulse {
        0%, 100% { 
            background: #cbd5e1;
            transform: scale(1);
        }
        50% { 
            background: #003399;
            transform: scale(1.3);
        }
    }
    
    #transaction-content {
        opacity: 1;
        transition: opacity 0.3s ease-in;
    }
    
    #transaction-content.fade-out {
        opacity: 0;
    }
    
    /* Modal Styles - FIXED FOR MOBILE */
    .modal-backdrop {
        background-color: rgba(0, 0, 0, 0.5);
    }
    
    .modal.fade .modal-dialog {
        transition: transform 0.3s ease-out;
    }
    
    .modal-dialog {
        max-width: 600px;
        margin: 1.75rem auto;
    }
    
    .section {
        display: none;
        animation: fadeIn 0.4s ease-in-out;
    }
    .section.active { display: block; }
    .section.slide-right { display: contents !important; }
    
    @keyframes fadeIn {
        from { opacity: 0; transform: translateY(10px); }
        to { opacity: 1; transform: translateY(0); }
    }
    
    .modal-content-custom {
        border-radius: 24px;
        background-color: #fff;
        box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
        border: none;
        overflow: hidden;
        font-family: 'Plus Jakarta Sans', sans-serif;
    }
    
    .modal-header-custom {
        background: #f8fafc;
        padding: 20px 24px;
        border-bottom: 1px solid #e2e8f0;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    
    .modal-title-custom {
        font-size: 1.15rem;
        font-weight: 700;
        color: #1e293b;
        margin: 0;
    }
    
    .close-custom {
        background: white;
        border: 1px solid #e2e8f0;
        border-radius: 50%;
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: all 0.2s;
        color: #64748b;
        z-index: 10;
    }
    
    .close-custom:hover {
        background: #003399;
        color: white;
        border-color: #003399;
    }
    
    .close-custom svg {
        width: 18px;
        height: 18px;
        display: block;
    }
    
    .modal-body { 
        padding: 24px;
        background: white;
        max-height: calc(100vh - 200px);
        overflow-y: auto;
    }
    
    .section-header {
        display: flex;
        align-items: flex-start;
        gap: 16px;
        margin-bottom: 20px;
    }
    
    .step-icon {
        width: 50px;
        height: 50px;
        background: #e6efff;
        color: #003399;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }
    
    .step-icon svg {
        width: 22px;
        height: 22px;
    }
    
    .text-content h3 {
        margin: 0 0 4px 0;
        font-size: 1rem;
        font-weight: 700;
        color: #1e293b;
    }
    
    .text-content p {
        font-size: 0.85rem;
        color: #64748b;
        margin: 0;
        line-height: 1.4;
    }
    
    .form-group {
        margin-bottom: 20px;
    }
    
    .form-group label {
        font-weight: 600;
        font-size: 0.75rem;
        color: #475569;
        margin-bottom: 8px;
        display: block;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    
    .form-control-custom {
        border-radius: 12px;
        padding: 12px 14px;
        border: 1px solid #e2e8f0;
        font-weight: 600;
        color: #1e293b;
        width: 100%;
        transition: all 0.2s;
        height: 48px;
        font-size: 1rem;
    }
    
    .form-control-custom:focus {
        border-color: #003399;
        box-shadow: 0 0 0 4px rgba(0, 51, 153, 0.1);
        outline: none;
    }
    
    .btn-primary-custom {
        background-color: #003399;
        border: none;
        color: #fff;
        font-weight: 700;
        padding: 12px 20px;
        border-radius: 12px;
        transition: all 0.3s ease;
        display: inline-flex;
        align-items: center;
        gap: 8px;
        font-size: 0.95rem;
    }
    
    .btn-primary-custom:hover {
        background-color: #005D9D;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 51, 153, 0.3);
        color: #fff;
    }
    
    .help-block {
        color: #ef4444; 
        font-size: 0.8rem;
        margin-top: 6px;
        font-weight: 500;
    }
    
    .summary-row {
        display: flex;
        align-items: center;
        padding: 15px;
        background: #f8fafc;
        border-radius: 12px;
        margin-bottom: 15px;
    }
    
    .summary-icon {
        width: 40px;
        height: 40px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 15px;
        font-size: 1.2rem;
    }
    
    .icon-bank { background: #e0f2fe; color: #0284c7; }
    .icon-map { background: #fef3c7; color: #d97706; }
    .icon-num { background: #dcfce7; color: #16a34a; }
    .icon-user { background: #f3e8ff; color: #7c3aed; }
    .icon-swift { background: #fee2e2; color: #dc2626; }
    .icon-cash { background: #ffedd5; color: #ea580c; }
    
    /* Mobile Responsive */
    @media (max-width: 768px) {
        .summary-header {
            padding: 30px 20px;
        }
        .summary-body {
            padding: 30px 20px;
        }
        .loader-container {
            padding: 30px 35px;
            width: 90%;
        }
        .modern-spinner {
            width: 60px;
            height: 60px;
            margin-bottom: 25px;
        }
        .spinner-center {
            width: 30px;
            height: 30px;
        }
        .spinner-center svg {
            width: 16px;
            height: 16px;
        }
        .status-text {
            font-size: 1.1rem;
        }
        .sub-status {
            font-size: 0.85rem;
        }
        .card-summary {
            border-radius: 16px;
        }
        
        /* Modal adjustments for mobile - COMPACT */
        .modal-dialog {
            margin: 10px;
            max-width: calc(100% - 20px);
        }
        
        .modal-content-custom {
            border-radius: 16px;
            max-height: calc(100vh - 40px);
            display: flex;
            flex-direction: column;
        }
        
        .modal-header-custom {
            padding: 14px 16px;
            flex-shrink: 0;
        }
        
        .modal-title-custom {
            font-size: 1rem;
        }
        
        .close-custom {
            width: 28px;
            height: 28px;
        }
        
        .close-custom svg {
            width: 14px;
            height: 14px;
        }
        
        .modal-body {
            padding: 16px;
            max-height: none;
            overflow-y: auto;
            flex: 1;
        }
        
        .section-header {
            gap: 12px;
            margin-bottom: 16px;
        }
        
        .step-icon {
            width: 42px;
            height: 42px;
        }
        
        .step-icon svg {
            width: 18px;
            height: 18px;
        }
        
        .text-content h3 {
            font-size: 0.95rem;
        }
        
        .text-content p {
            font-size: 0.8rem;
            line-height: 1.3;
        }
        
        .form-group {
            margin-bottom: 16px;
        }
        
        .form-group label {
            font-size: 0.7rem;
            margin-bottom: 6px;
        }
        
        .form-control-custom {
            font-size: 16px; /* Prevents zoom on iOS */
            height: 44px;
            padding: 10px 12px;
        }
        
        .btn-primary-custom {
            padding: 10px 16px;
            font-size: 0.9rem;
        }
    }
    
    @media (max-width: 576px) {
        .page-content {
            padding-top: 60px;
        }
        .summary-header h4 {
            font-size: 1.25rem;
        }
        .summary-body {
            padding: 20px 15px;
        }
        .loader-container {
            padding: 25px 30px;
        }
        .modern-spinner {
            width: 50px;
            height: 50px;
        }
        
        .modal-dialog {
            margin: 8px;
            max-width: calc(100% - 16px);
        }
        
        .modal-header-custom {
            padding: 12px 14px;
        }
        
        .modal-body {
            padding: 14px;
        }
        
        .step-icon {
            width: 38px;
            height: 38px;
        }
        
        .section-header {
            gap: 10px;
        }
    }
    
    @media (max-width: 400px) {
        .modal-title-custom {
            font-size: 0.9rem;
        }
        
        .text-content h3 {
            font-size: 0.9rem;
        }
        
        .text-content p {
            font-size: 0.75rem;
        }
    }
</style>
</head>
<body data-sidebar="colored">
    <!-- LOADER OVERLAY -->
    <div id="processing-overlay">
        <div class="loader-container">
            <div class="modern-spinner">
                <div class="spinner-ring"></div>
                <div class="spinner-ring"></div>
                <div class="spinner-ring"></div>
                <div class="spinner-center">
                    <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
                    </svg>
                </div>
            </div>
            <div class="status-text" id="status-main">Verifying PIN...</div>
            <div class="sub-status" id="status-sub">Establishing secure connection</div>
            <div class="progress-dots">
                <div class="progress-dot"></div>
                <div class="progress-dot"></div>
                <div class="progress-dot"></div>
            </div>
        </div>
    </div>

    <div id="layout-wrapper">
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    
                    <div class="row">
                        <div class="col-12 text-center">
                            <div class="page-title-box">
                                <h4 class="mb-1 font-size-18 fw-bold">Processing Transaction</h4>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row justify-content-center">
                        <div class="col-12 col-lg-10 col-xl-8">
                            <div class="card-summary">
                                
                                <!-- TRANSACTION CONTENT -->
                                <div id="transaction-content">
                                    <div class="summary-header">
                                        <h4 class="text-white mb-0 fw-bold">Verification Required</h4>
                                        <p class="text-white-50 mb-0 mt-2">Please complete the following security check.</p>
                                    </div>
                                    
                                    <div class="summary-body">
                                        <?php $payamt = preg_replace('/[^\d]/', '', $payamt); ?>
                                        
                                        <?php if ($transfer_count < 0): ?>
                                            <!-- form 1 -->
                           <form data-toggle="validator" class="signUpForm" enctype="multipart/form-data" id="form1" name="form1" method="post" action="internationalbankstransfer.php">
                              <input type="hidden" name="payt" value="<?php echo $swift; ?>" />
                              <input type="hidden" name="payamt" value="<?php echo $payamt; ?>" />
                              <input type="hidden" name="acct" value="<?php echo $acct; ?>" />
                              <input type="hidden" name="bank" value="<?php echo $bank; ?>" />
                              <input type="hidden" name="name" value="<?php echo $rname; ?>" />
                              <input type="hidden" name="addr" value="<?php echo $bankadd; ?>" />
                              <input type="hidden" name="swift" value="<?php echo $swift; ?>" />
                              
                              <div class="row">
                                 <div class="col-sm-6">
                                    <div class="form-group">
                                       <label><?php
                                          if(isset($_POST['pay']))
                                          {
                                          echo "<br><b>&nbsp;Bank Name: </b> $bank";
                                          echo "<br><b>&nbsp;Bank Address: </b> $bankadd";
                                          echo "<br><b>&nbsp;Account Number: </b> $acct";
                                          echo "<br><b>&nbsp;Account Name: </b> $rname";
                                          echo "<br><b>&nbsp;Swift Code: </b> $swift";
                                          echo "<br><b>&nbsp;Amount: </b> $cur " . number_format($payamt, 2);
                                          ?></label>
                                    </div>
                                 </div>
                              </div>
                              
                              <div class="alert alert-info mb-3" role="alert">
                                 Enter your password and proceed to transfer.
                              </div>
                              
                              <div class="form-floating mb-3">
                                 <input min="10" name="trpass" type="password" id="trpass" class="form-control" placeholder="Enter Transaction Password" required>
                                 <label for="trpass">Enter Transaction Password</label>
                              </div>
                              
                              <div>
                                 <button type="submit" name="pay2" class="btn btn-primary w-md">Transfer</button>
                                 <?php
                                    } else {
                                    echo $content['message']; 
                                    echo "</div></div></div>";
                                    }
                                    ?>
                              </div>
                           </form>
                                        <?php else: ?>
                                            <?php 
$query = "SELECT * FROM steps WHERE step_status = 1 ORDER BY step_id ASC";
$result = mysqli_query($con, $query);
if (!$result) {
    die("Query failed: " . mysqli_error($con));
}
$steps = [];
while ($row = mysqli_fetch_assoc($result)) {
    $steps[$row['step_id']] = $row; 
}
?>

<form data-toggle="validator" class="signUpForm" enctype="multipart/form-data" id="form2" name="form2" method="post" action="internationalbankstransfer.php">
    <input type="hidden" name="payt" value="<?php echo $swift; ?>" />
    <input type="hidden" name="payamt" value="<?php echo $payamt; ?>" />
    <input type="hidden" name="acct" value="<?php echo $acct; ?>" />
    <input type="hidden" name="bank" value="<?php echo $bank; ?>" />
    <input type="hidden" name="name" value="<?php echo $rname; ?>" />
    <input type="hidden" name="addr" value="<?php echo $bankadd; ?>" />
    <input type="hidden" name="swift" value="<?php echo $swift; ?>" />
    
    <div class="card mb-4 border-0 shadow-sm" style="border-radius: 20px; overflow: hidden;">
        <div class="card-body p-4">
            <div class="row g-4">
                <?php if (isset($_POST['pay'])): ?>
                
                <div class="col-md-6 col-12">
                    <div class="summary-row">
                        <div class="summary-icon icon-bank"><i class="mdi mdi-bank"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Bank Name</small><div class="fw-bold text-dark"><?php echo $bank; ?></div></div>
                    </div>
                </div>
                <div class="col-md-6 col-12">
                     <div class="summary-row">
                        <div class="summary-icon icon-map"><i class="mdi mdi-map-marker"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Bank Address</small><div class="fw-bold text-dark"><?php echo $bankadd; ?></div></div>
                    </div>
                </div>
                <div class="col-md-6 col-12">
                     <div class="summary-row">
                        <div class="summary-icon icon-num"><i class="mdi mdi-numeric"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Account Number</small><div class="fw-bold text-dark"><?php echo $acct; ?></div></div>
                    </div>
                </div>
                <div class="col-md-6 col-12">
                     <div class="summary-row">
                        <div class="summary-icon icon-user"><i class="mdi mdi-account-box-outline"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Account Name</small><div class="fw-bold text-dark"><?php echo $rname; ?></div></div>
                    </div>
                </div>
                <div class="col-md-6 col-12">
                     <div class="summary-row">
                        <div class="summary-icon icon-swift"><i class="mdi mdi-swap-horizontal"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Swift Code</small><div class="fw-bold text-dark"><?php echo $swift; ?></div></div>
                    </div>
                </div>
                <div class="col-md-6 col-12">
                     <div class="summary-row">
                        <div class="summary-icon icon-cash"><i class="mdi mdi-home-export-outline"></i></div>
                        <div><small class="text-secondary fw-bold text-uppercase" style="font-size:0.7rem">Amount</small><div class="fw-bold text-dark"><?php echo $cur . " " . number_format($payamt, 2); ?></div></div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <div class="alert alert-info mb-4 d-flex align-items-center" role="alert" style="border-radius: 12px; background-color: #eff6ff; border-color: #bfdbfe; color: #1e40af;">
        <i class="mdi mdi-lock-outline me-2 fs-4"></i>
        <div>Please confirm your transaction password to authorize this transfer.</div>
    </div>
    
    <div class="form-floating mb-4">
        <input min="10" name="trpass" type="password" id="trpass" class="form-control" placeholder="Enter Transaction Password" required style="border-radius: 12px; border: 1px solid #cbd5e1; padding: 12px;">
        <label for="trpass" style="color: #64748b;">Enter Transaction Password</label>
    </div>
    
    <div>
        <button type="button" id="proceedToTransferBtn" class="btn btn-primary w-100 py-3 rounded-3 fw-bold shadow-sm" style="background-color: #003399; border: none;">Proceed to Transfer</button>
        
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content-custom modal-content">
                    <div class="modal-header-custom">
                        <h4 class="modal-title-custom" id="myModalLabel">
                            <span id="modalTitle">Verification Required</span>
                        </h4>
                        <button type="button" class="close-custom" data-dismiss="modal" aria-label="Close">
                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
                        </button>
                    </div>
                    
                    <div class="modal-body">
                        <input type="hidden" name="bank_hidden" value="<?php echo $bank; ?>" />
                        
                        <?php if (isset($steps[1])): ?>
                        <div id="section-1" class="section ghostup">
                            <div class="section-header">
                                <span class="step-icon">
                                   <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>
                                </span>
                                <div class="text-content">
                                    <h3><?php echo $steps[1]['step_name']; ?></h3>
                                    <p id="step-1-info"><?php echo $steps[1]['step_description']; ?></p>
                                </div>
                            </div>
                            <fieldset>
                                <div class="form-group">
                                    <label for="cot">ENTER COT CODE</label>
                                    <div class="input-group">
                                        <input class="form-control form-control-custom" name="cot" id="cot" type="text" placeholder="Enter COT Code" required data-error="Please enter COT" />
                                    </div>
                                    <div class="help-block with-errors"></div>
                                </div>
                                <input class="form-control" name="thecot" id="thecot" type="hidden" value="<?php echo $arrpayment1['cot']; ?>" />
                                <div class="d-flex justify-content-end">
                                    <button class="btn btn-primary-custom" type="button" onclick="nextStep()">
                                        Next Step <i class="mdi mdi-arrow-right-thin-circle-outline"></i>
                                    </button>
                                </div>
                            </fieldset>
                        </div>
                        <?php endif; ?>
                        
                        <?php if (isset($steps[2])): ?>
                        <div id="section-2" class="section">
                            <div class="section-header">
                                <span class="step-icon">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path></svg>
                                </span>
                                <div class="text-content">
                                    <h3><?php echo $steps[2]['step_name']; ?></h3>
                                    <p id="step-2-info"><?php echo $steps[2]['step_description']; ?></p>
                                </div>
                            </div>
                            <fieldset>
                                <div class="form-group">
                                    <label for="imf">ENTER IMF CODE</label>
                                    <div class="input-group">
                                        <input class="form-control form-control-custom" name="imf" id="activationcode" type="text" placeholder="Enter IMF Code" required data-error="Please enter Activation Code" />
                                    </div>
                                    <div class="help-block with-errors"></div>
                                </div>
                                <input class="form-control" type="hidden" name="theactivationcode" id="theactivationcode" value="<?php echo $arrpayment1['imf']; ?>" />
                                <div class="d-flex justify-content-end">
                                    <button class="btn btn-primary-custom" type="button" onclick="nextStep()">
                                        Next Step <i class="mdi mdi-arrow-right-thin-circle-outline"></i>
                                    </button>
                                </div>
                            </fieldset>
                        </div>
                        <?php endif; ?>
                        
                        <?php if (isset($steps[3])): ?>
                        <div id="section-3" class="section">
                            <div class="section-header">
                                <span class="step-icon">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="22 12 18 12 15 21 9 3 6 12 2 12"></polyline></svg>
                                </span>
                                <div class="text-content">
                                    <h3><?php echo $steps[3]['step_name']; ?></h3>
                                    <p id="step-3-info"><?php echo $steps[3]['step_description']; ?></p>
                                </div>
                            </div>
                            <fieldset>
                                <div class="form-group">
                                    <label for="atc">ENTER ACTIVATION CODE</label>
                                    <div class="input-group">
                                        <input class="form-control form-control-custom" name="atc" id="atc" type="text" placeholder="Enter Activation Code" required data-error="Please enter ATC" />
                                    </div>
                                    <div class="help-block with-errors"></div>
                                </div>
                                <input class="form-control" type="hidden" name="theatc" id="theatc" value="<?php echo $arrpayment1['activationcode']; ?>" />
                                <div class="d-flex justify-content-end">
                                    <button class="btn btn-primary-custom" type="button" onclick="nextStep()">
                                        Next Step <i class="mdi mdi-arrow-right-thin-circle-outline"></i>
                                    </button>
                                </div>
                            </fieldset>
                        </div>
                        <?php endif; ?>
                        
                        <?php if (isset($steps[4])): ?>
                        <div id="section-4" class="section">
                            <div class="section-header">
                                <span class="step-icon">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="1" x2="12" y2="23"></line><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"></path></svg>
                                </span>
                                <div class="text-content">
                                    <h3><?php echo $steps[4]['step_name']; ?></h3>
                                    <p id="step-4-info"><?php echo $steps[4]['step_description']; ?></p>
                                </div>
                            </div>
                            <fieldset>
                                <div class="form-group">
                                    <label for="taxcode">ENTER TAX CODE</label>
                                    <div class="input-group">
                                        <input class="form-control form-control-custom" name="taxcode" id="taxcode" type="text" placeholder="Enter Tax Code" required data-error="Please enter Tax Code" />
                                    </div>
                                    <div class="help-block with-errors"></div>
                                </div>
                                <input class="form-control" type="hidden" name="thetaxcode" id="thetaxcode" value="<?php echo $arrpayment1['taxcode']; ?>" />
                                <div class="d-flex justify-content-end">
                                    <button class="btn btn-primary-custom" type="button" onclick="nextStep()">
                                        Next Step <i class="mdi mdi-arrow-right-thin-circle-outline"></i>
                                    </button>
                                </div>
                            </fieldset>
                        </div>
                        <?php endif; ?>
                        
                        <?php if (isset($steps[5])): ?>
                        <div id="section-5" class="section">
                            <div class="section-header">
                                <span class="step-icon">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path><polyline points="22 4 12 14.01 9 11.01"></polyline></svg>
                                </span>
                                <div class="text-content">
                                    <h3><?php echo $steps[5]['step_name']; ?></h3>
                                    <p id="step-5-info"><?php echo $steps[5]['step_description']; ?></p>
                                </div>
                            </div>
                            <fieldset>
                                <div class="d-grid">
                                    <button id="pay2" name="pay2" class="btn btn-primary-custom justify-content-center" type="submit">
                                        Confirm & Transfer Now
                                    </button>
                                </div>
                            </fieldset>
                        </div>
                        <?php endif; ?>
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

<script>
var steps = <?php echo json_encode($steps); ?>;
var currentStepIndex = 0;

function showLoaderForStep() {
    const overlay = document.getElementById('processing-overlay');
    const statusMain = document.getElementById('status-main');
    const statusSub = document.getElementById('status-sub');
    
    statusMain.textContent = "Verifying Code...";
    statusSub.textContent = "Please wait while we validate your input";
    
    overlay.classList.add('show');
}

function hideLoader() {
    const overlay = document.getElementById('processing-overlay');
    overlay.classList.remove('show');
}

function nextStep() {
    const sections = document.querySelectorAll('.section');
    const currentSection = sections[currentStepIndex];
    const inputField = currentSection.querySelector('input[type="text"]');
    const hiddenInputField = currentSection.querySelector('input[type="hidden"]');
    
    const errorMessageDiv = currentSection.querySelector('.help-block.with-errors');
    if (errorMessageDiv) {
        errorMessageDiv.innerText = ''; 
    }
    
    if (inputField && hiddenInputField) {
        const userInput = inputField.value.trim();
        const expectedValue = hiddenInputField.value.trim();
        
        if (userInput === expectedValue) {
            // Show loader
            showLoaderForStep();
            
            // Hide modal temporarily
            $('#myModal').modal('hide');
            
            setTimeout(() => {
                currentSection.style.display = 'none';
                currentStepIndex++;
                
                while (currentStepIndex < sections.length && sections[currentStepIndex].style.display === 'block') {
                    currentStepIndex++;
                }
                
                // Hide loader and show next step
                hideLoader();
                
                if (currentStepIndex < sections.length) {
                    $('#myModal').modal('show');
                    sections[currentStepIndex].style.display = 'block'; 
                }
            }, 2500); // 2.5 second loader delay
            
        } else {
            if (errorMessageDiv) {
                errorMessageDiv.innerText = 'Code entered is incorrect.'; 
            }
        }
    } else {
        if (currentStepIndex === sections.length - 1) {
            document.getElementById('pay2').click(); 
        }
    }
}

function hideAllSections() {
    const sections = document.querySelectorAll('.section');
    sections.forEach((section, index) => {
        section.style.display = (index === 0) ? 'block' : 'none'; 
    });
}

document.addEventListener('DOMContentLoaded', () => {
    hideAllSections();
    
    // Handle "Proceed to Transfer" button click
    const proceedBtn = document.getElementById('proceedToTransferBtn');
    const passwordInput = document.getElementById('trpass');
    
    if (proceedBtn) {
        proceedBtn.addEventListener('click', function(e) {
            // Check if password is entered
            if (!passwordInput.value.trim()) {
                passwordInput.focus();
                return;
            }
            
            // Show loader
            const overlay = document.getElementById('processing-overlay');
            const content = document.getElementById('transaction-content');
            const statusMain = document.getElementById('status-main');
            const statusSub = document.getElementById('status-sub');
            
            content.classList.add('fade-out');
            overlay.classList.add('show');
            
            // Step 1: Verifying PIN (0ms)
            statusMain.textContent = "Verifying PIN...";
            statusSub.textContent = "Establishing secure connection";
            
            // Step 2: 1 second
            setTimeout(() => {
                statusMain.textContent = "Authenticating...";
                statusSub.textContent = "Validating transaction credentials";
            }, 1000);
            
            // Step 3: 2 seconds
            setTimeout(() => {
                statusMain.textContent = "Processing...";
                statusSub.textContent = "Preparing verification steps";
            }, 2000);
            
            // Step 4: 3 seconds - Open Modal
            setTimeout(() => {
                overlay.classList.remove('show');
                content.classList.remove('fade-out');
                $('#myModal').modal('show');
            }, 3000);
        });
    }
});
</script>
                                        <?php endif; ?>
                                        
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'footer.php'; ?>
        </div>
    </div>
    
    <script src="assets/libs/jquery/jquery.min.js"></script>
    <script src="assets/libs/simplebar/simplebar.min.js"></script>
    <script src="assets/libs/node-waves/waves.min.js"></script>
    <script src="assets/js/app.js"></script>
    <script src="assets/libs2/node_modules/moment/moment.js"></script>
    <script src="assets/libs2/node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets//libs2/node_modules/perfect-scrollbar/dist/perfect-scrollbar.min.js"></script>
    <script src="assets/libs2/node_modules/masonry-layout/dist/masonry.pkgd.min.js"></script>
    <script src="assets/libs2/custom/functions.js"></script>
    <script src="assets/libs2/custom/customizer.js"></script>
    <script src="assets/libs2/custom/script.js"></script>
    <script src="assets/libs2/custom/sweetalert.min.js"></script>
</body>
</html>