<?php
session_start();
include_once("../config/db.php");

// Security: Ensure user is an admin before performing any action.
if(!isset($_SESSION['admin'])){
    header("Location: login.php");
    exit();
}

include 'admin_header.php';

$result = mysqli_query($conn, "SELECT * FROM reviews ORDER BY id DESC");
?>
<div class="page-header">
    <h1>Manage Reviews</h1>
    <a href="add_review.php" class="btn">Add New Review</a>
</div>

<div class="content-wrapper">
    <?php if(mysqli_num_rows($result) > 0): ?>
        <table>
            <thead>
                <tr>
                    <th style="width: 100px;">Image</th>
                    <th>Name</th>
                    <th>Place</th>
                    <th>Review</th>
                    <th style="width: 150px; text-align: center;">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php while($row = mysqli_fetch_assoc($result)): ?>
                    <tr>
                        <td>
                            <?php if(!empty($row['image'])): ?>
                                <img src="uploads/<?php echo htmlspecialchars($row['image']); ?>" alt="<?php echo htmlspecialchars($row['name']); ?>">
                            <?php else: ?>
                                <img src="https://via.placeholder.com/80x60?text=No+Image" alt="No Image">
                            <?php endif; ?>
                        </td>
                        <td><?php echo htmlspecialchars($row['name']); ?></td>
                        <td><?php echo htmlspecialchars($row['place']); ?></td>
                        <td><?php echo htmlspecialchars(substr($row['review'], 0, 100)); ?>...</td>
                        <td style="text-align: center;">
                            <a href="delete_review.php?id=<?php echo $row['id']; ?>" class="delete" onclick="return confirm('Are you sure you want to delete this review?');" style="text-decoration: none;">
                                <i class="fas fa-trash"></i> Delete
                            </a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    <?php else: ?>
        <div style="text-align: center; color: var(--text-secondary); padding: 2rem 0;">
            <i class="fas fa-star" style="font-size: 48px; margin-bottom: 15px; opacity: 0.3;"></i>
            <p>No reviews found. <a href="add_review.php">Add one now</a>.</p>
        </div>
    <?php endif; ?>
</div>
<?php include 'admin_footer.php'; ?>