<?php
include "config.php";

$filter = $_GET['filter'] ?? null;

if ($filter) {
    $stmt = $dbh->prepare("SELECT * FROM ea_tools WHERE category = :filter ORDER BY id DESC");
    $stmt->execute(['filter' => $filter]);
} else {
    $stmt = $dbh->query("SELECT * FROM ea_tools ORDER BY id DESC");
}
$eaItems = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Output only the inner HTML for #ea-list container
foreach ($eaItems as $ea): ?>
  <div class="ea-card" >
    <div class="ea-card-header">
      <span class="ea-badge"><?= htmlspecialchars($ea['category']) ?></span> 
      <span class="ea-title"><?= htmlspecialchars($ea['title']) ?></span>
    </div>
    <div class="ea-card-income">
      Income in last year <span class="ea-income"><?= htmlspecialchars($ea['income_last_year'] ?: 'N/A') ?></span>
    </div>
    <div class="ea-card-desc"><?= htmlspecialchars($ea['description']) ?></div>
    <div class="ea-card-footer">
      <span class="ea-price">USD <?= number_format($ea['price'], 2) ?></span>
      <?php if ($ea['old_price']): ?>
        <span class="ea-old-price">USD <?= number_format($ea['old_price'], 2) ?></span>
      <?php endif; ?>
      <button class="ea-btn"><i class="fas fa-shopping-cart"></i> Purchase</button>
    </div>
  </div>
<?php endforeach; ?>
