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

if (!isset($_GET['id'])) {
    header('Location: admin_reports.php');
    exit;
}

$id = (int)$_GET['id'];
$itemsPerPage = 10; // adjust as needed
// Get current page from query param, default to 1
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $itemsPerPage;

// Count total items
$totalStmt = $dbh->query("SELECT COUNT(*) FROM ea_tools");
$totalItems = $totalStmt->fetchColumn();

// Fetch limited items for current page
$stmt = $dbh->prepare("SELECT * FROM ea_tools ORDER BY id DESC LIMIT :offset, :limit");
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->bindValue(':limit', $itemsPerPage, PDO::PARAM_INT);
$stmt->execute();
$eaItems = $stmt->fetchAll(PDO::FETCH_ASSOC);

$totalPages = ceil($totalItems / $itemsPerPage);
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>EA Tools</title>
  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
  <style>
    body { background: #111; color: white; font-family: Arial, sans-serif; }
    .table thead th { background: #222; }
    .table tbody tr:nth-child(even) { background: #1a1a1a; }
    .btn-custom {
      background-color: #0b5ed7;
      color: #fff;
      border-radius: 4px;
    }
    .btn-custom:hover {
      background-color: #0a53be;
      color: #fff;
    }
    /* Horizontal scroll on small devices */
    .table-responsive {
      overflow-x: auto;
    }
    /* Search box styling */
    #searchInput {
      max-width: 300px;
      margin-bottom: 1rem;
    }
  </style>
</head>
<body>

<?php include "includes/header.php"; ?>

<div class="container my-4">
  <div class="d-flex justify-content-between align-items-center mb-3 flex-wrap">
    <a href="add_ea.php" class="btn btn-custom mb-2 text-white">Add New EA</a>
    <input type="search" id="searchInput" class="form-control" placeholder="Search..." />
  </div>

  <h2 class="text-center mb-3 text-white">EA Tools List</h2>

  <div class="table-responsive">
    <table class="table table-bordered table-striped text-white align-middle ">
      <thead >
        <tr>
          <th class="text-white">ID</th>
          <th class="text-white">Category</th>
          <th class="text-white">Title</th>
          <th class="text-white">Income</th>
          <th class="text-white">Price</th>
          <th class="text-white">Old Price</th>
          <th class="text-white">Actions</th>
        </tr>
      </thead>
      <tbody id="eaTableBody">
        <?php foreach ($eaItems as $ea): ?>
        <tr>
          <td><?= $ea['id'] ?></td>
          <td><?= htmlspecialchars($ea['category']) ?></td>
          <td><?= htmlspecialchars($ea['title']) ?></td>
          <td><?= htmlspecialchars($ea['income_last_year']) ?></td>
          <td>USD <?= number_format($ea['price'], 2) ?></td>
          <td>USD <?= number_format($ea['old_price'], 2) ?></td>
          <td>
            <a href="edit_ea.php?id=<?= $ea['id'] ?>" class="btn btn-warning btn-sm me-1 mb-1">Edit</a>
            <a href="delete_ea.php?id=<?= $ea['id'] ?>" class="btn btn-danger btn-sm mb-1" onclick="return confirm('Are you sure?')">Delete</a>
          </td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>

  <!-- Pagination -->
  <nav aria-label="Page navigation">
    <ul class="pagination justify-content-center">
      <?php if ($page > 1): ?>
      <li class="page-item"><a class="page-link" href="?page=<?= $page-1 ?>">Previous</a></li>
      <?php else: ?>
      <li class="page-item disabled"><span class="page-link">Previous</span></li>
      <?php endif; ?>

      <?php
      // Show max 7 pages around current page for better UX
      $startPage = max(1, $page - 3);
      $endPage = min($totalPages, $page + 3);
      for ($i = $startPage; $i <= $endPage; $i++): ?>
        <li class="page-item <?= $i == $page ? 'active' : '' ?>">
          <a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
        </li>
      <?php endfor; ?>

      <?php if ($page < $totalPages): ?>
      <li class="page-item"><a class="page-link" href="?page=<?= $page+1 ?>">Next</a></li>
      <?php else: ?>
      <li class="page-item disabled"><span class="page-link">Next</span></li>
      <?php endif; ?>
    </ul>
  </nav>

</div>

<?php include "includes/footer.php"; ?>

<!-- Bootstrap JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

<!-- Live search filter -->
<script>
  const searchInput = document.getElementById('searchInput');
  const tableBody = document.getElementById('eaTableBody');

  searchInput.addEventListener('input', function () {
    const filter = this.value.toLowerCase();
    const rows = tableBody.getElementsByTagName('tr');

    for (let row of rows) {
      const cells = row.getElementsByTagName('td');
      let matched = false;

      for (let cell of cells) {
        if (cell.textContent.toLowerCase().includes(filter)) {
          matched = true;
          break;
        }
      }

      row.style.display = matched ? '' : 'none';
    }
  });
</script>

</body>
</html>
