<?php
session_start();
include "php/database.php";

if (!isset($_SESSION['loggedin'])) {
	header('Location: login.php');
	exit;
}

$index_page = false;
$page_title = "Elenco permessi";
$prev_page = "openHome()";

include "public/header.php";
?>

    
<div class="card card-style">
    <div class="content pb-3" style="text-align: center">
        <?php
        $sql = "SELECT * FROM permissions WHERE user_id='" . $_SESSION['id'] . "' ORDER BY id DESC";

        $result = mysqli_query($con, $sql);
            ?>
            <table id="permission_list_table" class="display" style="width: 100%">
                <thead class="text-dark fs-4" style="background-color: lightgray;">
                    <tr style="font-size: 12px;">
                        <th class="border-bottom-0" style="display: none">
                            <h6 class="fw-semibold mb-0" style="font-size: 12px;">ID</h6>
                        </th>
                        <th class="border-bottom-0">
                            <h6 class="fw-semibold mb-0" style="font-size: 12px;">Tipo</h6>
                        </th>
                        <th class="border-bottom-0">
                            <h6 class="fw-semibold mb-0" style="font-size: 12px;">Data I/F</h6>
                        </th>
                        <th class="border-bottom-0">
                            <h6 class="fw-semibold mb-0" style="font-size: 12px;">Ora I/F</h6>
                        </th>
                        <th class="border-bottom-0">
                            <h6 class="fw-semibold mb-0" style="font-size: 12px;">Stato</h6>
                        </th>
                        <th>

                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = mysqli_fetch_assoc($result)) { 
                        $sql_justification    = "SELECT * FROM justifications WHERE id='" . $row["justification_id"] . "'";
                        $result_justification = mysqli_query($con, $sql_justification);
                        $stmt_justification   = mysqli_fetch_assoc($result_justification);
                      
                        $sql_status    = "SELECT * FROM requests_status WHERE id='" . $row["status_id"] . "'";
                        $result_status = mysqli_query($con, $sql_status);
                        $stmt_status   = mysqli_fetch_assoc($result_status);
                        ?>
                        <tr>
                            <td class='border-bottom-0' style="display: none">
                                <h6 class='fw-semibold mb-0' style="font-size: 12px;"><?php echo $row["id"]; ?></h6>
                            </td>
                            <td class='border-bottom-0'>
                                <h6 class='fw-semibold mb-0' style="font-size: 12px;"><?php echo $stmt_justification["name"]; ?></h6>
                            </td>
                            <td class='border-bottom-0'>
                                <p class='mb-0 fw-normal' style="font-size: 12px;"><?php echo date("d/m/Y", strtotime($row["start_date"])); ?><br><?php echo date("d/m/Y", strtotime($row["end_date"])); ?></p>
                            </td>
                            <td class='border-bottom-0'>
                                <?php
                                if ($row["full_day"] == 0) {
                                    ?><p class='mb-0 fw-normal' style="font-size: 12px;"><?php echo date("H:i", strtotime($row["start_time"])); ?> - <?php echo date("H:i", strtotime($row["end_time"])); ?></p><?php
                                } else {
                                    ?><p class='mb-0 fw-normal' style="font-size: 12px;">Giornata intera</p><?php
                                }
                                ?>
                            </td>
                            <td class='border-bottom-0'>
                                <?php
                                if ($row["status_id"] == 1) {
                                    echo "<p class='mb-0 fw-normal' style='font-size: 12px;'><span style='color: orange'><b>" . $stmt_status["name"] . "</b></span></p>";
                                } else if ($row["status_id"] == 2) {
                                    echo "<p class='mb-0 fw-normal' style='font-size: 12px;'><span style='color: green'><b>" . $stmt_status["name"] . "</b></span></p>";
                                } else if ($row["status_id"] == 3) {
                                    echo "<p class='mb-0 fw-normal' style='font-size: 12px;'><span style='color: red'><b>" . $stmt_status["name"] . "</b></span></p>";
                                }
                                ?>
                            </td>
                            <td>
                                <a href="permission_details.php?id=<?php echo $row["id"]; ?>" style="color:black"><i class="fa-solid fa-magnifying-glass"></i></a>
                            </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        <?php

        $con->close();
        ?>
    </div>
</div>

<script>
    $(document).ready( function () {
        new DataTable('#permission_list_table', {
            paging: false,
            scrollCollapse: true,
            scrollY: '70vh',
            order: [[2, 'desc']],
            layout: {
                topStart: {
                    buttons: [
                        {
                            text: 'Nuovo',
                            action: function (e, dt, node, config) {
                                openNewPermission();
                            }
                        },
                        {
                            extend: 'print',
                            split: ['excel','pdf']
                        }
                    ]
                }
            },
            language: {
                emptyTable: "Nessun dato presente nella tabella",
                search: "Cerca:",
                infoEmpty: "Visualizzati da 0 a 0 di 0 risultati",
                info: "Visualizzati da _START_ a _END_ di _TOTAL_ risultati",
                buttons: {
                    print: "Stampa",
                    colvis: "Visualizza",
                    colvisRestore: "Ripristina visualizzazione"
                }
            }
        });
    });
</script>

<?php include "public/footer.php" ?>