<?php

require_once "../../includes/initialize.php";
if (!$session->is_logged_in()) {
    redirect_to("login.php");
}
?>
 
<?php 
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int) $_GET['page'] : 1;
// 2. records per page ($per_page)
$per_page = 10;
// 3. total record count ($total_count)
$total_count = Request::submitted_requested_count_all($_SESSION['user_id']);
//4. to limit pagination number
// 3 means abc[page]efg -> 3 right, 3 left
$stages = 2;
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records
// for this page
$sql = "SELECT * FROM requests ";
$sql .= "WHERE user_id={$_SESSION['user_id']} ORDER BY created DESC ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$pagination->offset()} ";
$requests = Request::find_by_sql($sql);
if (empty($requests)) {
    $session->message("You have not submitted any requests yet.");
    redirect_to('index.php');
}
// Need to add ?page=$page to all links we want to