<?php
require 'auth_check.php';
require '../config/db.php';

$msg = "";
$msgClass = "";

// 1. HANDLE APPROVE
if (isset($_GET['approve'])) {
    $id = $_GET['approve'];
    $pdo->prepare("UPDATE reviews SET status = 1 WHERE id = ?")->execute([$id]);
    header("Location: reviews.php?msg=approved");
    exit();
}

// 2. HANDLE DELETE
if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $pdo->prepare("DELETE FROM reviews WHERE id = ?")->execute([$id]);
    header("Location: reviews.php?msg=deleted");
    exit();
}

// 3. FETCH REVIEWS
// We join with products table to show the product name
$stmt = $pdo->query("
    SELECT r.*, p.name as product_name 
    FROM reviews r 
    LEFT JOIN products p ON r.product_id = p.id 
    ORDER BY r.status ASC, r.created_at DESC
");
$reviews = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Manage Reviews</title>
    <link rel="stylesheet" href="admin_style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        /* Message Box */
        .msg-box { padding: 10px; border-radius: 5px; margin-bottom: 20px; text-align: center; }
        .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        
        /* Status Badges */
        .badge { padding: 5px 10px; border-radius: 4px; font-size: 11px; font-weight: bold; text-transform: uppercase; }
        .badge-pending { background: #f1c40f; color: #fff; }
        .badge-live { background: #27ae60; color: #fff; }
        
        /* Review Text Styling */
        .review-comment { 
            font-style: italic; 
            color: #555; 
            background: #f9f9f9; 
            padding: 8px; 
            border-radius: 4px; 
            border-left: 3px solid #ddd; 
            max-width: 400px;
        }
        .stars { color: #f39c12; letter-spacing: 1px; }
    </style>
</head>
<body>

<?php include 'sidebar.php'; ?>

    <div class="content">
        <h1>Customer Reviews</h1>

        <?php if(isset($_GET['msg'])): ?>
            <div class="msg-box success">
                <?php 
                    if($_GET['msg'] == 'approved') echo "Review approved successfully.";
                    if($_GET['msg'] == 'deleted') echo "Review deleted.";
                ?>
            </div>
        <?php endif; ?>

        <div style="background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 5px rgba(0,0,0,0.05);">
            <table style="width: 100%; border-collapse: collapse;">
                <thead>
                    <tr style="background: #f1f1f1; text-align: left;">
                        <th style="padding: 15px;">Status</th>
                        <th>Product</th>
                        <th>Customer</th>
                        <th>Rating</th>
                        <th>Comment</th>
                        <th>Date</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(count($reviews) > 0): ?>
                        <?php foreach ($reviews as $r): ?>
                        <tr style="border-bottom: 1px solid #eee;">
                            <td style="padding: 15px;">
                                <?php if($r['status'] == 0): ?>
                                    <span class="badge badge-pending">Pending</span>
                                <?php else: ?>
                                    <span class="badge badge-live">Live</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <strong><?php echo htmlspecialchars($r['product_name'] ?? 'Unknown Product'); ?></strong>
                            </td>
                            <td>
                                <?php echo htmlspecialchars($r['user_name']); ?>
                            </td>
                            <td>
                                <span class="stars"><?php echo str_repeat("★", $r['rating']); ?></span> 
                                <span style="color:#ccc; font-size:12px;">(<?php echo $r['rating']; ?>/5)</span>
                            </td>
                            <td>
                                <div class="review-comment">
                                    "<?php echo htmlspecialchars($r['comment']); ?>"
                                </div>
                            </td>
                            <td style="font-size: 13px; color: #777;">
                                <?php echo date('d M Y', strtotime($r['created_at'])); ?>
                            </td>
                            <td>
                                <?php if($r['status'] == 0): ?>
                                    <a href="reviews.php?approve=<?php echo $r['id']; ?>" class="btn btn-primary" style="background: #27ae60; font-size: 12px; padding: 5px 10px; text-decoration:none; color:white; border-radius:3px;">
                                        <i class="fas fa-check"></i> Approve
                                    </a>
                                <?php endif; ?>
                                
                                <a href="reviews.php?delete=<?php echo $r['id']; ?>" class="btn btn-primary" style="background: #c0392b; font-size: 12px; padding: 5px 10px; text-decoration:none; color:white; border-radius:3px; margin-left: 5px;" onclick="return confirm('Delete this review permanently?')">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="7" style="padding: 30px; text-align: center; color: #777;">No reviews found.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

</body>
</html>