include 'config.php';
include 'lib/pagination.php';
//classes loading end
session_start();
if (isset($_SESSION['userMerlaTrav'])) {
    //les sources
    $projetManager = new ProjetManager($pdo);
    $appartementManager = new AppartementManager($pdo);
    $appartement = "";
    $idAppartement = 0;
    $idProjet = $_GET['idProjet'];
    $projet = $projetManager->getProjetById($idProjet);
    if (isset($_GET['idAppartement']) and ($_GET['idAppartement'] > 0 and $_GET['idAppartement'] <= $appartementManager->getLastId())) {
        $idAppartement = htmlentities($_GET['idAppartement']);
        $appartement = $appartementManager->getAppartementById($idAppartement);
        $piecesManager = new AppartementPiecesManager($pdo);
        $piecesNumber = $piecesManager->getPiecesAppartementNumberByIdAppartement($idAppartement);
        if ($piecesNumber != 0) {
            $piecesAppartement = $piecesManager->getPiecesAppartementByIdAppartement($idAppartement);
        }
    }
    ?>
<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9"> <![endif]-->
<!--[if !IE]><!--> <html lang="en"> <!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
	<meta charset="utf-8" />
	<title>ImmoERP - Management Application</title>
	<meta content="width=device-width, initial-scale=1.0" name="viewport" />
        include '../model/' . $myClass . '.php';
    } elseif (file_exists('../controller/' . $myClass . '.php')) {
        include '../controller/' . $myClass . '.php';
    }
}
spl_autoload_register("classLoad");
include '../config.php';
include '../lib/image-processing.php';
//classes loading end
session_start();
//post input processing
$action = htmlentities($_POST['action']);
//This var contains result message of CRUD action
$actionMessage = "";
$typeMessage = "";
$appartementPiecesManager = new AppartementPiecesManager($pdo);
$idProjet = htmlentities($_POST['idProjet']);
$idAppartement = htmlentities($_POST['idAppartement']);
if ($action == "add") {
    if (file_exists($_FILES['url']['tmp_name']) || is_uploaded_file($_FILES['url']['tmp_name'])) {
        $url = imageProcessing($_FILES['url'], '/pieces/pieces_appartement/');
        $nom = htmlentities($_POST['nom']);
        $createdBy = $_SESSION['userMerlaTrav']->login();
        $created = date('Y-m-d h:i:s');
        //create object
        $appartementPiece = new AppartementPieces(array('nom' => $nom, 'url' => $url, 'idAppartement' => $idAppartement, 'created' => $created, 'createdBy' => $createdBy));
        //add it to db
        $appartementPiecesManager->add($appartementPiece);
        $actionMessage = "Opération Valide : Pièce Appartement Ajouté(e) avec succès.";
        $typeMessage = "success";
    } else {