<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
    exit("unauthorized");
}

include "../../php/database.php";
include "../../php/functions.php";


$attendance_id = $_GET["attendance_id"];
$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 class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Dettaglio cartellino - <?php echo $user["name"]; ?> <?php echo $user["surname"]; ?></h5>
    <button class="close" type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true" id="close_attendance_user_info">×</span></button>
</div>
<div class="modal-body">
    <div class="row">
        <div class="col-md-12">
            <h4>Giustificativi totali</h4>
        </div>
        <?php
        $sql = "SELECT * FROM attendance_users WHERE user_id = $user_id AND attendance_id = $attendance_id";
        $result_attendance_users = mysqli_query($con, $sql);
        while ($row = mysqli_fetch_assoc($result_attendance_users)) {
            $sql_justifications        = "SELECT * FROM justifications WHERE id = " . $row["justification_id"] . "";
            $result_justifications     = mysqli_query($con, $sql_justifications);
            $justifications = mysqli_fetch_assoc($result_justifications);

            $justification_time = timeFromMinutes($row["minutes"]);
        ?>
            <div class="col-md-2 form-group mb-3">
                <label for="firstName1"><?php echo $justifications["name"]; ?></label>
                <input class="form-control" id="firstName1" type="text" value="<?php echo $justification_time; ?>" disabled>
            </div>
        <?php
        }
        ?>
    </div>
    <hr>
    <table id="attendance_user_details_table" class="cell-border" style="width:100%">
        <thead>
            <tr>
                <th>Data</th>
                <th>Tipo</th>
                <th>In_1</th>
                <th>Out_1</th>
                <th>In_2</th>
                <th>Out_2</th>
                <th>In_3</th>
                <th>Out_3</th>
                <th>In_4</th>
                <th>Out_4</th>
                <th>Ore teoriche</th>
                <th>Ore lavorate</th>
                <th>Giust_1</th>
                <th>Ore_giust_1</th>
                <th>Giust_2</th>
                <th>Ore_giust_2</th>
                <th>Giust_3</th>
                <th>Ore_giust_3</th>
                <th>Giust_4</th>
                <th>Ore_giust_4</th>
                <th>Giust_5</th>
                <th>Ore_giust_5</th>
                <th>Giust_6</th>
                <th>Ore_giust_6</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $sql    = "SELECT * FROM attendance_rows WHERE attendance_id=$attendance_id AND user_id = $user_id ORDER BY date ASC";
            $result = mysqli_query($con, $sql);
            while ($row = mysqli_fetch_assoc($result)) {
                $theoretical_minutes = timeFromMinutes($row["theoretical_minutes"]);
                $worked_minutes = timeFromMinutes($row["worked_minutes"]);

                if ($row["justification_id_1"] > 0) {
                    $sql_justifications        = "SELECT * FROM justifications WHERE id = " . $row["justification_id_1"] . "";
                    $result_justifications     = mysqli_query($con, $sql_justifications);
                    $justifications = mysqli_fetch_assoc($result_justifications);
                    $justification_id_1 = $justifications["name"];
                    $justification_minutes_1 = timeFromMinutes($row["justification_minutes_1"]);
                } else {
                    $justification_id_1 = "";
                    $justification_minutes_1 = "";
                }

                if ($row["justification_id_2"] > 0) {
                    $sql_justifications        = "SELECT * FROM justifications WHERE id = " . $row["justification_id_2"] . "";
                    $result_justifications     = mysqli_query($con, $sql_justifications);
                    $justifications = mysqli_fetch_assoc($result_justifications);
                    $justification_id_2 = $justifications["name"];
                    $justification_minutes_2 = timeFromMinutes($row["justification_minutes_2"]);
                } else {
                    $justification_id_2 = "";
                    $justification_minutes_2 = "";
                }

                if ($row["justification_id_3"] > 0) {
                    $sql_justifications        = "SELECT * FROM justifications WHERE id = " . $row["justification_id_3"] . "";
                    $result_justifications     = mysqli_query($con, $sql_justifications);
                    $justifications = mysqli_fetch_assoc($result_justifications);
                    $justification_id_3 = $justifications["name"];
                    $justification_minutes_3 = timeFromMinutes($row["justification_minutes_3"]);
                } else {
                    $justification_id_3 = "";
                    $justification_minutes_3 = "";
                }

                if ($row["justification_id_4"] > 0) {
                    $sql_justifications        = "SELECT * FROM justifications WHERE id = " . $row["justification_id_4"] . "";
                    $result_justifications     = mysqli_query($con, $sql_justifications);
                    $justifications = mysqli_fetch_assoc($result_justifications);
                    $justification_id_4 = $justifications["name"];
                    $justification_minutes_4 = timeFromMinutes($row["justification_minutes_4"]);
                } else {
                    $justification_id_4 = "";
                    $justification_minutes_4 = "";
                }

                if ($row["justification_id_5"] > 0) {
                    $sql_justifications        = "SELECT * FROM justifications WHERE id = " . $row["justification_id_5"] . "";
                    $result_justifications     = mysqli_query($con, $sql_justifications);
                    $justifications = mysqli_fetch_assoc($result_justifications);
                    $justification_id_5 = $justifications["name"];
                    $justification_minutes_5 = timeFromMinutes($row["justification_minutes_5"]);
                } else {
                    $justification_id_5 = "";
                    $justification_minutes_5 = "";
                }

                if ($row["justification_id_6"] > 0) {
                    $sql_justifications        = "SELECT * FROM justifications WHERE id = " . $row["justification_id_6"] . "";
                    $result_justifications     = mysqli_query($con, $sql_justifications);
                    $justifications = mysqli_fetch_assoc($result_justifications);
                    $justification_id_6 = $justifications["name"];
                    $justification_minutes_6 = timeFromMinutes($row["justification_minutes_6"]);
                } else {
                    $justification_id_6 = "";
                    $justification_minutes_6 = "";
                }

                echo "<tr>";
                echo "<td>" . date("d/m/Y", strtotime($row["date"])) . "</td>";
                echo "<td>" . $row["day_type"] . "</td>";
                if ($row["entry_time_1"] != "00:00:00") {
                    echo "<td>" . $row["entry_time_1"] . "</td>";
                } else {
                    echo "<td></td>";
                };
                if ($row["exit_time_1"] != "00:00:00") {
                    echo "<td>" . $row["exit_time_1"] . "</td>";
                } else {
                    echo "<td></td>";
                };
                if ($row["entry_time_2"] != "00:00:00") {
                    echo "<td>" . $row["entry_time_2"] . "</td>";
                } else {
                    echo "<td></td>";
                };
                if ($row["exit_time_2"] != "00:00:00") {
                    echo "<td>" . $row["exit_time_2"] . "</td>";
                } else {
                    echo "<td></td>";
                };
                if ($row["entry_time_3"] != "00:00:00") {
                    echo "<td>" . $row["entry_time_3"] . "</td>";
                } else {
                    echo "<td></td>";
                };
                if ($row["exit_time_3"] != "00:00:00") {
                    echo "<td>" . $row["exit_time_3"] . "</td>";
                } else {
                    echo "<td></td>";
                };
                if ($row["entry_time_4"] != "00:00:00") {
                    echo "<td>" . $row["entry_time_4"] . "</td>";
                } else {
                    echo "<td></td>";
                };
                if ($row["exit_time_4"] != "00:00:00") {
                    echo "<td>" . $row["exit_time_4"] . "</td>";
                } else {
                    echo "<td></td>";
                };
                echo "<td>" . $theoretical_minutes . "</td>";
                echo "<td>" . $worked_minutes . "</td>";

                echo "<td>" . $justification_id_1 . "</td>";
                echo "<td>" . $justification_minutes_1 . "</td>";

                echo "<td>" . $justification_id_2 . "</td>";
                echo "<td>" . $justification_minutes_2 . "</td>";

                echo "<td>" . $justification_id_3 . "</td>";
                echo "<td>" . $justification_minutes_3 . "</td>";

                echo "<td>" . $justification_id_4 . "</td>";
                echo "<td>" . $justification_minutes_4 . "</td>";

                echo "<td>" . $justification_id_5 . "</td>";
                echo "<td>" . $justification_minutes_5 . "</td>";

                echo "<td>" . $justification_id_6 . "</td>";
                echo "<td>" . $justification_minutes_6 . "</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
    </table>
</div>

<div class="modal-footer">
    <button class="btn btn-secondary" type="button" data-dismiss="modal">Chiudi</button>
</div>