<?php
session_start();
// ১. অ্যাডমিন লগইন চেক
if (!isset($_SESSION['admin_logged_in'])) { 
    header("Location: login.php"); 
    exit(); 
}

require_once '../common/config.php';

// ২. পেমেন্ট স্ট্যাটাস আপডেট লজিক
if (isset($_GET['action']) && isset($_GET['id'])) {
    $deposit_id = intval($_GET['id']);
    $action = $_GET['action'];

    // রিকোয়েস্ট ডাটা আনা
    $stmt = $conn->prepare("SELECT * FROM deposits WHERE id = ? AND status = 'pending'");
    $stmt->bind_param("i", $deposit_id);
    $stmt->execute();
    $deposit = $stmt->get_result()->fetch_assoc();

    if ($deposit) {
        $user_id = $deposit['user_id'];
        $amount = $deposit['amount'];

        if ($action === 'approve') {
            // ইউজারের ব্যালেন্স আপডেট করা
            $update_user = $conn->query("UPDATE users SET balance = balance + $amount WHERE id = $user_id");
            if ($update_user) {
                $conn->query("UPDATE deposits SET status = 'approved' WHERE id = $deposit_id");
                $msg = "success_approved";
            }
        } elseif ($action === 'reject') {
            $conn->query("UPDATE deposits SET status = 'rejected' WHERE id = $deposit_id");
            $msg = "success_rejected";
        }
        header("Location: manage-payments.php?msg=$msg");
        exit();
    }
}

include '../admin/header.php';

// ৩. সব ডিপোজিট ডাটা আনা (লেটেস্টগুলো আগে)
$deposits = $conn->query("SELECT d.*, u.username FROM deposits d JOIN users u ON d.user_id = u.id ORDER BY d.id DESC");
?>

<div class="max-w-7xl mx-auto px-4 py-10 sm:px-6 lg:px-8">
    
    <!-- হেডার -->
    <div class="mb-10">
        <h2 class="text-2xl md:text-3xl font-bold logo-font tracking-wider uppercase">
            PAYMENT <span class="text-cyan-400 glow-text">CONTROL</span>
        </h2>
        <p class="text-gray-500 text-xs font-bold uppercase tracking-[3px] mt-1">Verify and Authorize User Credits</p>
    </div>

    <!-- নোটিফিকেশন -->
    <?php if(isset($_GET['msg'])): ?>
        <div class="bg-cyan-500/10 border border-cyan-500/50 text-cyan-400 p-4 rounded-xl mb-8 font-bold uppercase text-xs tracking-widest animate-pulse">
            <i class="fa-solid fa-circle-check mr-2"></i> 
            <?php 
                if($_GET['msg'] == 'success_approved') echo "Protocol Executed: Funds Credited to User Account!";
                if($_GET['msg'] == 'success_rejected') echo "Protocol Executed: Deposit Request Terminated!";
            ?>
        </div>
    <?php endif; ?>

    <!-- রিকোয়েস্ট লিস্ট (রেসপনসিভ কার্ড ভিউ) -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        <?php if($deposits->num_rows > 0): ?>
            <?php while($row = $deposits->fetch_assoc()): ?>
                
                <div class="cyber-card p-6 flex flex-col justify-between border-l-4 <?php 
                    echo $row['status'] == 'pending' ? 'border-yellow-500/50' : ($row['status'] == 'approved' ? 'border-green-500/50' : 'border-red-500/50'); 
                ?> relative overflow-hidden">
                    
                    <div class="flex justify-between items-start mb-6">
                        <div>
                            <p class="text-[10px] font-black text-gray-500 uppercase tracking-widest leading-none mb-1">Requester</p>
                            <h3 class="font-bold text-white text-lg uppercase tracking-wider"><?php echo $row['username']; ?></h3>
                        </div>
                        <div class="text-right">
                            <p class="text-[10px] font-black text-gray-500 uppercase tracking-widest leading-none mb-1">Amount</p>
                            <h3 class="font-bold text-cyan-400 text-xl tracking-tighter">৳<?php echo number_format($row['amount'], 2); ?></h3>
                        </div>
                    </div>

                    <div class="space-y-4 bg-black/30 p-4 rounded-xl border border-white/5 mb-6">
                        <div class="flex justify-between items-center">
                            <span class="text-[9px] font-bold text-gray-500 uppercase tracking-widest">Gateway</span>
                            <span class="text-xs font-bold text-white uppercase"><?php echo $row['method']; ?></span>
                        </div>
                        <div class="flex flex-col">
                            <span class="text-[9px] font-bold text-gray-500 uppercase tracking-widest mb-1">Transaction ID</span>
                            <span class="text-[11px] font-mono font-bold text-cyan-200 select-all"><?php echo $row['transaction_id']; ?></span>
                        </div>
                    </div>

                    <div class="flex items-center justify-between mb-6">
                        <span class="text-[10px] font-bold text-gray-500">
                            <i class="fa-solid fa-calendar-day mr-1 opacity-50"></i> <?php echo date('d M, h:i A', strtotime($row['created_at'])); ?>
                        </span>
                        
                        <!-- স্ট্যাটাস ব্যাজ -->
                        <span class="px-3 py-1 rounded-full text-[9px] font-black uppercase tracking-widest <?php 
                            echo $row['status'] == 'pending' ? 'bg-yellow-500/10 text-yellow-500 border border-yellow-500/30' : ($row['status'] == 'approved' ? 'bg-green-500/10 text-green-500 border border-green-500/30' : 'bg-red-500/10 text-red-500 border border-red-500/30'); 
                        ?>">
                            <?php echo $row['status']; ?>
                        </span>
                    </div>

                    <!-- অ্যাকশন বাটনস (শুধুমাত্র পেন্ডিং রিকোয়েস্টের জন্য) -->
                    <?php if($row['status'] == 'pending'): ?>
                        <div class="grid grid-cols-2 gap-3 mt-auto">
                            <a href="?action=approve&id=<?php echo $row['id']; ?>" 
                               onclick="return confirm('Confirm Credit Transmission?')"
                               class="bg-green-600 hover:bg-green-500 text-white text-center py-2.5 rounded-lg text-[10px] font-black uppercase tracking-widest transition-all shadow-[0_0_15px_rgba(34,197,94,0.3)]">
                                <i class="fa-solid fa-check mr-1"></i> Approve
                            </a>
                            <a href="?action=reject&id=<?php echo $row['id']; ?>" 
                               onclick="return confirm('Abort this Transaction?')"
                               class="bg-red-600 hover:bg-red-500 text-white text-center py-2.5 rounded-lg text-[10px] font-black uppercase tracking-widest transition-all shadow-[0_0_15px_rgba(239,68,68,0.3)]">
                                <i class="fa-solid fa-xmark mr-1"></i> Reject
                            </a>
                        </div>
                    <?php endif; ?>

                </div>

            <?php endwhile; ?>
        <?php else: ?>
            <div class="col-span-full text-center py-24 cyber-card border-dashed border-cyan-500/20">
                <i class="fa-solid fa-receipt text-6xl text-cyan-500/20 mb-4"></i>
                <p class="text-gray-500 font-bold uppercase tracking-[5px]">No Transactions Logged</p>
            </div>
        <?php endif; ?>
    </div>

</div>

<style>
    .cyber-card {
        background: rgba(15, 23, 42, 0.7);
        backdrop-filter: blur(20px);
        border: 1px solid rgba(255, 255, 255, 0.05);
        border-radius: 1.5rem;
    }
</style>

<?php include '../common/footer.php'; ?>