<?php
include "php/database.php";

$index_page = false;
$page_title = "Elenco timbrature";
$prev_page = "openHome()";

session_start();

include "public/header.php";
?>


<div class="card card-style">
    <div class="content pb-3" style="text-align: center">
        <?php
        $sql = "SELECT * FROM stamps WHERE user_id='" . $_SESSION['id'] . "' ORDER BY id DESC";

        $result = mysqli_query($con, $sql);
        ?>
        <table id="stamp_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</h6>
                    </th>
                    <th class="border-bottom-0">
                        <h6 class="fw-semibold mb-0" style="font-size: 12px;">Ora</h6>
                    </th>
                    <th style="width: 10px;">

                    </th>
                </tr>
            </thead>
            <tbody>
                <?php while ($row = mysqli_fetch_assoc($result)) { ?>
                    <?php if ($row["delete_request"] == true && $row["delete_request_rejected"] == false) {
                        echo "<tr style='background-color: #ffcdd2'>";
                    } else {
                        echo "<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 $row["type"]; ?></h6>
                    </td>
                    <td class='border-bottom-0'>
                        <p class='mb-0 fw-normal' style="font-size: 12px;white-space: nowrap;"><?php echo date("Y-m-d", strtotime($row["date"])); ?></p>
                    </td>
                    <td class='border-bottom-0'>
                        <p class='mb-0 fw-normal' style="font-size: 12px;"><?php echo date("H:i", strtotime($row["time"])); ?></p>
                    </td>
                    <td style="width: 10px;">
                        <a href="stamp_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>

<?php
include "public/modals/user_calendar.php";
?>

<script>
    $(document).ready(function() {
        new DataTable('#stamp_list_table', {
            paging: false,
            scrollCollapse: true,
            scrollY: '70vh',
            order: [
                [2, 'desc']
            ],
            layout: {
                topStart: {
                    buttons: [{
                            text: 'Nuova',
                            action: function(e, dt, node, config) {
                                openNewStamp();
                            }
                        },
                        {
                            text: 'Calendario',
                            action: function(e, dt, node, config) {
                                UserCalendarPage();
                            }
                        },
                        {
                            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"
                }
            }
        });
    });

    function UserCalendarPage() {
        location.href = "stamps_calendar.php";
    }
</script>

<?php include "public/footer.php" ?>