<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
    exit("unauthorized");
}

include "../../php/database.php";

$user = $_GET["user"];
$source = $_GET["source"];
$start_date = $_GET["start_date"];
$end_date = $_GET["end_date"];

if ($start_date == "") {
    $start_date = "2020-01-01";
}

if ($end_date == "") {
    $end_date = "2100-01-01";
}

?>

<div class="card-header" style="font-size: 16px;">Esito ricerca <?php echo $source; ?></div>
<div class="card-body">
    <table id="search_result_table" class="display" style="width:100%">
        <thead>
            <tr>
                <?php
                if ($source == "timbrature") {
                    echo "<th>Data</th>";
                    echo "<th>Ora</th>";
                    echo "<th>Tipo</th>";
                    echo "<th>Note</th>";
                    echo "<th>In trasferta</th>";
                    echo "<th>Destinazione trasferta</th>";
                    if ($user == "all") {
                        echo "<th>Utente</th>";
                    }
                } else if ($source == "permessi") {
                    echo "<th>Data inizio</th>";
                    echo "<th>Data fine</th>";
                    echo "<th>Ora inizio</th>";
                    echo "<th>Ora fine</th>";
                    echo "<th>Giustificativo</th>";
                    echo "<th>Giornata</th>";
                    echo "<th>Stato</th>";
                    if ($user == "all") {
                        echo "<th>Utente</th>";
                    }
                } else if ($source == "trasferte") {
                    echo "<th>Data inizio</th>";
                    echo "<th>Data fine</th>";
                    echo "<th>Italia/Estero</th>";
                    echo "<th>Destinazione</th>";
                    echo "<th>Stato</th>";
                    if ($user == "all") {
                        echo "<th>Utente</th>";
                    }
                }
                ?>
            </tr>
        </thead>
        <tbody>
            <?php
            if ($source == "timbrature") {
                if ($user == "all") {
                    $sql    = "SELECT * FROM stamps WHERE date BETWEEN '$start_date' AND '$end_date'";
                } else {
                    $sql    = "SELECT * FROM stamps WHERE user_id = " . $user . " AND date BETWEEN '$start_date' AND '$end_date'";
                }
                $result = mysqli_query($con, $sql);
                while ($row = mysqli_fetch_assoc($result)) {
                    echo "<tr>";
                    echo "<td data-sort='" . $row["date"] . "'>" . date("d/m/Y", strtotime($row["date"])) . "</td>";
                    echo "<td>" . date("H:i", strtotime($row["time"])) . "</td>";
                    echo "<td>" . $row["type"] . "</td>";
                    echo "<td>" . $row["note"] . "</td>";
                    if ($row["in_transfer"] == 0) {
                        echo "<td>NO</td>";
                    } else {
                        echo  "<td>SI</td>";
                    }
                    echo "<td>" . $row["transfer_location"] . "</td>";
                    if ($user == "all") {
                        $sql_user = "SELECT * FROM users WHERE id = " . $row["user_id"] . "";
                        $result_user = mysqli_query($con, $sql_user);
                        $user_data = mysqli_fetch_assoc($result_user);

                        echo "<td>" . $user_data["name"] . " " . $user_data["surname"] . "</td>";
                    }
                    echo "</tr>";
                }
            } else if ($source == "permessi") {
                if ($user == "all") {
                    $sql    = "SELECT * FROM permissions WHERE start_date BETWEEN '$start_date' AND '$end_date'";
                } else {
                    $sql    = "SELECT * FROM permissions WHERE user_id = " . $user . " AND start_date BETWEEN '$start_date' AND '$end_date'";
                }
                $result = mysqli_query($con, $sql);
                while ($row = mysqli_fetch_assoc($result)) {

                    $sql_justifications        = "SELECT * FROM justifications WHERE id=" . $row["justification_id"] . "";
                    $result_justifications     = mysqli_query($con, $sql_justifications);
                    $justification = mysqli_fetch_assoc($result_justifications);

                    $sql_status        = "SELECT * FROM requests_status WHERE id=" . $row["status_id"] . "";
                    $result_status     = mysqli_query($con, $sql_status);
                    $justification_status = mysqli_fetch_assoc($result_status);

                    echo "<tr>";
                    echo "<td data-sort='" . $row["start_date"] . "'>" . date("d/m/Y", strtotime($row["start_date"])) . "</td>";
                    echo "<td data-sort='" . $row["end_date"] . "'>" . date("d/m/Y", strtotime($row["end_date"])) . "</td>";
                    echo "<td>" . date("H:i", strtotime($row["start_time"])) . "</td>";
                    echo "<td>" . date("H:i", strtotime($row["end_time"])) . "</td>";
                    echo "<td>" . $justification["name"] . "</td>";
                    if ($row["full_day"] == 1) {
                        echo "<td>SI</td>";
                    } else {
                        echo "<td>NO</td>";
                    }
                    if ($row["status_id"] == 1) {
                        echo "<td style='color: orange; font-weight:900'>" . $justification_status["name"] . "</td>";
                    } else if ($row["status_id"] == 2) {
                        echo "<td style='color: green; font-weight:900'>" . $justification_status["name"] . "</td>";
                    } else if ($row["status_id"] == 3) {
                        echo "<td style='color: red; font-weight:900'>" . $justification_status["name"] . "</td>";
                    }
                    if ($user == "all") {
                        $sql_user = "SELECT * FROM users WHERE id = " . $row["user_id"] . "";
                        $result_user = mysqli_query($con, $sql_user);
                        $user_data = mysqli_fetch_assoc($result_user);

                        echo "<td>" . $user_data["name"] . " " . $user_data["surname"] . "</td>";
                    }
                    echo "</tr>";
                }
            } else if ($source == "trasferte") {
                if ($user == "all") {
                    $sql    = "SELECT * FROM transfers WHERE start_date BETWEEN '$start_date' AND '$end_date'";
                } else {
                    $sql    = "SELECT * FROM transfers WHERE user_id = " . $user . " AND start_date BETWEEN '$start_date' AND '$end_date'";
                }
                $result = mysqli_query($con, $sql);
                while ($row = mysqli_fetch_assoc($result)) {

                    $sql_status        = "SELECT * FROM requests_status WHERE id=" . $row["status_id"] . "";
                    $result_status     = mysqli_query($con, $sql_status);
                    $status = mysqli_fetch_assoc($result_status);

                    echo "<tr>";
                    echo "<td data-sort='" . $row["start_date"] . "'>" . date("d/m/Y", strtotime($row["start_date"])) . "</td>";
                    echo "<td data-sort='" . $row["end_date"] . "'>" . date("d/m/Y", strtotime($row["end_date"])) . "</td>";
                    echo "<td>" . $row["location"] . "</td>";
                    echo "<td>" . $row["destination"] . "</td>";
                    if ($row["status_id"] == 1) {
                        echo "<td style='color: orange; font-weight:900'>" . $status["name"] . "</td>";
                    } else if ($row["status_id"] == 2) {
                        echo "<td style='color: green; font-weight:900'>" . $status["name"] . "</td>";
                    } else if ($row["status_id"] == 3) {
                        echo "<td style='color: red; font-weight:900'>" . $status["name"] . "</td>";
                    }
                    if ($user == "all") {
                        $sql_user = "SELECT * FROM users WHERE id = " . $row["user_id"] . "";
                        $result_user = mysqli_query($con, $sql_user);
                        $user_data = mysqli_fetch_assoc($result_user);

                        echo "<td>" . $user_data["name"] . " " . $user_data["surname"] . "</td>";
                    }
                    echo "</tr>";
                }
            }
            ?>
        </tbody>
    </table>
</div>