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

include "../../php/database.php"; 
?>

<div class="card-title mb-3">Approvazioni e notifiche</div>

<ul class="nav nav-tabs" id="myIconTab" role="tablist">
    <li class="nav-item"><a class="nav-link active" id="home-icon-tab" data-toggle="tab" href="#homeIcon" role="tab" aria-controls="homeIcon" aria-selected="true"><i class="nav-icon i-Stamp"></i> Timbrature</a></li>
    <li class="nav-item"><a class="nav-link" id="profile-icon-tab" data-toggle="tab" href="#profileIcon" role="tab" aria-controls="profileIcon" aria-selected="false"><i class="nav-icon i-File-Clipboard-File--Text"></i> Permessi</a></li>
    <li class="nav-item"><a class="nav-link" id="contact-icon-tab" data-toggle="tab" href="#contactIcon" role="tab" aria-controls="contactIcon" aria-selected="false"><i class="nav-icon i-Truck mr-1"></i> Trasferte</a></li>
</ul>
<div class="tab-content" id="myIconTabContent">
    <div class="tab-pane fade show active" id="homeIcon" role="tabpanel" aria-labelledby="home-icon-tab">
        <table class="table table-striped" id="stamp_settings">
            <thead>
                <tr>
                    <th style="display: none;">ID</th>
                    <th>Nome</th>
                    <th>Cognome</th>
                    <th>Email</th>
                    <th>Approvazioni</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $index = 0;
                $sql    = "SELECT * FROM users WHERE role = 'Admin'";
                $result = mysqli_query($con, $sql);
                while ($row = mysqli_fetch_assoc($result)) {
                    $index++;
                    echo "<tr>";
                        echo "<td style='display:none'>". $row["id"] ."</td>";
                        echo "<td>". $row["name"] ."</td>";
                        echo "<td>". $row["surname"] ."</td>";
                        echo "<td>". $row["email"] ."</td>";
                        if($row["stamps_approver"] == 1) {
                            echo "<td><input style='margin: auto' class='form-check-input' id='stamps_approve_".$index."' type='checkbox' checked='checked'></td>";
                        } else {
                            echo "<td><input style='margin: auto' class='form-check-input' id='stamps_approve_".$index."' type='checkbox'></td>";
                        }
                    echo "</tr>";
                }
                ?>
            </tbody>
        </table>
        <button class="btn btn-primary" type="submit" onclick="saveStampSettings()" id="save_stamps_button">Salva</button>
        <div class="spinner spinner-primary mr-3" style="display: none;float:left" id="stamps_settings_loader"></div>
    </div>
    <div class="tab-pane fade" id="profileIcon" role="tabpanel" aria-labelledby="profile-icon-tab">
        <table class="table table-striped" id="permissions_settings">
            <thead>
                <tr>
                    <th style="display: none;">ID</th>
                    <th>Nome</th>
                    <th>Cognome</th>
                    <th>Email</th>
                    <th>Approvazioni</th>
                    <th>Notifiche</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $index = 0;
                $sql    = "SELECT * FROM users WHERE role = 'Admin'";
                $result = mysqli_query($con, $sql);
                while ($row = mysqli_fetch_assoc($result)) {
                    $index++;
                    echo "<tr>";
                        echo "<td style='display:none'>". $row["id"] ."</td>";
                        echo "<td>". $row["name"] ."</td>";
                        echo "<td>". $row["surname"] ."</td>";
                        echo "<td>". $row["email"] ."</td>";
                        if($row["permissions_approver"] == 1) {
                            echo "<td><input style='margin: auto' class='form-check-input' id='permissions_approve_".$index."' type='checkbox' checked='checked'></td>";
                        } else {
                            echo "<td><input style='margin: auto' class='form-check-input' id='permissions_approve_".$index."' type='checkbox'></td>";
                        }
                        if($row["permissions_notifies"] == 1) {
                            echo "<td><input style='margin: auto' class='form-check-input' id='permissions_notify_".$index."' type='checkbox' checked='checked'></td>";
                        } else {
                            echo "<td><input style='margin: auto' class='form-check-input' id='permissions_notify_".$index."' type='checkbox'></td>";
                        }
                    echo "</tr>";
                }
                ?>
            </tbody>
        </table>
        <button class="btn btn-primary" type="submit" onclick="savePermissionsSettings()" id="save_permissions_button">Salva</button>
        <div class="spinner spinner-primary mr-3" style="display: none;float:left" id="permissions_settings_loader"></div>
    </div>
    <div class="tab-pane fade" id="contactIcon" role="tabpanel" aria-labelledby="contact-icon-tab">
        <table class="table table-striped" id="transfers_settings">
            <thead>
                <tr>
                    <th style="display: none;">ID</th>
                    <th>Nome</th>
                    <th>Cognome</th>
                    <th>Email</th>
                    <th>Approvazioni</th>
                    <th>Notifiche</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $index = 0;
                $sql    = "SELECT * FROM users WHERE role = 'Admin'";
                $result = mysqli_query($con, $sql);
                while ($row = mysqli_fetch_assoc($result)) {
                    $index++;
                    echo "<tr>";
                        echo "<td style='display:none'>". $row["id"] ."</td>";
                        echo "<td>". $row["name"] ."</td>";
                        echo "<td>". $row["surname"] ."</td>";
                        echo "<td>". $row["email"] ."</td>";
                        if($row["transfers_approver"] == 1) {
                            echo "<td><input style='margin: auto' class='form-check-input' id='transfers_approve_".$index."' type='checkbox' checked='checked'></td>";
                        } else {
                            echo "<td><input style='margin: auto' class='form-check-input' id='transfers_approve_".$index."' type='checkbox'></td>";
                        }
                        if($row["transfers_notifies"] == 1) {
                            echo "<td><input style='margin: auto' class='form-check-input' id='transfers_notify_".$index."' type='checkbox' checked='checked'></td>";
                        } else {
                            echo "<td><input style='margin: auto' class='form-check-input' id='transfers_notify_".$index."' type='checkbox'></td>";
                        }
                    echo "</tr>";
                }
                ?>
            </tbody>
        </table>
        <button class="btn btn-primary" type="submit" onclick="saveTransfersSettings()" id="save_transfers_button">Salva</button>
        <div class="spinner spinner-primary mr-3" style="display: none;float:left" id="transfers_settings_loader"></div>
    </div>
</div>