<?php
ob_start();
include 'master.php';

if (isset($_SESSION['it_receipt'])) {
    $r = $_SESSION['it_receipt'];
    unset($_SESSION['it_receipt']);
} elseif (isset($_SESSION['lt_receipt'])) {
    $r = $_SESSION['lt_receipt'];
    unset($_SESSION['lt_receipt']);
} else {
    ob_end_clean();
    header('Location: index');
    exit;
}
$_sym        = currencySymbol($_SESSION['currency'] ?? 'USD');
$_fullname   = (string)(getUserDetails('fullname', $_SESSION['email']) ?? '');
$_type       = $r['type'] ?? 'Local Transfer';
$_status     = $r['status'] ?? 'Success';
$_is_crypto  = !empty($r['is_crypto']);
$_new_transfer_link = ($_type === 'International Transfer') ? 'internationaltransfer' : 'localtransfer';

// Amount display helpers
$_amt_display      = $_is_crypto
    ? number_format($r['amount'], 8) . ' BTC'
    : $_sym . number_format($r['amount'], 2);
$_prev_bal_display = $_is_crypto
    ? number_format($r['prev_balance'], 8) . ' BTC'
    : $_sym . number_format($r['prev_balance'], 2);
$_new_bal_display  = $_is_crypto
    ? number_format($r['new_balance'], 8) . ' BTC'
    : $_sym . number_format($r['new_balance'], 2);
?>

                <!-- Main Content -->
                <main class="flex-1 overflow-y-auto bg-gray-50 dark:bg-gray-900 min-h-0">
                    <div class="h-full overflow-y-auto">

<div class="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50 to-indigo-100 dark:from-slate-900 dark:via-slate-800 dark:to-slate-900 p-4 lg:p-6">
    <div class="max-w-2xl mx-auto">

        <!-- Mobile header -->
        <div class="lg:hidden mb-4 flex items-center space-x-3">
            <a href="index" class="w-10 h-10 bg-gray-100 dark:bg-gray-800 rounded-full flex items-center justify-center hover:bg-gray-200 dark:hover:bg-gray-700 transition-all">
                <i class="fa-solid fa-arrow-left text-gray-700 dark:text-gray-300"></i>
            </a>
            <p class="text-gray-900 dark:text-white font-semibold text-base">Transfer Receipt</p>
        </div>

        <!-- Desktop header -->
        <div class="hidden lg:flex items-center space-x-3 mb-6">
            <div class="w-10 h-10 rounded-xl bg-gradient-to-r from-emerald-500 to-emerald-600 flex items-center justify-center">
                <i class="fas fa-receipt text-white"></i>
            </div>
            <h1 class="text-3xl font-bold text-gray-900 dark:text-white">Transfer Receipt</h1>
        </div>

        <!-- Success banner -->
        <div class="mb-6 p-5 bg-emerald-50 dark:bg-emerald-900/20 border border-emerald-200 dark:border-emerald-700/50 rounded-2xl flex items-center space-x-4">
            <div class="w-12 h-12 rounded-2xl bg-emerald-100 dark:bg-emerald-900/50 flex items-center justify-center flex-shrink-0">
                <i class="fas fa-check-circle text-2xl text-emerald-600 dark:text-emerald-400"></i>
            </div>
            <div>
                <p class="text-base font-bold text-emerald-800 dark:text-emerald-200">Transfer Successful!</p>
                <p class="text-sm text-emerald-600 dark:text-emerald-400">Your funds have been sent successfully.</p>
            </div>
        </div>

        <!-- Receipt card -->
        <div class="bg-white/95 dark:bg-gray-800/95 backdrop-blur-xl rounded-3xl shadow-xl border border-white/20 dark:border-gray-700/50 overflow-hidden mb-6" id="receiptCard">

            <!-- Receipt header -->
            <div class="relative bg-gradient-to-br from-primary-500 via-primary-600 to-primary-700 dark:from-primary-600 dark:via-primary-700 dark:to-primary-800 p-6">
                <div class="absolute inset-0 opacity-10">
                    <div class="absolute top-0 right-0 w-32 h-32 rounded-full bg-white transform translate-x-16 -translate-y-16"></div>
                    <div class="absolute bottom-0 left-0 w-24 h-24 rounded-full bg-white transform -translate-x-12 translate-y-12"></div>
                </div>
                <div class="relative z-10 text-center">
                    <div class="w-16 h-16 rounded-2xl bg-white/20 backdrop-blur-sm flex items-center justify-center mx-auto mb-3">
                        <i class="fas fa-paper-plane text-white text-2xl"></i>
                    </div>
                    <h2 class="text-2xl font-bold text-white mb-1"><?php echo htmlspecialchars($_type); ?></h2>
                    <p class="text-white/70 text-sm"><?php echo date('l, F j, Y \a\t H:i', strtotime($r['date'])); ?></p>
                </div>
            </div>

            <!-- Amount spotlight -->
            <div class="px-6 py-5 border-b border-gray-100 dark:border-gray-700 text-center bg-gradient-to-br from-gray-50/50 to-white dark:from-gray-800/50 dark:to-gray-800">
                <p class="text-xs font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wider mb-1">Amount Transferred</p>
                <p class="text-4xl font-bold text-gray-900 dark:text-white"><?php echo $_amt_display; ?></p>
                <?php if ($_status === 'Pending'): ?>
                <span class="inline-flex items-center mt-2 px-3 py-1 rounded-full bg-amber-100 dark:bg-amber-900/40 text-amber-700 dark:text-amber-300 text-xs font-semibold">
                    <i class="fas fa-clock mr-1"></i> Pending Review
                </span>
                <?php else: ?>
                <span class="inline-flex items-center mt-2 px-3 py-1 rounded-full bg-emerald-100 dark:bg-emerald-900/40 text-emerald-700 dark:text-emerald-300 text-xs font-semibold">
                    <i class="fas fa-check-circle mr-1"></i> Successful
                </span>
                <?php endif; ?>
            </div>

            <!-- Recipient details -->
            <div class="p-6 space-y-0 divide-y divide-gray-100 dark:divide-gray-700">
                <div class="flex items-center justify-between py-3.5">
                    <span class="text-sm text-gray-500 dark:text-gray-400">Recipient</span>
                    <span class="text-sm font-semibold text-gray-900 dark:text-white"><?php echo htmlspecialchars($r['accountname']); ?></span>
                </div>
                <div class="flex items-center justify-between py-3.5">
                    <span class="text-sm text-gray-500 dark:text-gray-400">Account Number</span>
                    <span class="text-sm font-semibold text-gray-900 dark:text-white font-mono"><?php echo htmlspecialchars($r['accountnumber']); ?></span>
                </div>
                <div class="flex items-center justify-between py-3.5">
                    <span class="text-sm text-gray-500 dark:text-gray-400">Bank</span>
                    <span class="text-sm font-semibold text-gray-900 dark:text-white"><?php echo htmlspecialchars($r['bankname']); ?></span>
                </div>
                <div class="flex items-center justify-between py-3.5">
                    <span class="text-sm text-gray-500 dark:text-gray-400">Account Type</span>
                    <span class="text-sm font-semibold text-gray-900 dark:text-white"><?php echo htmlspecialchars($r['accounttype']); ?></span>
                </div>
                <?php if (!empty($r['description'])): ?>
                <div class="flex items-start justify-between py-3.5">
                    <span class="text-sm text-gray-500 dark:text-gray-400">Description</span>
                    <span class="text-sm font-semibold text-gray-900 dark:text-white text-right max-w-[60%]"><?php echo htmlspecialchars($r['description']); ?></span>
                </div>
                <?php endif; ?>
                <div class="flex items-center justify-between py-3.5">
                    <span class="text-sm text-gray-500 dark:text-gray-400">Date &amp; Time</span>
                    <span class="text-sm font-semibold text-gray-900 dark:text-white"><?php echo date('M d, Y H:i', strtotime($r['date'])); ?></span>
                </div>
            </div>

            <!-- Balance summary -->
            <div class="mx-6 mb-6 p-4 bg-gray-50 dark:bg-gray-700/50 rounded-2xl border border-gray-200/50 dark:border-gray-600/50">
                <p class="text-xs font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wider mb-3">Account Summary</p>
                <div class="space-y-2">
                    <div class="flex justify-between text-sm">
                        <span class="text-gray-500 dark:text-gray-400">Previous Balance</span>
                        <span class="font-medium text-gray-700 dark:text-gray-300"><?php echo $_prev_bal_display; ?></span>
                    </div>
                    <div class="flex justify-between text-sm">
                        <span class="text-gray-500 dark:text-gray-400">Amount Debited</span>
                        <span class="font-medium text-red-600 dark:text-red-400">− <?php echo $_amt_display; ?></span>
                    </div>
                    <div class="border-t border-gray-200 dark:border-gray-600 pt-2 flex justify-between text-sm">
                        <span class="font-semibold text-gray-900 dark:text-white">New Balance</span>
                        <span class="font-bold text-emerald-600 dark:text-emerald-400"><?php echo $_new_bal_display; ?></span>
                    </div>
                </div>
            </div>

            <!-- Notice -->
            <div class="px-6 pb-6">
                <p class="text-xs text-center text-gray-400 dark:text-gray-500">
                    The details shown on this receipt are accurate at the time of generation.<br>
                    For any discrepancies, contact <span class="font-medium"><?php echo htmlspecialchars((string)(getSiteConfig('sitemail') ?? '')); ?></span>
                </p>
            </div>
        </div>

        <!-- Actions -->
        <div class="grid grid-cols-1 sm:grid-cols-3 gap-3 mb-6">
            <button onclick="window.print()"
                class="inline-flex items-center justify-center px-5 py-3 border border-gray-300 dark:border-gray-600 rounded-xl text-sm font-semibold text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 hover:bg-gray-50 dark:hover:bg-gray-700 transition-all shadow-sm">
                <i class="fas fa-print mr-2"></i> Print Receipt
            </button>
            <a href="<?php echo $_new_transfer_link; ?>"
                class="inline-flex items-center justify-center px-5 py-3 border border-primary-300 dark:border-primary-600 rounded-xl text-sm font-semibold text-primary-700 dark:text-primary-300 bg-primary-50/70 dark:bg-primary-900/20 hover:bg-primary-100 dark:hover:bg-primary-900/30 transition-all shadow-sm">
                <i class="fas fa-paper-plane mr-2"></i> New Transfer
            </a>
            <a href="index"
                class="inline-flex items-center justify-center px-5 py-3 border border-transparent rounded-xl text-sm font-semibold text-white bg-gradient-to-r from-primary-500 to-primary-600 hover:from-primary-600 hover:to-primary-700 transition-all shadow-md">
                <i class="fas fa-home mr-2"></i> Dashboard
            </a>
        </div>

    </div>
</div>

<style>
@media print {
    body * { visibility: hidden; }
    #receiptCard, #receiptCard * { visibility: visible; }
    #receiptCard { position: fixed; top: 0; left: 0; width: 100%; }
}
</style>

                    </div>
                </main>
<?php include 'footer.php'; ?>
