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

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

$month = $_GET["month"];
$year = $_GET["year"];
$start_date_check = $_GET["start_date_check"];
$end_date_check = $_GET["end_date_check"];

$sql_problems    = "SELECT * FROM problems WHERE run_user_id = " . $_SESSION["id"] . " ORDER BY date DESC";
$result_preblems = mysqli_query($con, $sql_problems);
?>
<div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Nuovo cartellino <?php echo $month; ?>/<?php echo $year; ?></h5>
    <button class="close" type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true" id="close_new_attendance">×</span></button>
</div>
<div class="modal-body">
    <?php
    if ($result_preblems->num_rows > 0) {
    ?>
        <p style='color: red'>Correggere tutti i problemi per poter generare il cartellino</p>
        <table id="problems_list_table" class="display" style="width:100%">
            <thead>
                <tr>
                    <th>Dipendente</th>
                    <th>Tipo</th>
                    <th>Descrizione</th>
                    <th>Suggerimento</th>
                    <th>Data</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php
                $working_minutes = 0;
                $consecutive_days = 0;
                $current_date = "";
                $current_time = "";
                $index = 0;
                $stamps_not_match = false;

                $month = date("m");
                $year = date("Y");
                $date = mktime(0, 0, 0, $month, 1, $year);

                while ($problems = mysqli_fetch_assoc($result_preblems)) {
                    if ($problems["source_id"] > 0 && $problems["source_type"] == "Timbratura") {
                        $sql_stamps = "SELECT * FROM stamps WHERE id = " . $problems["source_id"] . "";
                        $result_stamps = mysqli_query($con, $sql_stamps);
                        $stamps = mysqli_fetch_assoc($result_stamps);
                    }

                    $sql_users = "SELECT * FROM users WHERE id = " . $problems["user_id"] . "";
                    $result_users = mysqli_query($con, $sql_users);
                    $users = mysqli_fetch_assoc($result_users);

                    $user_name = $users["name"] . " " . $users["surname"];

                    echo "<tr>";
                    echo "<td>" . $user_name . "</td>";
                    echo "<td>" . $problems["source_type"] . "</td>";
                    echo "<td>" . $problems["description"] . "</td>";
                    echo "<td>" . $problems["suggestion"] . "</td>";
                    echo "<td>" . date("d/m/Y", strtotime($problems["date"])) . "</td>";
                    echo "<td>";
                    if ($problems["source_type"] == "Timbratura" && $problems["source_id"] > 0) {
                        echo "<a href='stamps.php?stamp_id=" . $problems["source_id"] . "&&user_id=" . $problems["user_id"] . "' target='_blank' style='cursor: pointer; margin-right:10px'><i class='fa-solid fa-magnifying-glass' aria-hidden='true'></i></a>";
                    } else if ($problems["source_type"] == "Timbratura" && $problems["source_id"] == 0) {
                        echo "<a href='stamps.php?calendar_day=" . $problems["date"] . "&&user_id=" . $problems["user_id"] . "' target='_blank' style='cursor: pointer; margin-right:10px'><i class='fa-solid fa-magnifying-glass' aria-hidden='true'></i></a>";
                    } else if ($problems["source_type"] == "Permesso" && $problems["source_id"] > 0) {
                        echo "<a href='permissions.php?permission_id=" . $problems["source_id"] . "&&user_id=" . $problems["user_id"] . "' target='_blank' style='cursor: pointer; margin-right:10px'><i class='fa-solid fa-magnifying-glass' aria-hidden='true'></i></a>";
                    } else if ($problems["source_type"] == "Permesso" && $problems["source_id"] == 0) {
                        echo "<a href='permissions.php?calendar_day=" . $problems["date"] . "&&user_id=" . $problems["user_id"] . "' target='_blank' style='cursor: pointer; margin-right:10px'><i class='fa-solid fa-magnifying-glass' aria-hidden='true'></i></a>";
                    }
                    echo "</td>";
                    echo "</tr>";
                }
                ?>
            </tbody>
        </table>
    <?php
    } else {
    ?>
        <p style='color: black'>Nessun problema rilevato</p>
    <?php
    }
    ?>
</div>

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