<?php
$pageName = "Transaction Successful";
include($_SERVER['DOCUMENT_ROOT'] . "/user/layout/header.php");

// Guard — must have a valid transfer session
if (!isset($_SESSION['is_transfer']) || $_SESSION['is_transfer'] !== 'transfer') {
    header("Location:./dashboard.php");
    exit;
}

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

// Require whichever function file is relevant — both set dom_transfer session key
require_once($_SERVER['DOCUMENT_ROOT'] . "/include/config.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/include/Function/sql.php");

$conn = dbConnect();

// Fetch the latest transaction for this user
$sql = "SELECT * FROM transactions WHERE user_id=:user_id ORDER BY trans_id DESC LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->execute(['user_id' => $user_id]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);

// Fetch updated user balance
$sql2 = "SELECT * FROM users WHERE id=:id";
$stmt2 = $conn->prepare($sql2);
$stmt2->execute(['id' => $user_id]);
$user = $stmt2->fetch(PDO::FETCH_ASSOC);

// Fetch settings
$sql3  = "SELECT * FROM settings WHERE id='1'";
$stmt3 = $conn->prepare($sql3);
$stmt3->execute();
$page  = $stmt3->fetch(PDO::FETCH_ASSOC);

$transactiontype = $result['trans_type'];
$amount          = $result['amount'];
$trans_status    = $result['trans_status'];
$refrence_id     = $result['refrence_id'];
$created_at      = $result['created_at'];

// Determine fee shown
$fee = ($transactiontype === 'Wire transfer') ? $page['wirefee'] : $page['domesticfee'];

// Map status to colour
$statusColor = match(strtolower($trans_status)) {
    'approved', 'completed', 'success' => '#28A745',
    'pending'                          => '#EBBC46',
    'processing'                       => '#49786B',
    'failed', 'declined'               => '#D55672',
    default                            => '#7A7A7A'
};

$statusIcon = match(strtolower($trans_status)) {
    'approved', 'completed', 'success' => 'check-circle',
    'pending'                          => 'clock',
    'processing'                       => 'loader',
    'failed', 'declined'               => 'x-circle',
    default                            => 'info'
};

// Clean up sessions
unset($_SESSION['is_dom_transfer']);
unset($_SESSION['is_wire_transfer']);
?>

<!-- Main Content Area -->
<main class="pt-10 md:pt-14 pb-24 md:pb-8 min-h-screen lg:ml-80">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">

<style>
    @keyframes pop-in {
        0%   { transform: scale(0.5); opacity: 0; }
        70%  { transform: scale(1.1); opacity: 1; }
        100% { transform: scale(1); opacity: 1; }
    }
    @keyframes slide-up {
        from { transform: translateY(20px); opacity: 0; }
        to   { transform: translateY(0);    opacity: 1; }
    }
    @keyframes ripple {
        0%   { transform: scale(1);   opacity: 0.4; }
        100% { transform: scale(2.5); opacity: 0; }
    }
    .pop-in    { animation: pop-in  0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards; }
    .slide-up  { animation: slide-up 0.4s ease forwards; }
    .ripple-ring {
        position: absolute;
        inset: 0;
        border-radius: 9999px;
        border: 2px solid <?= $statusColor ?>;
        animation: ripple 1.5s ease-out infinite;
    }

    @media print {
        /* Reset everything */
        *, *::before, *::after {
            animation: none !important;
            transition: none !important;
        }

        html, body {
            margin: 0 !important;
            padding: 0 !important;
            height: auto !important;
            overflow: visible !important;
            -webkit-print-color-adjust: exact !important;
            print-color-adjust: exact !important;
        }

        @page {
            size: A4;
            margin: 10mm;
        }

        /* Hide non-print elements */
        .no-print,
        .appHeader,
        .appBottomMenu,
        nav,
        aside,
        header,
        footer,
        .sidebar,
        .ripple-ring { display: none !important; }

        /* Reset layout */
        main {
            margin: 0 !important;
            padding: 0 !important;
            min-height: auto !important;
        }
        main > div {
            margin: 0 !important;
            padding: 0 !important;
        }

        /* Compact spacing */
        .max-w-lg { max-width: 100% !important; }
        .space-y-4 > * + * { margin-top: 8px !important; }

        /* Compact cards */
        .rounded-xl { border-radius: 6px !important; }
        .p-8, .p-6 { padding: 12px !important; }
        .mb-6 { margin-bottom: 8px !important; }
        .mb-3 { margin-bottom: 4px !important; }

        /* Smaller status icon area */
        .w-20 { width: 48px !important; height: 48px !important; }
        .w-20 i { width: 24px !important; height: 24px !important; }
        .mb-6:has(.ripple-ring) { margin-bottom: 8px !important; }

        /* Compact text */
        .text-4xl { font-size: 22px !important; line-height: 1.2 !important; }
        .text-2xl, .text-xl { font-size: 16px !important; }
        .text-sm { font-size: 11px !important; line-height: 1.3 !important; }
        .text-xs { font-size: 10px !important; line-height: 1.3 !important; }

        /* Compact rows */
        .py-3 { padding-top: 4px !important; padding-bottom: 4px !important; }
        .mb-4 { margin-bottom: 6px !important; }
        .space-y-0 > * + * { margin-top: 0 !important; }

        /* Section headers */
        h3.uppercase { margin-bottom: 4px !important; }

        /* Info banner compact */
        .bg-\\[\\#EBBC46\\]\\/10 { padding: 8px !important; }
        .bg-\\[\\#EBBC46\\]\\/10 p { font-size: 10px !important; }

        /* Avoid page breaks inside cards */
        .bg-white { break-inside: avoid; page-break-inside: avoid; }

        /* Thank you note */
        .pb-4 { padding-bottom: 4px !important; }
    }
</style>

<!-- Back Button -->
<div class="mb-6 no-print">
    <div class="flex items-center space-x-3">
        <a href="dashboard.php" class="w-9 h-9 rounded-lg bg-white border border-[#E0E5E5] flex items-center justify-center hover:bg-[#DFE5E5] transition-colors">
            <i data-lucide="arrow-left" class="w-4 h-4 text-[#7A7A7A]"></i>
        </a>
        <div>
            <h1 class="text-xl md:text-2xl font-semibold text-[#0D1B23]">Transaction Receipt</h1>
            <p class="text-sm text-[#7A7A7A]">Reference: #<?= htmlspecialchars($refrence_id) ?></p>
        </div>
    </div>
</div>

<div class="max-w-lg mx-auto space-y-4">

    <!-- Status Card -->
    <div class="bg-white border border-[#E0E5E5] rounded-xl overflow-hidden">

        <!-- Coloured top bar -->
        <div class="h-1.5" style="background: <?= $statusColor ?>"></div>

        <div class="p-8 text-center">

            <!-- Icon with ripple -->
            <div class="relative inline-flex items-center justify-center mb-6">
                <div class="ripple-ring" style="animation-delay: 0s;"></div>
                <div class="ripple-ring" style="animation-delay: 0.5s;"></div>
                <div class="w-20 h-20 rounded-full flex items-center justify-center pop-in"
                     style="background: <?= $statusColor ?>1A; border: 2px solid <?= $statusColor ?>33;">
                    <i data-lucide="<?= $statusIcon ?>" class="w-9 h-9" style="color: <?= $statusColor ?>"></i>
                </div>
            </div>

            <!-- Status label -->
            <div class="inline-flex items-center px-3 py-1 rounded-full text-xs font-semibold uppercase tracking-wider mb-3"
                 style="background: <?= $statusColor ?>1A; color: <?= $statusColor ?>; border: 1px solid <?= $statusColor ?>33;">
                <?= htmlspecialchars(ucfirst($trans_status)) ?>
            </div>

            <!-- Amount -->
            <div class="slide-up" style="animation-delay: 0.1s;">
                <p class="text-xs uppercase tracking-wider text-[#7A7A7A] mb-1 font-medium">Transaction Amount</p>
                <h2 class="text-4xl font-bold text-[#0D1B23]">
                    <?= $currency ?><?= number_format($amount, 2, '.', ',') ?>
                </h2>
            </div>

            <!-- Transaction type badge -->
            <p class="text-sm text-[#7A7A7A] mt-2">
                <?= htmlspecialchars($transactiontype) ?>
            </p>
        </div>
    </div>

    <!-- Transaction Details Card -->
    <div class="bg-white border border-[#E0E5E5] rounded-xl p-6 slide-up" style="animation-delay: 0.15s;">
        <h3 class="text-xs font-semibold text-[#7A7A7A] uppercase tracking-wider mb-4">Recipient Details</h3>

        <div class="space-y-0">
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Account Name</span>
                <span class="text-sm font-medium text-[#0D1B23]"><?= htmlspecialchars($result['account_name']) ?></span>
            </div>
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Account Number</span>
                <span class="text-sm font-mono text-[#0D1B23]"><?= htmlspecialchars($result['account_number']) ?></span>
            </div>
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Bank Name</span>
                <span class="text-sm text-[#0D1B23]"><?= htmlspecialchars($result['bank_name']) ?></span>
            </div>
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Account Type</span>
                <span class="text-sm text-[#0D1B23]"><?= htmlspecialchars($result['account_type']) ?></span>
            </div>
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Country</span>
                <span class="text-sm text-[#0D1B23]"><?= htmlspecialchars($result['bank_country']) ?></span>
            </div>

            <?php if ($transactiontype === 'Wire transfer'): ?>
            <?php if (!empty($result['routine_number'])): ?>
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Routing Number</span>
                <span class="text-sm font-mono text-[#0D1B23]"><?= htmlspecialchars($result['routine_number']) ?></span>
            </div>
            <?php endif; ?>
            <?php if (!empty($result['swift_code'])): ?>
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">SWIFT / BIC</span>
                <span class="text-sm font-mono text-[#0D1B23]"><?= htmlspecialchars($result['swift_code']) ?></span>
            </div>
            <?php endif; ?>
            <?php endif; ?>
        </div>
    </div>

    <!-- Transaction Summary Card -->
    <div class="bg-white border border-[#E0E5E5] rounded-xl p-6 slide-up" style="animation-delay: 0.2s;">
        <h3 class="text-xs font-semibold text-[#7A7A7A] uppercase tracking-wider mb-4">Transaction Summary</h3>

        <div class="space-y-0">
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Transaction Type</span>
                <span class="text-sm text-[#0D1B23]"><?= htmlspecialchars($transactiontype) ?></span>
            </div>
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Amount</span>
                <span class="text-sm font-semibold text-[#0D1B23]"><?= $currency ?><?= number_format($amount, 2, '.', ',') ?></span>
            </div>
           
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Date & Time</span>
                <span class="text-sm text-[#0D1B23]"><?= htmlspecialchars($created_at) ?></span>
            </div>
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Status</span>
                <span class="text-xs font-semibold px-2.5 py-1 rounded-full"
                      style="background: <?= $statusColor ?>1A; color: <?= $statusColor ?>; border: 1px solid <?= $statusColor ?>33;">
                    <?= htmlspecialchars(ucfirst($trans_status)) ?>
                </span>
            </div>
            <div class="flex justify-between items-center py-3 border-b border-[#F0F0F0]">
                <span class="text-sm text-[#7A7A7A]">Reference ID</span>
                <span class="text-xs font-mono text-[#4A4A4A] bg-[#F0F0F0] px-2 py-1 rounded">#<?= htmlspecialchars($refrence_id) ?></span>
            </div>
            
        </div>
    </div>

    <!-- Info Banner -->
    <?php if (strtolower($trans_status) === 'processing' || strtolower($trans_status) === 'pending'): ?>
    <div class="bg-[#EBBC46]/10 border border-[#EBBC46]/20 rounded-xl p-4 slide-up" style="animation-delay: 0.25s;">
        <div class="flex items-start space-x-3">
            <i data-lucide="clock" class="w-5 h-5 text-[#EBBC46] flex-shrink-0 mt-0.5"></i>
            <div>
                <p class="text-sm font-semibold text-[#0D1B23] mb-0.5">Transfer Under Review</p>
                <p class="text-xs text-[#7A7A7A] leading-relaxed">
                    Your transfer is being processed. 
                    <?= ($transactiontype === 'Wire transfer') ? 'International wire transfers typically take 1–3 business days.' : 'Domestic transfers are usually completed within 24 hours.' ?>
                    You will be notified once it is completed.
                </p>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <!-- Action Buttons -->
    <div class="flex flex-col sm:flex-row gap-3 no-print slide-up" style="animation-delay: 0.3s;">
        <button onclick="window.print()"
            class="flex-1 px-5 py-3.5 rounded-lg border border-[#C8D1D1] hover:bg-[#DFE5E5] transition-colors text-sm font-medium text-[#7A7A7A] flex items-center justify-center space-x-2">
            <i data-lucide="printer" class="w-4 h-4"></i>
            <span>Print Receipt</span>
        </button>
        <a href="dashboard.php"
            class="flex-1 px-5 py-3.5 rounded-lg font-medium text-white text-sm bg-primary-500 hover:bg-primary-600 transition-colors flex items-center justify-center space-x-2">
            <i data-lucide="home" class="w-4 h-4"></i>
            <span>Back to Dashboard</span>
        </a>
    </div>

    <!-- Thank you note -->
    <p class="text-xs text-center text-[#7A7A7A] pb-4 slide-up" style="animation-delay: 0.35s;">
        <i data-lucide="shield" class="w-3 h-3 inline mr-1"></i>
        Thank you for banking with us. Keep your reference ID for your records.
    </p>

</div>
</div>
</main>

<script>
document.addEventListener('DOMContentLoaded', function () {
    lucide.createIcons();
});
</script>

<?php
// Clear transfer sessions after displaying
unset($_SESSION['dom_transfer']);
unset($_SESSION['is_transfer']);

include($_SERVER['DOCUMENT_ROOT'] . "/user/layout/footer.php");
?>