<?php
require_once 'common/config.php';
include 'common/header.php';

// ১. ইউজার লগইন না থাকলে লগইন পেজে পাঠিয়ে দিবে
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

// ২. ইউজারের তথ্য এবং কেনা ফাইলের তালিকা আনা
$user_res = $conn->query("SELECT * FROM users WHERE id = $user_id");
$user_info = $user_res->fetch_assoc();

$purchase_query = "SELECT p.purchase_date, f.id as file_id, f.file_title, f.file_name 
                   FROM purchases p 
                   JOIN files f ON p.file_id = f.id 
                   WHERE p.user_id = $user_id 
                   ORDER BY p.purchase_date DESC";
$purchases = $conn->query($purchase_query);
?>

<div class="max-w-7xl mx-auto px-4 py-10 sm:px-6 lg:px-8">
    
    <!-- ড্যাশবোর্ড হেডার -->
    <div class="mb-10 text-center md:text-left">
        <h2 class="text-3xl md:text-5xl font-bold logo-font tracking-tighter text-white uppercase leading-none">
            USER <span class="text-cyan-400 glow-text">DASHBOARD</span>
        </h2>
        <p class="text-[10px] md:text-xs text-gray-500 uppercase tracking-[4px] mt-4 font-black opacity-60">
            Welcome back, <?php echo $user_info['username']; ?>. Terminal Authorized.
        </p>
    </div>

    <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-8 border-t-4 border-cyan-500/50 relative overflow-hidden">
                <div class="absolute -top-10 -right-10 w-32 h-32 bg-cyan-500/5 rounded-full blur-3xl"></div>
                
                <div class="flex flex-col items-center text-center">
                    <div class="w-24 h-24 bg-cyan-500/10 rounded-3xl border border-cyan-500/30 flex items-center justify-center mb-6 shadow-[0_0_20px_rgba(34,211,238,0.2)]">
                        <i class="fa-solid fa-user-gear text-5xl text-cyan-400"></i>
                    </div>
                    <h3 class="text-xl font-bold text-white uppercase tracking-widest mb-1"><?php echo $user_info['username']; ?></h3>
                    <p class="text-xs text-gray-500 font-mono mb-6"><?php echo $user_info['email']; ?></p>
                    
                    <!-- ব্যালেন্স ডিসপ্লে -->
                    <div class="w-full bg-black/40 border border-white/5 rounded-2xl p-6 mb-6">
                        <p class="text-[10px] font-black text-gray-500 uppercase tracking-[3px] mb-2">Available Credits</p>
                        <h4 class="text-3xl font-black text-cyan-400 tracking-tighter">৳<?php echo number_format($user_info['balance'], 2); ?></h4>
                    </div>

                    <div class="grid grid-cols-1 w-full gap-3">
                        <a href="add-money.php" class="bg-cyan-600 hover:bg-cyan-500 text-white font-black py-4 rounded-xl text-[10px] uppercase tracking-[3px] transition-all">
                            <i class="fa-solid fa-plus-circle mr-2"></i> Add Credits
                        </a>
                        <a href="logout.php" class="bg-red-500/10 border border-red-500/30 text-red-500 font-black py-3 rounded-xl text-[10px] uppercase tracking-[3px] hover:bg-red-500 hover:text-white transition-all">
                            <i class="fa-solid fa-power-off mr-2"></i> Terminate Session
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- ডান কলাম: পারচেজ হিস্টোরি -->
        <div class="lg:col-span-2">
            <div class="cyber-card p-6 md:p-10 border-t-4 border-blue-500/50 min-h-[400px]">
                <h3 class="text-xl font-bold logo-font text-white uppercase mb-8 flex items-center">
                    <i class="fa-solid fa-clock-rotate-left mr-3 text-blue-500"></i> Purchase <span class="text-blue-400 ml-2">History</span>
                </h3>

                <?php if($purchases->num_rows > 0): ?>
                    <div class="space-y-4">
                        <?php while($row = $purchases->fetch_assoc()): ?>
                            <div class="group bg-slate-900/40 border border-white/5 p-4 md:p-6 rounded-2xl flex flex-col md:flex-row items-center justify-between gap-4 hover:border-cyan-500/30 transition-all">
                                <div class="flex items-center space-x-4 w-full md:w-auto">
                                    <div class="w-12 h-12 bg-blue-500/10 rounded-xl flex items-center justify-center text-blue-400 border border-blue-500/20">
                                        <i class="fa-solid fa-file-shield"></i>
                                    </div>
                                    <div class="min-w-0">
                                        <h4 class="text-white font-bold text-sm md:text-base truncate uppercase tracking-wider"><?php echo $row['file_title']; ?></h4>
                                        <p class="text-[9px] text-gray-500 uppercase tracking-widest mt-1">
                                            Unlocked on: <?php echo date('d M, Y', strtotime($row['purchase_date'])); ?>
                                        </p>
                                    </div>
                                </div>
                                <div class="flex gap-3 w-full md:w-auto">
                                    <a href="view-file.php?id=<?php echo $row['file_id']; ?>" class="flex-1 md:flex-none bg-blue-500/10 border border-blue-500/30 text-blue-400 px-6 py-2 rounded-lg text-[10px] font-black uppercase tracking-widest hover:bg-blue-500 hover:text-white transition-all text-center">
                                        View
                                    </a>
                                    <a href="uploads/<?php echo $row['file_name']; ?>" download class="flex-1 md:flex-none bg-cyan-500 text-black px-6 py-2 rounded-lg text-[10px] font-black uppercase tracking-widest hover:bg-white transition-all text-center shadow-[0_0_15px_rgba(6,182,212,0.3)]">
                                        Download
                                    </a>
                                </div>
                            </div>
                        <?php endwhile; ?>
                    </div>
                <?php else: ?>
                    <div class="flex flex-col items-center justify-center py-20 text-center opacity-30">
                        <i class="fa-solid fa-box-open text-6xl mb-4"></i>
                        <p class="text-sm font-black uppercase tracking-[5px]">No Assets Unlocked Yet</p>
                        <a href="index.php" class="text-cyan-400 text-[10px] mt-4 underline tracking-widest">Visit File Hub</a>
                    </div>
                <?php endif; ?>
            </div>
        </div>

    </div>
</div>

<style>
    .cyber-card {
        background: rgba(1, 10, 32, 0.85);
        backdrop-filter: blur(25px);
        -webkit-backdrop-filter: blur(25px);
        border: 1px solid rgba(255, 255, 255, 0.05);
        border-radius: 2.5rem;
    }
    .glow-text {
        text-shadow: 0 0 15px rgba(34, 211, 238, 0.6);
    }
</style>

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