<?php
include 'auth.php';
require_once '../config/db.php';

$id = (int)$_GET['id'];
$testimonial = $conn->query("SELECT * FROM testimonials WHERE id = $id")->fetch_assoc();
if (!$testimonial) {
    header("Location: testimonials.php");
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $client_name = sanitize($conn, $_POST['client_name']);
    $message = sanitize($conn, $_POST['message']);
    $rating = isset($_POST['rating']) ? (int)$_POST['rating'] : 5;

    $sql = "UPDATE testimonials SET client_name = '$client_name', message = '$message', rating = $rating WHERE id = $id";
    if ($conn->query($sql)) {
        $_SESSION['success_msg'] = "Review updated successfully!";
        header("Location: testimonials.php");
        exit();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Review | Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700;800&display=swap" rel="stylesheet">
    <style>
        * { font-family: 'Inter', sans-serif; }
        body { background: #f0f2f5; padding: 30px; }
        .form-card { background: #fff; border-radius: 20px; box-shadow: 0 4px 20px rgba(0,0,0,0.06); padding: 40px; max-width: 700px; margin: 0 auto; }
        .form-label { font-weight: 600; font-size: 14px; color: #333; }
        .form-control, .form-select { border-radius: 12px; padding: 12px 16px; border: 1px solid #e0e0e0; }
        .form-control:focus, .form-select:focus { border-color: #ff6900; box-shadow: 0 0 0 3px rgba(255,105,0,0.1); }
        .btn-back { text-decoration: none; color: #666; font-size: 14px; }
        .btn-back:hover { color: #ff6900; }
        .star-rating { font-size: 28px; cursor: pointer; color: #ddd; transition: 0.2s; }
        .star-rating.active { color: #ffc107; }
    </style>
</head>
<body>
    <div class="container-fluid" style="max-width: 800px;">
        <div class="mb-4">
            <a href="testimonials.php" class="btn-back"><i class="fas fa-arrow-left me-2"></i>Back to Reviews</a>
            <h2 class="fw-bold mt-2">Edit Review</h2>
        </div>

        <div class="form-card">
            <form method="POST">
                <div class="mb-3">
                    <label class="form-label">Client Name</label>
                    <input type="text" name="client_name" class="form-control" value="<?= htmlspecialchars($testimonial['client_name']) ?>" required>
                </div>

                <div class="mb-3">
                    <label class="form-label">Review Message</label>
                    <textarea name="message" class="form-control" rows="4" required><?= htmlspecialchars($testimonial['message']) ?></textarea>
                </div>

                <div class="mb-4">
                    <label class="form-label">Rating</label>
                    <div class="star-rating-input">
                        <input type="hidden" name="rating" id="rating-value" value="<?= $testimonial['rating'] ?>">
                        <?php for ($i = 1; $i <= 5; $i++): ?>
                        <span class="star-rating <?= $i <= $testimonial['rating'] ? 'active' : '' ?>" data-value="<?= $i ?>">&#9733;</span>
                        <?php endfor; ?>
                        <span class="ms-2 text-muted small" id="rating-label"><?= $testimonial['rating'] ?> / 5</span>
                    </div>
                </div>

                <div class="d-flex gap-2">
                    <button type="submit" class="btn btn-primary rounded-pill px-5 shadow-sm">
                        <i class="fas fa-save me-2"></i> Update Review
                    </button>
                    <a href="testimonials.php" class="btn btn-light rounded-pill px-4">Cancel</a>
                </div>
            </form>
        </div>
    </div>

    <script>
    document.querySelectorAll('.star-rating').forEach(star => {
        star.addEventListener('click', function() {
            let val = parseInt(this.dataset.value);
            document.getElementById('rating-value').value = val;
            document.getElementById('rating-label').textContent = val + ' / 5';
            document.querySelectorAll('.star-rating').forEach(s => {
                s.classList.toggle('active', parseInt(s.dataset.value) <= val);
            });
        });
    });
    </script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
