<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
    exit("unauthorized");
}

include "../../php/database.php";

?>
<table id="attendance_list_table" class="display" style="width:100%;">
    <thead>
        <tr>
            <th>Anno</th>
            <th>Mese</th>
            <th>Stato</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php
        $sql    = "SELECT * FROM attendance ORDER BY year, month DESC";
        $result = mysqli_query($con, $sql);
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>";
            echo "<td>" . $row["year"] . "</td>";
            if ($row["month"] == 1) {
                echo "<td>Gennaio</td>";
            } else if ($row["month"] == 2) {
                echo "<td>Febbraio</td>";
            } else if ($row["month"] == 3) {
                echo "<td>Marzo</td>";
            } else if ($row["month"] == 4) {
                echo "<td>Aprile</td>";
            } else if ($row["month"] == 5) {
                echo "<td>Maggio</td>";
            } else if ($row["month"] == 6) {
                echo "<td>Giugno</td>";
            } else if ($row["month"] == 7) {
                echo "<td>Luglio</td>";
            } else if ($row["month"] == 8) {
                echo "<td>Agosto</td>";
            } else if ($row["month"] == 9) {
                echo "<td>Settembre</td>";
            } else if ($row["month"] == 10) {
                echo "<td>Ottobre</td>";
            } else if ($row["month"] == 11) {
                echo "<td>Novembre</td>";
            } else if ($row["month"] == 12) {
                echo "<td>Dicembre</td>";
            }

            if ($row["status"] == "Pronto") {
                echo "<td><span class='badge badge-pill badge-info p-2 m-1'>Pronto</span></td>";
            } else if ($row["status"] == "Inviato") {
                echo "<td><span class='badge badge-pill badge-success p-2 m-1'>Inviato</span></td>";
            } else if ($row["status"] == "Da ricalcolare") {
                echo "<td><span class='badge badge-pill badge-warning p-2 m-1'>Da ricalcolare</span></td>";
            } else if ($row["status"] == "Chiuso") {
                echo "<td><span class='badge badge-pill badge-primary p-2 m-1'>Chiuso</span></td>";
            }
            echo "<td><i class='fa-solid fa-magnifying-glass' style='float: right; cursor: pointer' onclick='AttendanceDetails(" . $row["id"] . ");'></i></td>";
            echo "</tr>";
        }
        ?>
    </tbody>
</table>