<?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>Tvoje narudžbe - KrikStudio</title>
</head>
<body>
    <?php
        if(!isset($_SESSION['username'])) {
            echo '<script>';
            echo 'window.location.href="./"';
            echo '</script>';
        }
        define('nav', TRUE);
        include 'inc/nav.php';
    ?>

    <section class="history">
        <div class="wrapper">
            <h2>POVIJEST</h2>
            <?php
                $day = new DateTime(date("d.m.Y."));
                $today = $day->format('Y-m-d');
                $counter = 1;
                $query = "SELECT * FROM orders WHERE user = '$username' AND date <= '$today' AND completed = 1 ORDER BY date DESC";
                $result = mysqli_query($dbc, $query);
                if($result) {
                    echo "<table id='your-history'>";
                    echo "<thead>";
                    echo "<tr>";
                    echo "<th>#</th>";
                    echo "<th>DATUM</th>";
                    echo "<th>MENU</th>";
                    echo "<th>DODANO</th>";
                    echo "</tr>";
                    echo "</thead>";
                    echo "<tbody>";
                    while($row = mysqli_fetch_array($result)) {
                        $date = date_create($row['date']);
                        $date = date_format($date,"d.m.Y.");
                        $date_added = date_create($row['added']);
                        $date_added = date_format($date_added,"d.m.Y. H:i:s");
                        $now = date("d.m.Y. H:i:s");
                        if($date_added == $now) {
                            $date_added = '';
                        }
                        
                        echo "<tr>";
                        echo "<td>".$counter."</td>";
                        echo "<td>".$date."</td>";
                        echo "<td>MENU ".$row['menu']."</td>";
                        echo "<td>".$date_added."</td>";
                        echo "</tr>";
                        $counter++;
                    }
                    echo "</tbody>";
                    echo "</table>";
                }
            ?>
        </div>
    </section>

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

    <script>
        feather.replace();
        $(document).ready( function () {
            $('#your-history').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",
                    }
                },
            });
        });
    </script>
</body>