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

// Fetch available accounts grouped by platform
$accounts = $pdo->query("SELECT id, platform, username, followers, price, profile_url FROM marketplace_accounts WHERE status = 'available' AND approval_status = 'approved' ORDER BY platform, created_at DESC")->fetchAll(PDO::FETCH_ASSOC);

// Count by platform
$platform_counts = [];
foreach ($accounts as $acc) {
    $platform_counts[$acc['platform']] = ($platform_counts[$acc['platform']] ?? 0) + 1;
}
?>
<title><?php echo $page_title; ?></title>

<!-- Messages -->
<?php if (isset($_SESSION['mp_success'])): ?>
<div class="bg-green-50 border border-green-200 rounded-2xl p-4 mb-5 flex items-center gap-3">
    <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center flex-shrink-0">
        <i class="fa-solid fa-circle-check text-green-500 text-lg"></i>
    </div>
    <div>
        <p class="text-sm font-bold text-green-900"><?php echo htmlspecialchars($_SESSION['mp_success']); ?></p>
        <a href="my-accounts" class="text-xs text-green-700 font-semibold hover:underline">View My Accounts →</a>
    </div>
</div>
<?php unset($_SESSION['mp_success']); endif; ?>
<?php if (isset($_SESSION['mp_error'])): ?>
<div class="bg-red-50 border border-red-200 rounded-2xl p-4 mb-5 flex items-center gap-3">
    <div class="w-10 h-10 rounded-full bg-red-100 flex items-center justify-center flex-shrink-0">
        <i class="fa-solid fa-circle-exclamation text-red-500 text-lg"></i>
    </div>
    <p class="text-sm font-bold text-red-900"><?php echo htmlspecialchars($_SESSION['mp_error']); ?></p>
</div>
<?php unset($_SESSION['mp_error']); endif; ?>

<!-- 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="absolute -left-4 -bottom-4 w-20 h-20 bg-white opacity-[0.03] 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-store text-lg"></i>
                </div>
                <div>
                    <h2 class="text-lg font-bold">Account Marketplace</h2>
                    <p class="text-xs opacity-70">Premium social media accounts ready for instant delivery</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($accounts); ?></p>
                    <p class="text-[9px] opacity-60 uppercase tracking-wider">Available</p>
                </div>
                <div class="bg-white/10 rounded-lg px-3 py-1.5 text-center">
                    <p class="text-lg font-bold"><?php echo count($platform_counts); ?></p>
                    <p class="text-[9px] opacity-60 uppercase tracking-wider">Platforms</p>
                </div>
                <a href="my-accounts" class="ml-auto bg-white/15 border border-white/25 rounded-xl px-4 py-2 text-xs font-bold hover:bg-white/25 transition-colors flex items-center gap-2">
                    <i class="fa-solid fa-box"></i> My Purchases
                </a>
            </div>
        </div>
    </div>
</section>

<!-- Platform Filter Tabs -->
<?php if (!empty($platform_counts)): ?>
<section class="mb-5">
    <div class="flex gap-2 overflow-x-auto pb-2" style="scrollbar-width:none;-webkit-overflow-scrolling:touch;">
        <button onclick="filterPlatform('all')" class="platform-tab active flex-shrink-0 px-4 py-2 rounded-xl text-xs font-bold border transition-all" data-platform="all">
            All <span class="ml-1 opacity-60">(<?php echo count($accounts); ?>)</span>
        </button>
        <?php foreach ($platform_counts as $plat => $count): ?>
        <button onclick="filterPlatform('<?php echo htmlspecialchars($plat); ?>')" class="platform-tab flex-shrink-0 px-4 py-2 rounded-xl text-xs font-bold border transition-all" data-platform="<?php echo htmlspecialchars($plat); ?>">
            <?php echo htmlspecialchars($plat); ?> <span class="ml-1 opacity-60">(<?php echo $count; ?>)</span>
        </button>
        <?php endforeach; ?>
    </div>
</section>
<?php endif; ?>

<style>
.platform-tab{background:#fff;border-color:rgba(0,0,0,.08);color:#64748b;}
.platform-tab.active{background:#4D148C;border-color:#4D148C;color:#fff;}
.platform-tab:hover:not(.active){border-color:#4D148C;color:#4D148C;}
.account-card{transition:transform .2s,box-shadow .2s;}
.account-card:hover{transform:translateY(-2px);box-shadow:0 8px 25px rgba(0,0,0,.08);}
</style>

<!-- Accounts Grid -->
<section class="mb-8">
    <?php if (empty($accounts)): ?>
    <div class="bg-white rounded-2xl border border-gray-100 p-10 text-center">
        <div class="w-16 h-16 rounded-full bg-gray-50 flex items-center justify-center mx-auto mb-4">
            <i class="fa-solid fa-store text-2xl text-gray-300"></i>
        </div>
        <p class="text-sm font-bold text-gray-600 mb-1">No accounts available</p>
        <p class="text-xs text-gray-400">New accounts are added regularly. Check back soon!</p>
    </div>
    <?php else: ?>
    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4" id="accountsGrid">
        <?php foreach ($accounts as $acc): 
            $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[$acc['platform']] ?? 'fa-globe';
            $gradient = $colors[$acc['platform']] ?? 'from-gray-500 to-gray-600';
        ?>
        <div class="account-card bg-white rounded-2xl border border-gray-100 overflow-hidden" data-platform="<?php echo htmlspecialchars($acc['platform']); ?>">
            <!-- Card Header -->
            <div class="bg-gradient-to-r <?php echo $gradient; ?> p-4 flex items-center gap-3">
                <div class="w-11 h-11 rounded-full bg-white/20 flex items-center justify-center">
                    <i class="fa-brands <?php echo $icon; ?> text-white text-xl"></i>
                </div>
                <div class="flex-1 min-w-0">
                    <p class="text-white text-sm font-bold truncate"><?php echo htmlspecialchars($acc['username']); ?></p>
                    <p class="text-white/70 text-[10px] uppercase font-semibold tracking-wide"><?php echo htmlspecialchars($acc['platform']); ?></p>
                </div>
            </div>
            <!-- Card Body -->
            <div class="p-4">
                <div class="flex items-center justify-between mb-4">
                    <div class="flex items-center gap-1.5">
                        <i class="fa-solid fa-users text-[10px] text-gray-400"></i>
                        <span class="text-xs text-gray-600 font-medium"><?php echo number_format($acc['followers']); ?> followers</span>
                    </div>
                    <div class="flex items-center gap-1">
                        <i class="fa-solid fa-shield-check text-[10px] text-green-500"></i>
                        <span class="text-[10px] text-green-600 font-semibold">Verified</span>
                    </div>
                </div>
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-[10px] text-gray-400 uppercase font-semibold">Price</p>
                        <p class="text-lg font-bold text-primary">₦<?php echo number_format($acc['price'], 0); ?></p>
                    </div>
                    <div class="flex items-center gap-2">
                        <?php if (!empty($acc['profile_url'])): ?>
                        <a href="<?php echo htmlspecialchars($acc['profile_url']); ?>" target="_blank" class="bg-gray-100 hover:bg-gray-200 text-gray-600 text-[10px] font-bold px-3 py-2.5 rounded-xl transition-all flex items-center gap-1">
                            <i class="fa-solid fa-eye text-[9px]"></i> Preview
                        </a>
                        <?php endif; ?>
                        <button onclick="confirmPurchase(<?php echo $acc['id']; ?>, '<?php echo htmlspecialchars(addslashes($acc['platform'])); ?>', '<?php echo htmlspecialchars(addslashes($acc['username'])); ?>', <?php echo $acc['price']; ?>)" 
                                class="bg-accent hover:bg-orange-700 text-white text-xs font-bold px-5 py-2.5 rounded-xl transition-all flex items-center gap-2 shadow-sm shadow-orange-200">
                            <i class="fa-solid fa-cart-shopping text-[10px]"></i> Buy Now
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</section>

<!-- How It Works -->
<section class="mb-6">
    <h3 class="font-bold text-sm text-gray-500 uppercase tracking-wider mb-3">How It Works</h3>
    <div class="grid grid-cols-1 sm:grid-cols-3 gap-3">
        <div class="bg-white rounded-2xl border border-gray-100 p-4 text-center">
            <div class="w-10 h-10 rounded-full bg-purple-50 flex items-center justify-center mx-auto mb-2">
                <i class="fa-solid fa-magnifying-glass text-primary text-sm"></i>
            </div>
            <p class="text-xs font-bold text-gray-800">1. Browse</p>
            <p class="text-[10px] text-gray-400 mt-1">Find the account you want</p>
        </div>
        <div class="bg-white rounded-2xl border border-gray-100 p-4 text-center">
            <div class="w-10 h-10 rounded-full bg-orange-50 flex items-center justify-center mx-auto mb-2">
                <i class="fa-solid fa-wallet text-accent text-sm"></i>
            </div>
            <p class="text-xs font-bold text-gray-800">2. Pay</p>
            <p class="text-[10px] text-gray-400 mt-1">Deducted from your wallet</p>
        </div>
        <div class="bg-white rounded-2xl border border-gray-100 p-4 text-center">
            <div class="w-10 h-10 rounded-full bg-green-50 flex items-center justify-center mx-auto mb-2">
                <i class="fa-solid fa-key text-green-500 text-sm"></i>
            </div>
            <p class="text-xs font-bold text-gray-800">3. Get Access</p>
            <p class="text-[10px] text-gray-400 mt-1">Credentials revealed instantly</p>
        </div>
    </div>
</section>

<script>
// Platform filter
function filterPlatform(platform) {
    document.querySelectorAll('.platform-tab').forEach(function(tab) {
        tab.classList.toggle('active', tab.dataset.platform === platform);
    });
    document.querySelectorAll('.account-card').forEach(function(card) {
        if (platform === 'all' || card.dataset.platform === platform) {
            card.style.display = '';
        } else {
            card.style.display = 'none';
        }
    });
}

// Purchase confirmation
function confirmPurchase(id, platform, username, price) {
    Swal.fire({
        title: '<span style="font-size:18px;font-weight:700;">Confirm Purchase</span>',
        html: '<div style="text-align:left;font-size:13px;line-height:1.8;padding:8px 0;">' +
              '<div style="background:#f8fafc;border-radius:12px;padding:12px;margin-bottom:12px;">' +
              '<p><strong>Platform:</strong> ' + platform + '</p>' +
              '<p><strong>Account:</strong> ' + username + '</p>' +
              '<p><strong>Price:</strong> <span style="color:#4D148C;font-weight:700;">₦' + price.toLocaleString('en-NG') + '</span></p>' +
              '</div>' +
              '<p style="color:#64748b;font-size:11px;"><i class="fa-solid fa-info-circle"></i> Amount will be deducted from your wallet. Login credentials will be revealed after purchase.</p></div>',
        showCancelButton: true,
        confirmButtonColor: '#FF6600',
        cancelButtonColor: '#e2e8f0',
        confirmButtonText: '<i class="fa-solid fa-cart-shopping"></i> Confirm Purchase',
        cancelButtonText: '<span style="color:#64748b;">Cancel</span>',
        customClass: { popup: 'rounded-2xl' }
    }).then(function(result) {
        if (result.isConfirmed) {
            window.location.href = '<?php echo $site_data['web_url']; ?>/user/ajax/marketplace-buy.php?id=' + id;
        }
    });
}
</script>

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