<?php
include "../database.php";
session_start();
?>

<div data-bs-parent="#tab-group-1" class="" id="new">
    <?php
$sql = "SELECT * FROM transfer WHERE user_id='" . $_SESSION['id'] . "' ORDER BY id DESC";

$result = mysqli_query($con, $sql);
if ($result->num_rows > 0) {
 ?>
    <table id="transfer_list_table" class="display">
        <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;">Inizio/fine</h6>
                </th>
                <th class="border-bottom-0">
                    <h6 class="fw-semibold mb-0" style="font-size: 12px;">Destinazione</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_status    = "SELECT * FROM justification_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">
            <p class='mb-0 fw-normal' style="font-size: 12px;"><?php echo $row["id"]; ?></p>
        </td>
        <td class='border-bottom-0'>
            <h6 class='fw-semibold mb-0' 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"])); ?></h6>
        </td>
        <td class='border-bottom-0'>
            <p class='mb-0 fw-normal' style="font-size: 12px;"><?php echo $row["destination"]; ?></p>
        </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>
        <i class="fa-solid fa-magnifying-glass" onclick="transferDetailUser('<?php echo $row["id"]; ?>')"></i>
        </td>
    </tr>
        <?php
}
 ?>
        </tbody>
    </table>
<?php
} else {
 echo "<div class='content mb-2 mt-4'><h5 style='color:black;text-align: center; font-size: 20px; margin-top: 50px;margin-bottom: 50px;'>Non ci sono richieste di trasferta</h5></div>";
}
?>
</div>

<?php
$con->close();
?>