<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
    exit("unauthorized");
}

include "../../php/database.php";
include "../../php/functions.php";

$id = $_GET["id"];

$sql_attendance        = "SELECT * FROM attendance WHERE id=$id";
$result_attendance     = mysqli_query($con, $sql_attendance);
$attendance = mysqli_fetch_assoc($result_attendance);
$year = $attendance["year"];
$month = sprintf('%02d', $attendance["month"]);

$sql = "SELECT * FROM company_data WHERE name = 'Codice azienda'";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
$company_code = $row["value"];

$filename = "ORARIO_" . $company_code . "_" . $month . "" . $year . ".txt";

if ($attendance["sent"] == 1) {
    $attendance_sent = "SI";
    $send_datetime = date("d/m/Y H:i", strtotime($attendance["send_datetime"]));
} else {
    $attendance_sent = "NO";
    $send_datetime = "";
}
?>
<h5><b>Dettaglio cartellino <?php echo $attendance["month"]; ?>/<?php echo $attendance["year"]; ?></b></h5>
<div style="position: absolute; top: 4px; right: 16px;">
    <?php
    if ($attendance["closed"] == 0) {
    ?>
        <button class="btn btn-danger m-1" type="button" onclick="DeleteAttendance(<?php echo $id; ?>)">Elimina</button>
        <button class="btn btn-warning m-1" type="button" onclick="RecalculateAttendance(<?php echo $id; ?>)">Ricalcola</button>
    <?php
    }
    ?>
    <button class="btn btn-success m-1" type="button" onclick="SendAttendance(<?php echo $id; ?>)">Invia</button>
    <a class="btn btn-primary m-1" href="../../../cartellini/<?php echo $filename; ?>" target="_blank" download>Scarica</a>
    <?php
    if ($attendance["closed"] == 0) {
    ?>
        <button class="btn btn-info m-1" type="button" onclick="CloseAttendance(<?php echo $id; ?>)">Chiudi</button>
    <?php
    }
    ?>

</div>
<hr style="margin-top: 0; margin-bottom: 10px">
<div class="row">
    <div class="col-md-3 form-group mb-3">
        <label for="firstName2">Creazione</label>
        <input class="form-control form-control-rounded" id="firstName2" type="text" value="<?php echo date("d/m/Y H:i", strtotime($attendance["creation_datetime"])); ?>" disabled>
    </div>
    <div class="col-md-3 form-group mb-3">
        <label for="firstName2">Stato</label>
        <?php
        if ($attendance["status"] == "Pronto") {
            echo "<input class='form-control form-control-rounded' id='firstName2' type='text' value='" . $attendance["status"] . "' style='color: blue; font-weight:900' disabled>";
        } else if ($attendance["status"] == "Inviato") {
            echo "<input class='form-control form-control-rounded' id='firstName2' type='text' value='" . $attendance["status"] . "' style='color: green; font-weight:900' disabled>";
        } else if ($attendance["status"] == "Da ricalcolare") {
            echo "<input class='form-control form-control-rounded' id='firstName2' type='text' value='" . $attendance["status"] . "' style='color: orange; font-weight:900' disabled>";
        } else if ($attendance["status"] == "Chiuso") {
            echo "<input class='form-control form-control-rounded' id='firstName2' type='text' value='" . $attendance["status"] . "' style='color: black; font-weight:900' disabled>";
        }
        ?>
    </div>
    <div class="col-md-3 form-group mb-3">
        <label for="firstName2">Inviato</label>
        <input class="form-control form-control-rounded" id="firstName2" type="text" value="<?php echo $attendance_sent; ?>" disabled>
    </div>
    <div class="col-md-3 form-group mb-3">
        <label for="firstName2">Data invio</label>
        <input class="form-control form-control-rounded" id="firstName2" type="text" value="<?php echo $send_datetime; ?>" disabled>
    </div>
</div>

<table id="attendance_details_list_table" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Nome</th>
            <th>Cognome</th>
            <th>Ore lavorate</th>
            <th>Ore straordinario</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php
        $sql    = "SELECT * FROM attendance_users WHERE attendance_id=$id GROUP BY user_id";
        $result = mysqli_query($con, $sql);
        while ($row = mysqli_fetch_assoc($result)) {
            $sql_worked        = "SELECT SUM(minutes) AS total FROM attendance_users WHERE attendance_id = $id AND user_id=" . $row["user_id"] . " AND (justification_id = 20 OR justification_id = 13 OR justification_id = 14 OR justification_id = 15)";
            $result_worked     = mysqli_query($con, $sql_worked);
            $worked = mysqli_fetch_assoc($result_worked);
            $worked_time = timeFromMinutes($worked["total"]);

            $sql_overtired        = "SELECT SUM(minutes) AS total FROM attendance_users WHERE attendance_id = $id AND user_id=" . $row["user_id"] . " AND (justification_id = 24 OR justification_id = 25 OR justification_id = 26 OR justification_id = 28)";
            $result_overtired     = mysqli_query($con, $sql_overtired);
            $overtired = mysqli_fetch_assoc($result_overtired);
            $overtired_time = timeFromMinutes($overtired["total"]);

            $sql_user        = "SELECT * FROM users WHERE id=" . $row["user_id"] . "";
            $result_user     = mysqli_query($con, $sql_user);
            $user = mysqli_fetch_assoc($result_user);

            echo "<tr>";
            echo "<td>" . $user["name"] . "</td>";
            echo "<td>" . $user["surname"] . "</td>";
            echo "<td>" . $worked_time . "</td>";
            echo "<td>" . $overtired_time . "</td>";
            echo "<td><i class='fa-solid fa-circle-info' style='float: right; cursor: pointer' onclick='AttendanceUserInfo($id, " . $row["user_id"] . ");'></i></td>";
            echo "</tr>";
        }
        ?>
    </tbody>
</table>