<?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']);

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

<h4 class="page-title">Image</h4>
    <div class="row">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <div class="card-title">Add Image</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">
                            <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"; ?>
