<?php
	session_start();
	include 'prijava/connect.php';
    setlocale(LC_TIME,"hr_HR");
?>
<!DOCTYPE html>
<head>
    <?php
        define('meta', TRUE);
        include 'inc/head.php';
    ?>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.2/css/jquery.dataTables.css">
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.13.2/js/jquery.dataTables.js"></script>
    <title>Sva povijest - 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="all-history">
        <div class="wrapper">
            <a class="back-btn" href="./narudzbe"><i data-feather="arrow-left"></i> Povratak</a>
            <h2>SVE NARUDŽBE</h2>
            <!--<input id="myInput" type="text" placeholder="Search..">-->
            <?php
                if ($dbc) {
                    $query = "SELECT * FROM orders ORDER BY date DESC, id DESC";
                    $counter = 1;
                    $result = mysqli_query($dbc, $query);
                    if($result) {
                        echo "<table id='history-table'>";
                        echo "<thead>";
                        echo "<tr>";
                        echo "<th>#</th>";
                        echo "<th>KORISNIK</th>";
                        echo "<th>DATUM</th>";
                        echo "<th>MENU</th>";
                        echo "<th>DODANO</th>";
                        echo "<th>STANJE</th>";
                        echo "</tr>";
                        echo "</thead>";
                        echo "<tbody>";
                        while($row = mysqli_fetch_array($result)) {
                            $check = new DateTime(date("d.m.Y. H:i:s"));
                            $check = $check->format('d.m.Y. H:i:s');

                            $date = date_create($row['date']);
                            $date2 = date_create($row['added']);
                            
                            $date = date_format($date,"d.m.Y.");
                            $date2 = date_format($date2,"d.m.Y. H:i:s");
                            
                            echo "<tr>";
                            echo "<td>".$counter."</td>";
                            echo "<td>".$row['user']."</td>";
                            echo "<td>".$date."</td>";
                            echo "<td>".$row['menu']."</td>";
                            if($check == $date2) {
                                echo "<td style='font-style:italic'></td>";
                            } else {
                                echo "<td>".$date2."</td>";
                            }
                            
                            if($row['completed'] == 1) {
                                echo "<td><span class='history-done'>Završeno</span></td>";
                            } else {
                                echo "<td><span class='history-ordered'>Naručeno</span></td>";
                            }
                            echo "</tr>";
                            $counter++;
                        }
                        echo "</tbody>";
                        echo "</table>";
                    }
                }
            ?>
        </div>
    </section>

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

    <script>
        feather.replace();
        $(document).ready( function () {
            $('#history-table').DataTable({
                ordering: false,
                "language": {
                    "lengthMenu": "Prikaži _MENU_ rezultata po stranici",
                    "zeroRecords": "Nema rezultata",
                    "info": "Stranica _PAGE_ od _PAGES_",
                    "search": "",
                    "searchPlaceholder": "Pretraga",
                    "infoEmpty": "Nema rezultata",
                    "infoFiltered": "(filtered from _MAX_ total records)",
                    "paginate": {
                        "previous": "Prethodna",
                        "next": "Sljedeća",
                    }
                },
            });
        });
        $(document).ready(function(){
            $("#myInput").on("keyup", function() {
                var value = $(this).val().toLowerCase();
                $("#history-table tbody tr").filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
                });
            });
        });
    </script>
</body>