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

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

?>
<div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Permessi da approvare</h5>
    <button class="close" type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true" id="close_permissions_to_approve">×</span></button>
</div>
<div class="modal-body">
    <table id="permissions_to_approve_table" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Tipo</th>
                <th>Utente</th>
                <th>Giustificativo</th>
                <th>Data inizio</th>
                <th>Data fine</th>
                <th>Ora inizio</th>
                <th>Ora fine</th>
                <th>Giornata</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $sql    = "SELECT * FROM permissions WHERE status_id = 1 OR request_delete = 1 OR request_update = 1";
            $result = mysqli_query($con, $sql);
            while ($row = mysqli_fetch_assoc($result)) {
                $sql_users        = "SELECT * FROM users WHERE id='" . $row["user_id"] . "'";
                $result_users     = mysqli_query($con, $sql_users);
                $user = mysqli_fetch_assoc($result_users);

                $sql_justifications        = "SELECT * FROM justifications WHERE id=" . $row["justification_id"] . "";
                $result_justifications     = mysqli_query($con, $sql_justifications);
                $justification = mysqli_fetch_assoc($result_justifications);

                echo "<tr>";
                if ($row["status_id"] == 1) {
                    echo "<td>Nuovo</td>";
                } else if ($row["request_delete"] == 1) {
                    echo "<td>Eliminazione</td>";
                } else if ($row["request_update"] == 1) {
                    echo "<td>Modifica</td>";
                }
                echo "<td>" . $user["name"] . " " . $user["surname"] . "</td>";
                echo "<td>" . $justification["name"] . "</td>";
                echo "<td data-sort='" . $row["start_date"] . "'>" . date("d/m/Y", strtotime($row["start_date"])) . "</td>";
                echo "<td data-sort='" . $row["end_date"] . "'>" . date("d/m/Y", strtotime($row["end_date"])) . "</td>";
                echo "<td>" . date("H:i", strtotime($row["start_time"])) . "</td>";
                echo "<td>" . date("H:i", strtotime($row["end_time"])) . "</td>";
                if ($row["full_day"] == 1) {
                    echo "<td>SI</td>";
                } else {
                    echo "<td>NO</td>";
                }
                echo "<td>";
                echo "<a onclick='approvePermission(" . $row["user_id"] . "," . $row["id"] . ")' style='cursor: pointer; margin-right:10px'><i class='fa-solid fa-thumbs-up' style='color: green'></i></a>";
                echo "<a onclick='rejectPermission(" . $row["user_id"] . "," . $row["id"] . ")' style='cursor: pointer; margin-right:10px'><i class='fa-solid fa-thumbs-down' style='color: red'></i></a>";
                echo "<a onclick='editPermission(" . $row["user_id"] . "," . $row["id"] . ")' style='cursor: pointer; margin-right:10px'><i class='fa-solid fa-magnifying-glass'></i></a>";
                echo "</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
    </table>
</div>
<div class="modal-footer">
    <button class="btn btn-secondary" type="button" data-dismiss="modal">Chiudi</button>
</div>