<?php
$page_title = 'Manage Reviews - Admin';

if (!Admin::isLoggedIn()) {
    Utils::redirect('/routex/admin/login');
}

require_once __DIR__ . '/../../includes/trackheader.php';

$review_model = new Review($pdo);
$page = $_GET['page'] ?? 1;
$page = max(1, intval($page));
$limit = 20;
$offset = ($page - 1) * $limit;

$reviews = $review_model->getAll($limit, $offset);
$total = $review_model->getCount();
$total_pages = ceil($total / $limit);
?>

<section class="min-h-screen bg-gray-50">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Header -->
        <div class="flex flex-col md:flex-row justify-between items-center mb-8 gap-4">
            <div>
                <h1 class="text-4xl font-bold text-gray-900">Manage Reviews</h1>
                <p class="text-gray-600 mt-2">Total: <?php echo $total; ?> reviews</p>
            </div>
            <div class="flex gap-3">
                <a href="/routex/admin/dashboard" class="px-6 py-3 bg-white text-gray-700 border-2 border-gray-300 rounded-lg font-semibold hover:bg-gray-50 transition-all duration-300">
                    <i class="fas fa-arrow-left mr-2"></i>Dashboard
                </a>
                <a href="/routex/admin/review-create" class="btn-primary">
                    <i class="fas fa-plus mr-2"></i>Create Review
                </a>
            </div>
        </div>

        <!-- Reviews Table -->
        <div class="bg-white rounded-xl shadow-md overflow-hidden">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-100 border-b">
                        <tr>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Reviewer</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Rating</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Review</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Featured</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Created</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($reviews as $review): ?>
                        <tr class="border-b hover:bg-gray-50">
                            <td class="px-6 py-4">
                                <div>
                                    <p class="font-semibold text-gray-900"><?php echo htmlspecialchars($review['reviewer_name']); ?></p>
                                    <p class="text-sm text-gray-600"><?php echo htmlspecialchars($review['reviewer_email'] ?? ''); ?></p>
                                </div>
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex">
                                    <?php for ($i = 0; $i < $review['rating']; $i++): ?>
                                    <i class="fas fa-star text-yellow-400"></i>
                                    <?php endfor; ?>
                                </div>
                            </td>
                            <td class="px-6 py-4 text-sm text-gray-600">
                                <?php echo htmlspecialchars(substr($review['review_text'], 0, 50)) . '...'; ?>
                            </td>
                            <td class="px-6 py-4">
                                <?php if ($review['is_featured']): ?>
                                <span class="bg-green-100 text-green-800 px-3 py-1 rounded-full text-sm font-semibold">Yes</span>
                                <?php else: ?>
                                <span class="bg-gray-100 text-gray-800 px-3 py-1 rounded-full text-sm font-semibold">No</span>
                                <?php endif; ?>
                            </td>
                            <td class="px-6 py-4 text-sm text-gray-600">
                                <?php echo Utils::formatDate($review['created_at']); ?>
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex space-x-2">
                                    <a href="/routex/admin/review-edit?id=<?php echo $review['id']; ?>" class="text-purple-600 hover:text-purple-800 font-semibold">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            <?php if ($total_pages > 1): ?>
            <div class="px-6 py-4 border-t flex justify-center space-x-2">
                <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                <a href="/routex/admin/reviews?page=<?php echo $i; ?>" class="<?php echo $i === $page ? 'btn-primary' : 'px-4 py-2 border-2 border-gray-300 rounded-lg hover:border-purple-600'; ?>">
                    <?php echo $i; ?>
                </a>
                <?php endfor; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
</section>

<footer class="bg-white border-t mt-20 no-print">
  <div class="max-w-7xl mx-auto px-4 py-6 text-center text-sm text-slate-500">
    © 2026 RoutexShipping. All rights reserved.
  </div>
</footer>
