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

// Pagination config
$limit = 10; // reviews per page
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Count total reviews
$total_stmt = $pdo->query("SELECT COUNT(*) FROM reviews");
$total_reviews = $total_stmt->fetchColumn();
$total_pages = ceil($total_reviews / $limit);

// Fetch reviews for current page with related user and company info
$stmt = $pdo->prepare("
    SELECT r.id, r.comment, r.rating, u.username, c.name AS company_name 
    FROM reviews r
    JOIN users u ON r.user_id = u.id
    JOIN companies c ON r.company_id = c.id
    ORDER BY r.id DESC
    LIMIT :limit OFFSET :offset
");
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$reviews = $stmt->fetchAll();
?>

<?php include 'menu.php'; ?>
<!DOCTYPE html>
<html>
<head>
    <title>Manage Reviews</title>
    <link rel="stylesheet" href="../style.css">
    <style>
        table { border-collapse: collapse; width: 100%; }
        th, td { padding: 8px; border: 1px solid #ccc; text-align: left; }
        th { background-color: #f2f2f2; }
        .delete-link { color: red; text-decoration: none; }
        .pagination a {
            margin: 0 5px; text-decoration: none; padding: 5px 10px; border: 1px solid #ccc; border-radius: 4px;
        }
        .pagination a.active {
            background: #007BFF; color: white; border-color: #007BFF;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>All Reviews</h2>
    <table>
        <tr>
            <th>ID</th>
            <th>User</th>
            <th>Company</th>
            <th>Rating</th>
            <th>Comment</th>
            <th>Action</th>
        </tr>
        <?php foreach($reviews as $r): ?>
            <tr>
                <td><?= $r['id'] ?></td>
                <td><?= htmlspecialchars($r['username']) ?></td>
                <td><?= htmlspecialchars($r['company_name']) ?></td>
                <td><?= $r['rating'] ?></td>
                <td><?= htmlspecialchars(substr($r['comment'], 0, 50)) ?><?= strlen($r['comment']) > 50 ? '...' : '' ?></td>
                <td>
                    <a href="edit_review.php?id=<?= $r['id'] ?>">✏ Edit</a> /
                    <a href="delete_review.php?id=<?= $r['id'] ?>" onclick="return confirm('Delete review?');">🗑 Delete</a>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>

    <!-- Pagination links -->
    <div class="pagination" style="margin-top:15px;">
        <?php if($total_pages > 1): ?>
            <?php for($i = 1; $i <= $total_pages; $i++): ?>
                <a href="?page=<?= $i ?>" class="<?= $i == $page ? 'active' : '' ?>"><?= $i ?></a>
            <?php endfor; ?>
        <?php endif; ?>
    </div>

    <p><a href="dashboard.php">← Back to Dashboard</a></p>
</div>
</body>
</html>
