<?php

include_once "movies_bd.php";
include_once "users_bd.php";
session_start();
$userBD = new UsersBD();
$idUser = $_SESSION['utilisateur']->getId();
if (isset($idUser) && $userBD->getUser($idUser) != null) {
    $movieBD = new MoviesBD();
    if (isset($_POST['add'])) {
        $_SESSION["ajout"] = false;
        print_r($_POST);
        if (isset($_POST)) {
            $movie = new Movie(-1, $idUser, $_POST['titre'], $_POST['resume'], $_POST['synopsis'], $_POST['realisateur'], $_POST['annee'], $_FILES['affiche']['name'], $_POST['categorie']);
            $_SESSION["ajout"] = $movieBD->addMovie($movie, $_FILES['affiche'], $idUser);
        }
        header('Location: ../add.php');
    } else {
        if (isset($_POST['edit'])) {
            $_SESSION["edit"] = false;
            if (isset($_POST)) {
                $movie = $movieBD->getMovie($_POST['id'], $idUser);
                $movie->setTitle(isset($_POST['titre']) ? $_POST['titre'] : $movie->getTitle());
                $movie->setDescriptionShort(isset($_POST['resume']) ? $_POST['resume'] : $movie->getDescriptionShort());
                $movie->setDescriptionLong(isset($_POST['synopsis']) ? $_POST['synopsis'] : $movie->getDescriptionLong());
                $movie->setDirector(isset($_POST['realisateur']) ? $_POST['realisateur'] : $movie->getDirector());
                $movie->setYear(isset($_POST['annee']) ? $_POST['annee'] : $movie->getYear());
                $movie->setImage(empty($_FILES['affiche']['name']) ? $movie->getImage() : $_FILES['affiche']['name']);
                $movie->setCategory(isset($_POST['categorie']) ? $_POST['categorie'] : $movie->getCategory());
                print_r($movie);
                $_SESSION["edit"] = $movieBD->updateMovie($movie, empty($_FILES['affiche']['nom']) ? null : $_FILES['affiche']);