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

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

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 = "";
    if (!empty($_FILES['image']['name'])) { 
        $imageName = basename($_FILES['image']['name']);
        $target = "../uploads/" . $imageName;
        move_uploaded_file($_FILES['image']['tmp_name'], $target);
    }

    $sql = "INSERT INTO testimonials (name, role, image, rating, comments) VALUES ('$name', '$role', '$imageName', '$rating', '$comments')";
    if (mysqli_query($conn, $sql)) {
        header("Location: testimonial.php?success=1");
        exit;
    } else {
        header("Location: testimonial.php?error=" . urlencode(mysqli_error($conn)));
        exit;
    }
}
?>

<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">Add 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" placeholder="Enter Name">
                        </div>
                        <div class="form-group">
                            <label for="role">Role</label>
                            <input type="text" class="form-control" id="role" name="role" placeholder="Enter Role">
                        </div>
                        <div class="form-group">
                            <label for="rating">Rating</label>
                            <input type="text" class="form-control" id="rating" name="rating" placeholder="Enter Rating">
                        </div>
                        <div class="form-group">
                            <div class="form-group">
                                <label for="editor">Comments</label>
                                <div id="editor" style="height: 200px; background:#fff;"></div>
                                <textarea name="comments" id="comments" style="display:none;"></textarea>
                            </div>
                            <div class="form-group">
                                <label for="exampleFormControlFile1">Image</label>
                                <input type="file" class="form-control-file" id="exampleFormControlFile1" name="image">
                            </div>
                        </div>
                        <div class="card-action">
                            <button class="btn btn-success">Submit</button>
                            <button type="button" class="btn btn-danger" onclick="window.location.href='testimonial.php'">Cancel</button>
                        </div>
                    </div>
                </form>
            </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']
            ]
        }
    });

    document.querySelector("form").addEventListener("submit", function() {
        document.querySelector("#comments").value = quill.root.innerHTML;
    });
</script>