<?php

require_once "includes/database.php";
require_once "includes/DAO/MoviesDAO.php";
$title = isset($_POST["title"]) ? htmlspecialchars($_POST["title"]) : null;
$shortDesc = isset($_POST["shortDescription"]) ? htmlspecialchars($_POST["shortDescription"]) : null;
$longDesc = isset($_POST["longDescription"]) ? htmlspecialchars($_POST["longDescription"]) : null;
$director = isset($_POST["director"]) ? htmlspecialchars($_POST["director"]) : null;
$year = isset($_POST["year"]) ? htmlspecialchars($_POST["year"]) : null;
$image = isset($_FILES["image"]["name"]) ? $_FILES["image"]["name"] : null;
if (isset($title)) {
    move_uploaded_file($_FILES["image"]["tmp_name"], './images/' . $image);
    $moviesDAO = new MoviesDAO(Database::getInstance());
    $movie = new Movie(-1, $title, $shortDesc, $longDesc, $director, $year, $image);
    $result = $moviesDAO->insert($movie);
}
?>


<!DOCTYPE html>
<html>

<?php 
include_once "includes/HTML/head.php";
?>
<body>
<div class="container">
    <?php 
include_once "includes/HTML/header.php";
?>
// Récupération de toutes les données saisies
$mov_title = isset($_POST["mov_title"]) ? escapeCharacters($_POST["mov_title"]) : null;
$mov_description_short = isset($_POST["mov_description_short"]) ? escapeCharacters($_POST["mov_description_short"]) : null;
$mov_description_long = isset($_POST["mov_description_long"]) ? escapeCharacters($_POST["mov_description_long"]) : null;
$mov_director = isset($_POST["mov_director"]) ? escapeCharacters($_POST["mov_director"]) : null;
$mov_year = isset($_POST["mov_year"]) ? escapeCharacters($_POST["mov_year"]) : null;
$mov_image = isset($_FILES["mov_image"]["name"]) ? $_FILES["mov_image"]["name"] : null;
// Si au moins une information est présente, alors on insère un nouveau film
if (isset($mov_title)) {
    // On déplace l'image sur le serveur
    $mov_image_on_server = "./images/" . $mov_image;
    move_uploaded_file($_FILES["mov_image"]["tmp_name"], $mov_image_on_server);
    // On insère le film
    $moviesDAO = new MoviesDAO(MaBD::getInstance());
    $movie = new Movie(DAO::UNKNOWN_ID, $mov_title, $mov_description_short, $mov_description_long, $mov_director, $mov_year, $mov_image_on_server);
    $res = $moviesDAO->insert($movie);
}
?>

<!DOCTYPE HTML>
<html>

<?php 
include_once "includes/Head.php";
?>

<body>
    <?php 
include_once "includes/NavBar.php";
?>