<?php
include "../database.php";
session_start();
?>

<div data-bs-parent="#tab-group-1" class="" id="new">
    <?php
$sql = "SELECT * FROM permissions WHERE user_id='" . $_SESSION['id'] . "' ORDER BY id DESC";

$result = mysqli_query($con, $sql);
if ($result->num_rows > 0) {
 ?>
    <table id="permission_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;">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 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">
            <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>
        <i class="fa-solid fa-magnifying-glass" onclick="permissionDetailUser('<?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 permesso</h5></div>";
}
?>
</div>

<?php
$con->close();
?>