<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

$db = (new Database())->connect();

$search = trim($_GET['search'] ?? '');
$page = max(1, (int)($_GET['page'] ?? 1));

$allowedLimits = [10, 25, 50, 100];
$limit = (int)($_GET['limit'] ?? 10);

if (!in_array($limit, $allowedLimits)) {
    $limit = 10;
}

$offset = ($page - 1) * $limit;

/*
    COUNT QUERY
*/
$countSql = "SELECT COUNT(*) FROM categories";
$countParams = [];

if (!empty($search)) {
    $countSql .= " WHERE name LIKE ?";
    $countParams[] = "%$search%";
}

$stmt = $db->prepare($countSql);
$stmt->execute($countParams);
$totalCategories = $stmt->fetchColumn();

$totalPages = ceil($totalCategories / $limit);

/*
    MAIN QUERY
*/
$sql = "SELECT * FROM categories";
$params = [];

if (!empty($search)) {
    $sql .= " WHERE name LIKE ?";
    $params[] = "%$search%";
}

$sql .= " ORDER BY name ASC LIMIT $limit OFFSET $offset";

$stmt = $db->prepare($sql);
$stmt->execute($params);

$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="card shadow-sm">
    <div class="card-body table-responsive">

        <table class="table table-bordered table-hover align-middle">

            <thead class="table-dark">
                <tr>
                    <th width="100">ID</th>
                    <th>Category Name</th>
                    <th width="180">Actions</th>
                </tr>
            </thead>

            <tbody>

            <?php if (empty($categories)): ?>
                <tr>
                    <td colspan="3" class="text-center">
                        No categories found.
                    </td>
                </tr>
            <?php endif; ?>

            <?php foreach ($categories as $category): ?>
                <tr>
                    <td><?= $category['id'] ?></td>

                    <td>
                        <?= htmlspecialchars($category['name']) ?>
                    </td>

                    <td>
                        <a
                            href="edit.php?id=<?= $category['id'] ?>"
                            class="btn btn-warning btn-sm"
                        >
                            Edit
                        </a>

                        <a
                            href="delete.php?id=<?= $category['id'] ?>"
                            class="btn btn-danger btn-sm"
                            onclick="return confirm('Delete this category?')"
                        >
                            Delete
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>

            </tbody>

        </table>

        <!-- PAGINATION -->
        <?php if ($totalPages > 1): ?>
            <nav>
                <ul class="pagination justify-content-center">

                    <?php if ($page > 1): ?>
                        <li class="page-item">
                            <button
                                class="page-link"
                                onclick="loadCategories(<?= $page - 1 ?>)"
                            >
                                Previous
                            </button>
                        </li>
                    <?php endif; ?>

                    <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                        <li class="page-item <?= $page == $i ? 'active' : '' ?>">
                            <button
                                class="page-link"
                                onclick="loadCategories(<?= $i ?>)"
                            >
                                <?= $i ?>
                            </button>
                        </li>
                    <?php endfor; ?>

                    <?php if ($page < $totalPages): ?>
                        <li class="page-item">
                            <button
                                class="page-link"
                                onclick="loadCategories(<?= $page + 1 ?>)"
                            >
                                Next
                            </button>
                        </li>
                    <?php endif; ?>

                </ul>
            </nav>
        <?php endif; ?>

    </div>
</div>