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

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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = mysqli_real_escape_string($conn, $_POST['title']);
    $description = mysqli_real_escape_string($conn, $_POST['description']);
    $category = mysqli_real_escape_string($conn, $_POST['category']);

    $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $title), '-'));

    $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 course (title, slug, image, description, category) VALUES ('$title', '$slug', '$imageName', '$description', '$category')";
    if (mysqli_query($conn, $sql)) {
        header("Location: course.php?success=1");
        exit;
    } else {
        header("Location: course.php?error=" . urlencode(mysqli_error($conn)));
        exit;
    }
}
?>

<h4 class="page-title">Course</h4>
    <div class="row">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <div class="card-title">Add Course</div>
                </div>
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="card-body">
                        <div class="form-group">
                            <label for="title">Title</label>
                            <input type="text" class="form-control" id="title" name="title" placeholder="Enter Title">
                        </div>
                        <div class="form-group">
                            <label for="category">Category</label>
                            <input type="text" class="form-control" id="category" name="category" placeholder="Enter Category">
                        </div>
                        <div class="form-group">
                            <div class="form-group">
                                <label for="editor">Description</label>
                                <div id="editor" style="height: 200px; background:#fff;"></div>
                                <textarea name="description" id="description" 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='course.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("#description").value = quill.root.innerHTML;
    });
</script>