<?php
	session_start();
	include 'prijava/connect.php';
    setlocale(LC_TIME,"hr_HR");
?>
<!DOCTYPE html>
<head>
    <?php
        define('meta', TRUE);
        include 'inc/head.php';
    ?>
    <script src="js/delete-user.js"></script>
    <title>Korisnici - KrikStudio</title>
</head>
<body>
    <?php
        if(!isset($_SESSION['username']) || $_SESSION['admin'] == 0) {
            echo '<script>';
            echo 'window.location.href="./"';
            echo '</script>';
        }
        define('nav', TRUE);
        include 'inc/nav.php';
    ?>

    <section class="users">
        <div class="wrapper">
            <h2>KORISNICI<a class="add-user" href="registracija"><i data-feather="user-plus"></i>Dodaj korisnika</a></h2>
            
            <?php
                $counter = 1;
                $query = "SELECT * FROM user ORDER BY admin DESC";
                $result = mysqli_query($dbc, $query);
                if($result) {
                    echo "<table>";
                    echo "<thead>";
                    echo "<tr>";
                    echo "<th>#</th>";
                    echo "<th>KORISNIK</th>";
                    echo "<th>KORISNIČKO IME</th>";
                    echo "<th>ULOGA</th>";
                    echo "<th>AKCIJA</th>";
                    echo "</tr>";
                    echo "</thead>";
                    echo "<tbody>";
                    while($row = mysqli_fetch_array($result)) {
                        $id = $row['id'];
                        echo "<tr>";
                        echo "<td>".$counter."</td>";
                        echo "<td>".$row['name']."</td>";
                        echo "<td>".$row['username']."</td>";
                        if($row['admin'] == 1) {
                            echo "<td class='priv-1'>Administrator</td>";
                        } else {
                            echo "<td class='priv-0'>Korisnik</td>";
                        }
                        echo "<td><button onclick='userAdmin($id)'><i data-feather='user'></i></button><button onclick='userDelete($id)'><i data-feather='trash-2'></i></button></td>";
                        echo "</tr>";
                        $counter++;
                    }
                    echo "</tbody>";
                    echo "</table>";
                }

                if ($dbc && isset($_POST['delete'])) {
                    // $id = $_POST['id'];
                    // $query = "DELETE FROM orders WHERE id = $id";
                    // $result = mysqli_query($dbc,$query) or die('Error querying database.');
                    echo '<script>';
                    echo 'alert("Narudžba je otkazana!");';
                    echo '</script>';
                }
            ?>
        </div>
    </section>

    <?php define('footer', TRUE);include 'inc/footer.php'; ?>

    <script>
        feather.replace();
    </script>
</body>