<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) { header("Location: login.php"); exit(); }
require_once '../common/config.php';

$user_id = intval($_GET['id']);

// ম্যানুয়ালি টাকা অ্যাড করার লজিক
if (isset($_POST['add_balance'])) {
    $amount = floatval($_POST['amount']);
    $conn->query("UPDATE users SET balance = balance + $amount WHERE id = $user_id");
    header("Location: user-details.php?id=$user_id&msg=updated");
    exit();
}

$user = $conn->query("SELECT * FROM users WHERE id = $user_id")->fetch_assoc();
$purchases = $conn->query("SELECT p.*, f.file_title FROM purchases p JOIN files f ON p.file_id = f.id WHERE p.user_id = $user_id ORDER BY p.purchase_date DESC");

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

<div class="max-w-5xl mx-auto px-4 py-10">
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        <!-- বাম পাশে প্রোফাইল ও টাকা অ্যাড -->
        <div class="lg:col-span-1 space-y-6">
            <div class="cyber-card p-6 border-t-4 border-cyan-500/50">
                <h3 class="text-white font-bold uppercase mb-4 logo-font">User Info</h3>
                <p class="text-gray-400 text-xs uppercase mb-1">Username: <span class="text-white"><?php echo $user['username']; ?></span></p>
                <p class="text-gray-400 text-xs uppercase mb-6">Balance: <span class="text-cyan-400 font-bold">৳<?php echo number_format($user['balance'], 2); ?></span></p>
                
                <form action="" method="POST" class="pt-6 border-t border-white/5">
                    <label class="block text-[10px] font-bold text-gray-500 uppercase mb-2">Add Credits Manually</label>
                    <input type="number" name="amount" required placeholder="Amount" class="w-full bg-slate-900 border border-white/10 rounded-lg py-2 px-4 text-white mb-3 outline-none focus:border-cyan-500">
                    <button type="submit" name="add_balance" class="w-full bg-cyan-600 text-white py-2 rounded-lg text-[10px] font-black uppercase">Execute Addition</button>
                </form>
            </div>
        </div>

        <!-- ডান পাশে পারচেজ হিস্টোরি -->
        <div class="lg:col-span-2">
            <div class="cyber-card p-6 border-t-4 border-purple-500/50">
                <h3 class="text-white font-bold uppercase mb-6 logo-font">Purchase Logs</h3>
                <div class="space-y-4">
                    <?php while($p = $purchases->fetch_assoc()): ?>
                        <div class="bg-black/20 p-4 rounded-xl border border-white/5 flex justify-between items-center">
                            <div>
                                <h4 class="text-white font-bold text-sm uppercase tracking-wide"><?php echo $p['file_title']; ?></h4>
                                <p class="text-[9px] text-gray-500 uppercase mt-1">Date: <?php echo date('d M Y, h:i A', strtotime($p['purchase_date'])); ?></p>
                            </div>
                            <span class="text-[9px] font-bold text-green-500 bg-green-500/10 px-2 py-1 rounded border border-green-500/20">SUCCESS</span>
                        </div>
                    <?php endwhile; ?>
                    <?php if($purchases->num_rows == 0) echo "<p class='text-gray-600 text-xs uppercase text-center py-10'>No panels purchased by this user.</p>"; ?>
                </div>
            </div>
        </div>
    </div>
</div>