<?php
include_once './modeles/modeleFilms.php';

$currentPage = filter_input(
    INPUT_GET,
    'page',
    FILTER_VALIDATE_INT,
    ['options' => ['min_range' => 1]]
);

if ($currentPage === null || $currentPage === false) {
    $currentPage = 1;
}

$perPage = 10;
$data = getFilms($currentPage, $perPage);
$films = $data['films'];
$errorMessage = $data['error'];
$totalFilms = $data['total'];
$totalPages = max(1, (int) ceil($totalFilms / $perPage));

if ($currentPage > $totalPages && $totalFilms > 0) {
    $currentPage = $totalPages;
    $data = getFilms($currentPage, $perPage);
    $films = $data['films'];
    $errorMessage = $data['error'];
    $totalFilms = $data['total'];
}

//
// on "charge" la vue
//

include_once './vues/header.php';
include_once './vues/vueFilms.php';
include_once './vues/footer.php';

