<?php
//Developer: CORE TECH WEB DESIGN
include('header.php');
$page_title = WEB_NAME . " | My Purchased Accounts";

// Fetch user's purchased accounts with seller info
$stmt = $pdo->prepare("SELECT ma.*, u.full_name as seller_name, u.phone as seller_phone FROM marketplace_accounts ma LEFT JOIN users u ON ma.seller_id = u.id WHERE ma.buyer_id = ? AND ma.status = 'sold' ORDER BY ma.purchased_at DESC");
$stmt->execute([$user['id']]);
$purchases = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<title><?php echo $page_title; ?></title>

<!-- Header Banner -->
<section class="mb-6">
    <div class="bg-gradient-to-r from-primary to-[#6b23c2] rounded-3xl p-6 text-white relative overflow-hidden">
        <div class="absolute -right-8 -top-8 w-32 h-32 bg-white opacity-5 rounded-full"></div>
        <div class="relative z-10">
            <div class="flex items-center gap-3 mb-2">
                <div class="w-10 h-10 rounded-xl bg-white/15 flex items-center justify-center">
                    <i class="fa-solid fa-box text-lg"></i>
                </div>
                <div>
                    <h2 class="text-lg font-bold">My Purchased Accounts</h2>
                    <p class="text-xs opacity-70">All accounts you've purchased with login credentials</p>
                </div>
            </div>
            <div class="flex items-center gap-4 mt-4">
                <div class="bg-white/10 rounded-lg px-3 py-1.5 text-center">
                    <p class="text-lg font-bold"><?php echo count($purchases); ?></p>
                    <p class="text-[9px] opacity-60 uppercase tracking-wider">Owned</p>
                </div>
                <a href="marketplace" class="ml-auto bg-accent border border-accent rounded-xl px-4 py-2 text-xs font-bold hover:bg-orange-700 transition-colors flex items-center gap-2">
                    <i class="fa-solid fa-store"></i> Buy More
                </a>
            </div>
        </div>
    </div>
</section>

<?php if (empty($purchases)): ?>
<!-- Empty State -->
<section class="mb-6">
    <div class="bg-white rounded-2xl border border-gray-100 p-10 text-center">
        <div class="w-16 h-16 rounded-full bg-purple-50 flex items-center justify-center mx-auto mb-4">
            <i class="fa-solid fa-box-open text-2xl text-primary opacity-50"></i>
        </div>
        <p class="text-sm font-bold text-gray-700 mb-1">No purchases yet</p>
        <p class="text-xs text-gray-400 mb-5">Accounts you buy from the marketplace will appear here with full login credentials.</p>
        <a href="marketplace" class="inline-flex items-center gap-2 bg-primary text-white text-xs font-bold px-5 py-3 rounded-xl hover:bg-purple-800 transition-colors shadow-sm">
            <i class="fa-solid fa-store"></i> Visit Marketplace
        </a>
    </div>
</section>
<?php else: ?>
<!-- Purchased Accounts -->
<section class="mb-6">
    <div class="space-y-4">
        <?php foreach ($purchases as $p): 
            $icons = [
                'Instagram'=>'fa-instagram','TikTok'=>'fa-tiktok','Facebook'=>'fa-facebook',
                'Twitter/X'=>'fa-x-twitter','YouTube'=>'fa-youtube','Snapchat'=>'fa-snapchat',
                'Telegram'=>'fa-telegram','WhatsApp'=>'fa-whatsapp'
            ];
            $colors = [
                'Instagram'=>'from-pink-500 to-purple-600','TikTok'=>'from-gray-900 to-gray-700',
                'Facebook'=>'from-blue-600 to-blue-500','Twitter/X'=>'from-gray-900 to-gray-700',
                'YouTube'=>'from-red-600 to-red-500','Snapchat'=>'from-yellow-400 to-yellow-500',
                'Telegram'=>'from-blue-400 to-blue-500','WhatsApp'=>'from-green-500 to-green-600'
            ];
            $icon = $icons[$p['platform']] ?? 'fa-globe';
            $gradient = $colors[$p['platform']] ?? 'from-gray-500 to-gray-600';
        ?>
        <div class="bg-white rounded-2xl border border-gray-100 overflow-hidden shadow-sm">
            <!-- Card Header -->
            <div class="bg-gradient-to-r <?php echo $gradient; ?> px-5 py-4 flex items-center justify-between">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 rounded-full bg-white/20 flex items-center justify-center">
                        <i class="fa-brands <?php echo $icon; ?> text-white text-lg"></i>
                    </div>
                    <div>
                        <p class="text-white text-sm font-bold"><?php echo htmlspecialchars($p['username']); ?></p>
                        <p class="text-white/60 text-[10px] uppercase font-semibold tracking-wide"><?php echo htmlspecialchars($p['platform']); ?> · <?php echo number_format($p['followers']); ?> followers</p>
                    </div>
                </div>
                <div class="text-right">
                    <span class="bg-white/20 text-white text-[9px] font-bold px-2.5 py-1 rounded-lg uppercase tracking-wide">Owned</span>
                    <p class="text-white/50 text-[9px] mt-1"><?php echo date('M d, Y', strtotime($p['purchased_at'])); ?></p>
                </div>
            </div>

            <!-- Credentials -->
            <div class="p-5">
                <p class="text-[10px] text-gray-400 uppercase font-bold tracking-wider mb-3"><i class="fa-solid fa-key text-primary"></i> Login Credentials</p>
                <div class="bg-slate-50 rounded-xl p-4 space-y-3">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-[10px] text-gray-400 uppercase font-semibold">Email / Phone</p>
                            <p class="text-sm font-bold text-gray-800 font-mono mt-0.5"><?php echo htmlspecialchars($p['login_email']); ?></p>
                        </div>
                        <button onclick="copyText('<?php echo htmlspecialchars(addslashes($p['login_email'])); ?>', this)" class="bg-white border border-gray-200 text-primary text-[10px] font-bold px-3 py-1.5 rounded-lg hover:bg-purple-50 transition-colors">
                            <i class="fa-regular fa-copy"></i> Copy
                        </button>
                    </div>
                    <div class="h-px bg-gray-200"></div>
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-[10px] text-gray-400 uppercase font-semibold">Password</p>
                            <p class="text-sm font-bold text-gray-800 font-mono mt-0.5"><?php echo htmlspecialchars($p['login_password']); ?></p>
                        </div>
                        <button onclick="copyText('<?php echo htmlspecialchars(addslashes($p['login_password'])); ?>', this)" class="bg-white border border-gray-200 text-primary text-[10px] font-bold px-3 py-1.5 rounded-lg hover:bg-purple-50 transition-colors">
                            <i class="fa-regular fa-copy"></i> Copy
                        </button>
                    </div>
                    <?php if (!empty($p['extra_info'])): ?>
                    <div class="h-px bg-gray-200"></div>
                    <div>
                        <p class="text-[10px] text-gray-400 uppercase font-semibold">Extra Info</p>
                        <p class="text-xs text-gray-700 mt-1 leading-relaxed"><?php echo nl2br(htmlspecialchars($p['extra_info'])); ?></p>
                    </div>
                    <?php endif; ?>
                </div>
                <div class="mt-3 flex items-center gap-2">
                    <i class="fa-solid fa-shield-check text-green-500 text-[10px]"></i>
                    <span class="text-[10px] text-gray-400">These credentials are private and only visible to you</span>
                </div>
                <?php if (!empty($p['seller_name'])): ?>
                <div class="mt-3 bg-blue-50 border border-blue-100 rounded-xl p-3 flex items-center gap-3">
                    <div class="w-8 h-8 rounded-full bg-blue-100 flex items-center justify-center flex-shrink-0">
                        <i class="fa-solid fa-user text-blue-500 text-xs"></i>
                    </div>
                    <div>
                        <p class="text-[10px] text-blue-600 uppercase font-bold tracking-wide">Seller Contact</p>
                        <p class="text-xs font-bold text-blue-900"><?php echo htmlspecialchars($p['seller_name']); ?></p>
                        <?php if (!empty($p['seller_phone'])): ?>
                        <p class="text-xs text-blue-700"><?php echo htmlspecialchars($p['seller_phone']); ?></p>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
</section>
<?php endif; ?>

<script>
function copyText(text, btn) {
    if (navigator.clipboard) {
        navigator.clipboard.writeText(text).then(function() {
            var orig = btn.innerHTML;
            btn.innerHTML = '<i class="fa-solid fa-check"></i> Copied!';
            btn.classList.add('bg-green-50', 'text-green-600', 'border-green-200');
            setTimeout(function() { btn.innerHTML = orig; btn.classList.remove('bg-green-50', 'text-green-600', 'border-green-200'); }, 2000);
        });
    } else {
        var el = document.createElement('textarea');
        el.value = text; el.style.position = 'fixed'; el.style.opacity = '0';
        document.body.appendChild(el); el.select();
        document.execCommand('copy'); document.body.removeChild(el);
        var orig = btn.innerHTML;
        btn.innerHTML = '<i class="fa-solid fa-check"></i> Copied!';
        setTimeout(function() { btn.innerHTML = orig; }, 2000);
    }
}
</script>

<?php include('footer.php'); ?>
