<?php
session_start();
require_once "include/db.php";

if (!isset($_SESSION['user_id']) || !isset($_GET['ref'])) {
    header("Location: deposit.php");
    exit;
}

$ref = mysqli_real_escape_string($conn, $_GET['ref']);
$query = "SELECT * FROM transactions WHERE ref = '$ref' LIMIT 1";
$result = mysqli_query($conn, $query);
$txn = mysqli_fetch_assoc($result);

// Fetch Payment Details from Database based on the method used in the transaction
$method = $txn['method'];
$detailQuery = mysqli_query($conn, "SELECT * FROM payment_details WHERE method_slug = '$method' LIMIT 1");
$payDetail = mysqli_fetch_assoc($detailQuery);

include "include/header.php";
?>

<?php include "include/nav.php" ?>

<div class="flex min-h-screen transition-colors duration-300">
    <?php include "include/sidebar.php" ?>
    
    <main class="flex-1 md:ml-72 pb-24">
        <div class="max-w-3xl mx-auto px-4 py-8">
            
            <!-- BACK BUTTON -->
            <div class="mb-6">
                <a href="deposit.php" class="inline-flex items-center gap-2 text-[10px] font-black uppercase tracking-widest text-slate-400 hover:text-blue-500 transition-colors">
                    <i data-lucide="arrow-left" class="w-4 h-4"></i>
                    Back to Deposit
                </a>
            </div>

            <div class="glass-card rounded-[2.5rem] p-8 border border-blue-500/10 shadow-2xl">
                <div class="text-center mb-8">
                    <p class="text-[10px] font-black uppercase text-slate-400">Order Reference: #<?= $txn['ref'] ?></p>
                    <p class="text-[10px] font-black uppercase text-slate-400 mt-2">Amount Due</p>
                    <h2 class="text-5xl font-black text-blue-600">$<?= number_format($txn['amount'], 2) ?></h2>
                </div>

                <!-- DYNAMIC PAYMENT DETAILS -->
                <div class="bg-slate-50 dark:bg-slate-900/50 rounded-3xl p-6 mb-8 border border-slate-200 dark:border-slate-800">
                    <?php if($method == 'bank'): ?>
                        <!-- Bank Display -->
                        <div class="space-y-4 text-left">
                            <div class="flex justify-between border-b border-slate-200 dark:border-slate-800 pb-2">
                                <span class="text-xs text-slate-400 font-bold uppercase">Bank Name</span>
                                <span class="font-black text-slate-900 dark:text-white"><?= $payDetail['val_2'] ?></span>
                            </div>
                            <div class="flex justify-between border-b border-slate-200 dark:border-slate-800 pb-2">
                                <span class="text-xs text-slate-400 font-bold uppercase">Account Number</span>
                                <span class="font-black text-blue-500" id="accNum"><?= $payDetail['val_1'] ?></span>
                            </div>
                            <div class="flex justify-between">
                                <span class="text-xs text-slate-400 font-bold uppercase">Account Name</span>
                                <span class="font-black text-slate-900 dark:text-white"><?= $payDetail['val_3'] ?></span>
                            </div>
                        </div>
                    <?php else: ?>
                        <!-- Crypto Display -->
                        <div class="text-center space-y-4">
                            <img src="https://api.qrserver.com/v1/create-qr-code/?data=<?= $payDetail['val_1'] ?>" class="mx-auto w-32 h-32 rounded-xl mb-4 bg-white p-2">
                            <p class="text-[10px] font-black text-slate-400 uppercase">Deposit Address (<?= $payDetail['val_2'] ?>)</p>
                            <code class="block bg-blue-500/5 p-3 rounded-xl text-blue-500 font-bold break-all"><?= $payDetail['val_1'] ?></code>
                        </div>
                    <?php endif; ?>
                </div>

                <!-- PROOF UPLOAD FORM -->
                <form action="process/submit_proof.php" method="POST" enctype="multipart/form-data" class="space-y-6">
                    <input type="hidden" name="ref" value="<?= $ref ?>">
                    
                    <div class="text-left">
                        <label class="text-[10px] font-black uppercase text-slate-400 px-2 block mb-2">Upload Payment Receipt</label>
                        <div class="relative group">
                            <input type="file" name="proof_img" required class="absolute inset-0 w-full h-full opacity-0 cursor-pointer z-10">
                            <div class="border-2 border-dashed border-slate-300 dark:border-slate-800 rounded-2xl py-8 text-center group-hover:border-blue-500 transition-all">
                                <i data-lucide="upload-cloud" class="w-8 h-8 mx-auto text-slate-400 mb-2"></i>
                                <p class="text-xs font-bold text-slate-500">Click or Drag Image Proof</p>
                            </div>
                        </div>
                    </div>

                    <button type="submit" class="w-full py-5 bg-blue-600 text-white font-black rounded-2xl shadow-xl shadow-blue-600/20 uppercase tracking-widest text-xs active:scale-95 transition-all">
                        Submit for Verification
                    </button>
                </form>
            </div>
        </div>
    </main>
</div>

<script src="https://unpkg.com/lucide@latest"></script>
<script>lucide.createIcons();</script>
</body>
</html>