<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
    exit("unauthorized");
}

include "../../php/database.php";

$user_id = $_GET["user_id"];

$sql_problems_range    = "SELECT * FROM problems_date_range WHERE user_id = 0";
$result_problems_range = mysqli_query($con, $sql_problems_range);
$last_update_problems = mysqli_fetch_assoc($result_problems_range);

$sql_problems_range_user    = "SELECT * FROM problems_date_range WHERE user_id = " . $_SESSION["id"] . "";
$result_problems_range_user = mysqli_query($con, $sql_problems_range_user);
$last_update_problems_user = mysqli_fetch_assoc($result_problems_range_user);

if ($last_update_problems["datetime"] >= $last_update_problems_user["datetime"]) {
    $sql_problems    = "SELECT * FROM problems WHERE user_id = " . $user_id . " AND run_user_id = 0 ORDER BY date DESC";
} else {
    $sql_problems    = "SELECT * FROM problems WHERE user_id = " . $user_id . " AND run_user_id = " . $_SESSION["id"] . " ORDER BY date DESC";
}

$sql_problems    = "SELECT * FROM problems WHERE user_id = " . $user_id . " AND 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">Elenco problemi rilevati</h5>
    <button class="close" type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true" id="close_problems_list">×</span></button>
</div>
<div class="modal-body">
    <?php
    if ($result_preblems->num_rows > 0) {
    ?>
        <table id="problems_list_table" class="display" style="width:100%">
            <thead>
                <tr>
                    <th>Tipo</th>
                    <th>Descrizione</th>
                    <th>Suggerimento</th>
                    <th>Data</th>
                    <th>Risoluzione</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php

                while ($problems = mysqli_fetch_assoc($result_preblems)) {
                    echo "<tr>";
                    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><a href='#' onclick='resolveProblem(" . $problems["id"] . ", $user_id)' style='color: blue'><u>Risolto</u></a></td>";
                    echo "<td>";
                    echo "<a href='situation.php?event_id=" . $problems["source_id"] . "&&event_type=" . $problems["source_type"] . "&&user_id=" . $problems["user_id"] . "&&calendar_view=" . $problems["calendar_type"] . "&&day=" . $problems["date"] . "' 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>