<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
    exit("unauthorized");
}

include "../../php/database.php";

$user_id = $_GET["user_id"];

$sql_user        = "SELECT * FROM users WHERE id=" . $user_id . "";
$result_user     = mysqli_query($con, $sql_user);
$user = mysqli_fetch_assoc($result_user);
?>
<div style="position: absolute; width: 100%">
    <h4 style="text-align: center;">
        <?php
        if ($_SESSION["role"] == "Admin") {
            echo "Elenco trasferte " . $user["name"] . " " . $user["surname"] . "";
        }
        ?>
    </h4>
</div>

<table id="transfers_list_table" class="display" style="width:100%">
    <thead>
        <tr>
            <!-- <th>ID</th> -->
            <th>Data inizio</th>
            <th>Data fine</th>
            <th>Italia/Estero</th>
            <th>Destinazione</th>
            <th>Stato</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php
        $sql    = "SELECT * FROM transfers WHERE user_id = " . $user_id . "";
        $result = mysqli_query($con, $sql);
        while ($row = mysqli_fetch_assoc($result)) {
            $sql_status        = "SELECT * FROM requests_status WHERE id=" . $row["status_id"] . "";
            $result_status     = mysqli_query($con, $sql_status);
            $status = mysqli_fetch_assoc($result_status);

            echo "<tr>";
            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>" . $row["location"] . "</td>";
            echo "<td>" . $row["destination"] . "</td>";
            if ($row["status_id"] == 1) {
                echo "<td style='color: orange; font-weight:900'>" . $status["name"] . "</td>";
            } else if ($row["status_id"] == 2) {
                echo "<td style='color: green; font-weight:900'>" . $status["name"] . "</td>";
            } else if ($row["status_id"] == 3) {
                echo "<td style='color: red; font-weight:900'>" . $status["name"] . "</td>";
            }
            echo "<td>";
            echo "<a onclick='editTransfer(" . $row["user_id"] . "," . $row["id"] . ")' style='cursor: pointer; margin-right:10px'><i class='fa-solid fa-magnifying-glass'></i></a>";
            if ($_SESSION["role"] == "Admin") {
                if ($row["status_id"] == 1) {
                    echo "<i class='fa-solid fa-triangle-exclamation' style='color: red'></i>";
                }
            }
            echo "</td>";
            echo "</tr>";
        }
        ?>
    </tbody>
</table>