<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include 'includes/messages_head.php' ?>
<body>
<?php include 'includes/header.php';?>
<!-- LOGO HEADER END-->
<?php include 'includes/navbar.php';?>

<!-- MENU SECTION END-->

<?php
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
    exit();
}

$currPage = isset($_GET["page"]) ? $_GET["page"] : 1;

$offset = ($currPage-1)*5;
$size = 5;

$sql = "SELECT * from messages ORDER BY id DESC limit $offset,$size ";
$result = $mysqli->query($sql);


$countPages = array_values(mysqli_fetch_array($mysqli->query("SELECT COUNT(*) as total FROM messages")))[0];
$countPages = floor(($countPages-1)/5+1);

//set messages as read
$mysqli->query("update messages set isRead = 1")

?>

<div class="tab-content">
    <div class="container">
        <div class="row" style="margin-top:15px">
            <table class="table table-filter">
                <tbody>
                    <?php while ($row = $result->fetch_assoc()) { ?>
                        <tr data-status="pagado">
                            <td>
                                <div class="media">
                                    <div class="media-body">
                                        <span class="media-meta pull-right"><?= $row["date"] ?></span>
                                        <h4 class="title">
                                            <?= $row["fname"] ?> <?= $row["lname"] ?>
                                            <span class="pull-right pagado">(<?= $row["email"] ?>)</span>
                                        </h4>
                                        <p class="summary"><?= $row["message"] ?></p>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>

            <div class="text-center">
                <ul class="pagination">
                    <?php for($page=1;$page<=$countPages;$page++){ ?>
                    <li  <?php if($page==$currPage) { ?>
                        class="active"
                    <?php } ?>
                    ><a href="messages.php?page=<?=$page?>">
                            <?=$page?>
                            <?php } ?>

                        </a>
                    </li>
                </ul>
            </div>

        </div>
    </div>


</div>
<?php include 'includes/footer-scripts.php';?>
</body>
</html>