<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) { header("Location: login.php"); exit(); }
include '../db_connect.php';

// Handle Add Review
if (isset($_POST['add_review'])) {
    $name = $_POST['name'];
    $text = $_POST['review_text'];
    $rating = $_POST['rating'];
    
    $stmt = $conn->prepare("INSERT INTO reviews (patient_name, review_text, rating) VALUES (?, ?, ?)");
    $stmt->bind_param("ssi", $name, $text, $rating);
    $stmt->execute();
    header("Location: reviews.php");
}

// Handle Delete
if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    $conn->query("DELETE FROM reviews WHERE id=$id");
    header("Location: reviews.php");
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Manage Reviews</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-50 p-6">
    <nav class="mb-6 flex gap-4">
        <a href="dashboard.php" class="text-blue-600 font-bold">← Back to Dashboard</a>
    </nav>

    <div class="grid md:grid-cols-3 gap-8">
        <div class="bg-white p-6 rounded shadow-md h-fit">
            <h2 class="text-xl font-bold mb-4">Add Patient Review</h2>
            <form method="POST" class="space-y-4">
                <input type="text" name="name" placeholder="Patient Name" required class="w-full border p-2 rounded">
                <textarea name="review_text" rows="4" placeholder="What did they say?" required class="w-full border p-2 rounded"></textarea>
                <div>
                    <label class="block text-sm text-gray-600">Rating (Stars)</label>
                    <select name="rating" class="w-full border p-2 rounded">
                        <option value="5">⭐⭐⭐⭐⭐ (5 Stars)</option>
                        <option value="4">⭐⭐⭐⭐ (4 Stars)</option>
                        <option value="3">⭐⭐⭐ (3 Stars)</option>
                    </select>
                </div>
                <button type="submit" name="add_review" class="w-full bg-yellow-500 text-white py-2 rounded hover:bg-yellow-600">Add Review</button>
            </form>
        </div>

        <div class="md:col-span-2 grid gap-4">
            <?php
            $result = $conn->query("SELECT * FROM reviews ORDER BY id DESC");
            while($row = $result->fetch_assoc()) {
                echo '<div class="bg-white p-4 rounded shadow border-l-4 border-yellow-400 relative">';
                echo '<h3 class="font-bold text-gray-800">' . $row['patient_name'] . '</h3>';
                echo '<p class="text-yellow-500 text-sm mb-2">';
                for($i=0; $i<$row['rating']; $i++) { echo '★'; }
                echo '</p>';
                echo '<p class="text-gray-600 italic">"' . $row['review_text'] . '"</p>';
                echo '<a href="reviews.php?delete=' . $row['id'] . '" class="absolute top-2 right-2 text-red-500 text-xs hover:underline" onclick="return confirm(\'Delete this review?\')">Delete</a>';
                echo '</div>';
            }
            ?>
        </div>
    </div>
</body>
</html>