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

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

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

$sql = "SELECT * FROM course 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") {
    $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 = $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 course SET title='$title', slug='$slug', description='$description', image='$imageName', category='$category' WHERE id=$id";

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

<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">Edit 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" value="<?= htmlspecialchars($data['title']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="category">Category</label>
                        <input type="text" class="form-control" id="category" name="category" value="<?= htmlspecialchars($data['category']); ?>">
                    </div>
                    
                    <div class="form-group">
                        <label for="editor">Description</label>
                        <div id="editor" style="height:200px; background:#fff;">
                            <?= $data['description']; ?>
                        </div>
                        <textarea name="description" id="description" 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="course.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("#description").value = quill.root.innerHTML;
    });
</script>