<?php
$title = (isset($title)) ? $title : 'Bookstore Project';
$content = (isset($content)) ? $content : '';
$nav = (isset($nav)) ? $nav : false;
require_once 'utilcart.php';
require_once 'dbPDO.php';
require_once 'utiluser.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title><?php echo $title; ?> | Better Books</title>
    <link rel="stylesheet" type="text/css" href="bookStyle.css">
    <link href='https://fonts.googleapis.com/css?family=Roboto:400,700,500' rel='stylesheet' type='text/css'>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="bookPhoto.js"></script>
</head>
<body<?php echo ($nav) ? '': ' class="noNav"';?>>
<div id="wrap">
    <header>
        <section class="title">
            <h1 title="<?php echo $title; ?>"><?php echo $title; ?></h1>
        </section>
        <section class="menu">
        <a href="./" class="logo"><img src="books.png">Better Books</a> | <a href="./">Home</a> | <a href="shoppingCart.php">Cart<?php echo (count($bookArray) > 0) ? ' (' . array_sum($bookArray) . ')' : '' ?></a> | <a href="account.php">

<?php 
if ($userCustID > 0) {
    $user = $db->prepare('SELECT fname FROM bookCustomers WHERE CustID = ?;');
    $user->execute(array($userCustID));
    $username = $user->fetch();
    if (strlen($username['fname']) > 0) {
        echo $username['fname'];
    } else {
        echo 'Account';
    }
} else {
    echo 'Account';
}
?>
</a>
        </section>
    </header>
    <main>
        <?php if ($nav) { ?>
        <aside id="menu">
            <section>
                <h1>Search</h1>
                <form action="searchBrowse.php" method="get">
                    <input type="search" name="search" results="5">
                    <div class="center"><input type="submit" value="Search"></div>
                </form>
            </section>
            <nav>
                <h1>Browse</h1>
                <ul>
                    <?php 
                    $categories = $db->query('SELECT DISTINCT c.CategoryID AS catID, c.CategoryName, count(c.CategoryID) as total FROM bookcategories c, bookcategoriesbooks cb WHERE cb.CategoryID = c.CategoryID GROUP BY c.CategoryID ORDER BY c.CategoryName;')->fetchAll();
                    foreach ($categories as $row) {
                        $catName = str_replace(" ", "+", $row['CategoryName']);
                        echo "<li><a href=\"searchBrowse.php?catID=$row[catID]&catName=$catName\">$row[CategoryName] <small>($row[total])</small></a></li>\n";
                    }
                    ?>
                </ul>
            </nav>
            <?php } ?>
        </aside>
        <article id="content">
            <?php echo $content; ?>
        </article>
    </main>
    <footer>
        <?php 
        if ($userUUID && $nav) {
            echo "<div class=\"recent\"><div title=\"Recently Viewed:\">Recently Viewed:</div>";
            $recent = $db->prepare('SELECT DISTINCT d.title, d.ISBN FROM bookdescriptions d, bookcustomerviews v WHERE d.ISBN = v.ISBN AND v.uuid = ? ORDER BY v.time DESC LIMIT 5;');
            $recent->execute(array(hex2bin($userUUID)));
            foreach ($recent->fetchAll() as $row) {
                echo "<div title=\"$row[title]\"><a href=\"productPage.php?isbn=$row[ISBN]\">$row[title]</a></div>";
            }
            echo "</div>";
        }
        ?>
            <div class="links"><a href="./">Home</a> | <a href="shoppingCart.php">Cart</a> | <a href="account.php">Account</a> | <a href="about.php">About Us</a> <?php if ($userCustID > 0) {?>| <a href="account.php?logout=true">Logout</a><?php } ?></div></footer>
    </div>
</body>
</html>
