<?php include "../parts/header.php"; ?>
<?php include "../parts/sidepanel.php"; ?>

<?php
include("../opendb.php");

$id = intval($_GET['id']); 

$sql = "SELECT * FROM testimonials WHERE id=$id";
$result = mysqli_query($conn, $sql);
$data = mysqli_fetch_assoc($result);

$baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .
           "://{$_SERVER['HTTP_HOST']}/psn_mentor_new/admin/"; 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $role = mysqli_real_escape_string($conn, $_POST['role']);
    $rating = mysqli_real_escape_string($conn, $_POST['rating']);
    $comments = mysqli_real_escape_string($conn, $_POST['comments']);

    $imageName = $data['image'];
    if (!empty($_FILES['image']['name'])) {
        $imageName = basename($_FILES['image']['name']);
        $target = "../uploads/" . $imageName;
        move_uploaded_file($_FILES['image']['tmp_name'], $target);
    }

    $updateSql = "UPDATE testimonials SET name='$name', role='$role', rating='$rating', image='$imageName', comments='$comments' WHERE id=$id";

    if (mysqli_query($conn, $updateSql)) {
        echo "<script>alert('Testimonial updated successfully'); window.location.href='testimonial.php';</script>";
    } else {
        echo "<div class='alert alert-danger'>Error: " . mysqli_error($conn) . "</div>";
    }
}
?>

<h4 class="page-title">Testimonial</h4>
<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <div class="card-title">Edit Testimonial</div>
            </div>
            <form action="" method="POST" enctype="multipart/form-data">
                <div class="card-body">
                    <div class="form-group">
                        <label for="name">Name</label>
                        <input type="text" class="form-control" id="name" name="name" value="<?= htmlspecialchars($data['name']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="role">Role</label>
                        <input type="text" class="form-control" id="role" name="role" value="<?= htmlspecialchars($data['role']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="rating">Rating</label>
                        <input type="text" class="form-control" id="rating" name="rating" value="<?= htmlspecialchars($data['rating']); ?>">
                    </div>
                    
                    <div class="form-group">
                        <label for="editor">Comments</label>
                        <div id="editor" style="height:200px; background:#fff;">
                            <?= $data['comments']; ?>
                        </div>
                        <textarea name="comments" id="comments" style="display:none;"></textarea>
                    </div>
                    <div class="form-group">
                        <label>Current Image</label><br>
                        <?php if (!empty($data['image'])): ?>
                            <img src="<?= $baseUrl . 'uploads/' . htmlspecialchars($data['image']); ?>" alt="Course Image" width="120">
                        <?php else: ?>
                            <span class="text-muted">No image</span>
                        <?php endif; ?>
                    </div>
                    <div class="form-group">
                        <label for="exampleFormControlFile1">Change Image</label>
                        <input type="file" class="form-control-file" id="exampleFormControlFile1" name="image">
                    </div>
                    <div class="card-action">
                        <button type="submit" class="btn btn-success">Update</button>
                        <a href="testimonial.php" class="btn btn-danger">Cancel</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<?php include "../parts/footer.php"; ?>


<script src="https://cdn.jsdelivr.net/npm/quill@2.0.3/dist/quill.js"></script>

<script>
    const quill = new Quill('#editor', {
        theme: 'snow',
        modules: {
            toolbar: [
                [{ 'list': 'ordered'}, { 'list': 'bullet' }],
                ['bold', 'italic', 'underline']
            ]
        }
    });

    // On form submit, copy Quill HTML into hidden textarea
    document.querySelector("form").addEventListener("submit", function() {
        document.querySelector("#comments").value = quill.root.innerHTML;
    });
</script>