<?php

require('../lib/get-orders.php');
require('../lib/validate-params.php');

$sort = 'ID';
$order = 'ASC';

// Read value from ?sort and ?order parameters
if (isset($_GET['sort'])) {
    $sort = sanitizeSortParam($_GET['sort']);
}
if (isset($_GET['order'])) {
    $order = sanitizeOrderParam($_GET['order']);
}

$orders = getOrders($sort, $order);

?>

<html>
    <head>
        <title>Orders</title>
        <!-- linking css -->
        <link rel="stylesheet" href="assets/css/order.css" />
        <!-- scripts -->
              <script src="assets/orders.js"></script>
        			<script src="assets/js/jquery.min.js"></script>
        			<script src="assets/js/jquery.poptrox.min.js"></script>
        			<script src="assets/js/skel.min.js"></script>
        			<script src="assets/js/util.js"></script>
        			<script src="assets/js/main.js"></script>
    </head>
    <body>

      <!-- navbar -->
        <header id="header">
        <div class="logo"><a href="#">Shadowheartz<span>Orders</span></a></div>
        <a href="http://localhost/pedejais%20darbs/webroot/index.html" class="btn">Home</a>
        <a href="http://localhost/pedejais%20darbs/webroot/orders.php" class="btn">Orders</a>
        <a href="http://localhost/pedejais%20darbs/webroot/contact.html" class="btn">Contact</a>
        </header>

    <div class="table_container">

    	<div class="table">
    		<div class="table-header">
    			<div class="header__item">
                    <?php if ($sort == 'ID') { ?>
                        <a class="filter__link is-active" href="?sort=ID&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC'; ?>">id</a>
                    <?php } else { ?>
                        <a class="filter__link" href="?sort=ID&order=ASC">id</a>
                    <?php } ?>
                </div>
    			<div class="header__item">
                    <?php if ($sort == 'Name') { ?>
                        <a class="filter__link is-active" href="?sort=Name&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC'; ?>">Name</a>
                    <?php } else { ?>
                        <a class="filter__link" href="?sort=Name&order=ASC">Name</a>
                    <?php } ?>
                </div>
    			<div class="header__item">
                    <?php if ($sort == 'Created on') { ?>
                        <a class="filter__link is-active" href="?sort=Created on&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC'; ?>">Created on</a>
                    <?php } else { ?>
                        <a class="filter__link" href="?sort=Created on&order=ASC">Created on</a>
                    <?php } ?>
                </div>
    			<div class="header__item">
                    <?php if ($sort == 'Status') { ?>
                        <a class="filter__link is-active" href="?sort=Status&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC'; ?>">Status</a>
                    <?php } else { ?>
                        <a class="filter__link" href="?sort=Status&order=ASC">Status</a>
                    <?php } ?>
                </div>
    		</div>
        <div class="table-content">
                <?php foreach  ($orders as $key => $order) { ?>
                    <div class="table-row">
                        <div class="table-data"><?php echo $order['ID'] ?></div>
                        <div class="table-data"><?php echo $order['Name'] ?></div>
                        <div class="table-data"><?php echo $order['Created on'] ?></div>
                        <div class="table-data <?php echo $order['Status']; ?>"><?php echo $order['Status'] ?></div>
                    </div>
                <?php } ?>
    		</div>
    	</div>
    </div>
    <!-- for icons -->
        <footer id="footer">
          <div class="container">
            <ul class="icons"> <!-- opens in new page not overiding the main one -->
              <li><a target="blank" rel="noopener" href="https://steamcommunity.com/id/Shadowheartz/" class="icon fa-steam-square"><span class="label">steam</span></a></li>
              <li><a target="blank" rel="noopener" href="https://open.spotify.com/artist/3W980ccXL5bmbKN08bvpCC" class="icon fa-spotify"><span class="label">laukupuikas</span></a></li>
            </ul>
          </div>
          <div class="undertext">
            &copy; Made by Shadowheartz <a target="blank" rel="noopener" href="https://steamcommunity.com/id/Shadowheartz/">steam</a> and artwork <a href="https://imgur.com/a/PM1gj">Designs</a>
          </div>
        </footer>
    </body>
</html>
