<?php
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

if (!defined('NICKELBOX')) {
    die("Direct access denied.");
}

$cats = $binstack->select('categories', ['catid (id)', 'catname (name)']);
$categories = [];
foreach ($cats as $c) {
    if ($binstack->has('items', ['AND' => ['catid' => $c['id'], 'price[>]' => 0]])) {
        $categories[] = $c;
    }
}
?>
<nav class="navbar navbar-expand navbar-dark bg-dark">
    <a class="navbar-brand" href="./"><?php echo $settings['sitename']; ?></a>
    <div class="collapse navbar-collapse">
        <ul class="navbar-nav d-none d-md-inline">
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="categorydropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Categories
                </a>
                <div class="dropdown-menu" aria-labelledby="categorydropdown">
                    <?php
                    foreach ($categories as $c) {
                        ?>
                        <a class="dropdown-item" href="./?page=browse&cat=<?php echo $c['id']; ?>"><?php echo $c['name']; ?></a>
                        <?php
                    }
                    ?>
                </div>
            </li>
        </ul>
        <form class="form-inline ml-3" method="GET">
            <div class="input-group">
                <input type="hidden" name="page" value="search" />
                <input name="q" class="form-control" type="search" placeholder="Search" aria-label="Search" value="<?php
                if (isset($_GET['q'])) {
                    echo htmlspecialchars($_GET['q']);
                }
                ?>">
                <div class="input-group-addon">
                    <button class="btn btn-success" type="submit"><i class="fas fa-search"></i> <span class="d-none d-md-inline">Search</span></button>
                </div>
            </div>
        </form>
        <div class="d-none d-md-inline ml-auto">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" href="./?page=cart">
                        <i class="fas fa-shopping-cart"></i> Cart
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="./?page=account">
                        <i class="fas fa-user-circle"></i> Account
                    </a>
                </li>
            </ul>
        </div>
    </div>
</nav>
<nav class="navbar navbar-expand navbar-dark bg-dark d-md-none">
    <div class="collapse navbar-collapse justify-content-between">
        <ul class="navbar-nav">
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="categorydropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Categories
                </a>
                <div class="dropdown-menu" aria-labelledby="categorydropdown">
                    <?php
                    foreach ($categories as $c) {
                        ?>
                        <a class="dropdown-item" href="./?page=browse&cat=<?php echo $c['id']; ?>"><?php echo $c['name']; ?></a>
                        <?php
                    }
                    ?>
                </div>
            </li>
        </ul>
        <ul class="navbar-nav ml-auto">
            <li class="nav-item">
                <a class="nav-link" href="./?page=cart">
                    <i class="fas fa-shopping-cart"></i> Cart
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="./?page=account">
                    <i class="fas fa-user-circle"></i> Account
                </a>
            </li>
        </ul>
    </div>
</nav>