<?php
/**
 * @version 7.10 [rev.7.10.02]
 */

$categories = $this->categories()->getData();

/** @var \Ppb\Service\Table\Relational\Categories $categoriesService */
$categoriesService = $this->categories()->getCategories();
if (!count($categories)) {
    $categories = $this->categories(
        $categoriesService->getTable()
            ->select()
            ->where('parent_id IS NULL')
            ->where('enable_auctions = ?', 1)
            ->where('user_id IS NULL')
            ->order(array('order_id ASC', 'name ASC'))
    );
}

/** @var \Cube\Controller\Request $request */
$request = $this->request()->getRequest();

$categoryId = $request->getParam('category_id');
$parentId = $request->getParam('parent_id');

?>

<div id="desktop-navigation" class="hidden-xs">
    <!-- branding header -->
    <div class="branding-header">
        <div class="container">
            <!-- logo left -->
            <div class="logo">
                <a href="<?php echo $this->url(null, 'app-home'); ?>"
                   title="<?php echo $this->settings['sitename']; ?>">
                    <img src="<?php echo $this->thumbnail()->generateImagePath($this->settings['site_logo_path']); ?>"
                         alt="<?php echo $this->settings['sitename']; ?>">
                </a>
            </div>

            <div class="header-bar">
                <?php if ($this->settings['user_languages']) { ?>
                    <div class="language-selector pull-right">
                        <?php echo $this->language(); ?>
                    </div>
                <?php } ?>

                <div class="login-buttons pull-right">
                    <span class="welcome-msg">
                        <?php echo $this->_('Welcome'); ?>,
                        <b><?php echo (isset($this->loggedInUser['username'])) ? $this->loggedInUser['username'] : $this->_('Guest'); ?></b>
                    </span>

                    <?php if (!empty($this->loggedInUser['id'])) { ?>
                        <a href="<?php echo $this->url(null,
                            'members-index'); ?>"><?php echo $this->_('Members Area'); ?></a>
                        <a href="<?php echo $this->url(null,
                            'members-logout'); ?>" title="<?php echo $this->_('Logout'); ?>"><i
                                    class="fa fa-sign-out"></i></a>
                        <?php
                    }
                    else {
                        ?>
                        <a <?php echo ($this->settings['registration_type'] == 'quick' && !$this->settings['enable_recaptcha']) ? 'class="dialog-box"' : ''; ?>
                                href="<?php echo $this->url(array('module' => 'members', 'controller' => 'user', 'action' => ($this->settings['registration_type'] == 'quick' && !$this->settings['enable_recaptcha']) ? 'register-modal' : 'register')); ?>"
                                title="<?php echo $this->_('Register'); ?>"><i class="fa fa-user-plus"></i></a>
                        <a class="dialog-box"
                           href="<?php echo $this->url(array('module' => 'members', 'controller' => 'user', 'action' => 'login-modal')); ?>"
                           title="<?php echo $this->_('Login'); ?>"><i class="fa fa-sign-in"></i></a>
                    <?php } ?>
                    <a href="<?php echo $this->url(array('module' => 'members', 'controller' => 'tools', 'action' => 'watched-items')); ?>"
                       title="<?php echo $this->_('Wishlist'); ?>"><i class="fa fa-heart-o"></i></a>
                </div>

                <div class="header-search-form">
                    <form id="form-header-search" class="form-inline form-header"
                          action="<?php echo $this->url(null, 'listings-search'); ?>"
                          method="get">
                        <div class="form-group hidden-sm">
                            <input type="hidden" name="parent_id"
                                   value="<?php echo $request->getParam('parent_id'); ?>">

                            <ul id="category-select-content">
                                <li>
                                    <a href="#"
                                       data-id=""
                                       data-category-name="<?php echo $this->_('All Categories'); ?>">
                                        <?php echo $this->_('All Categories'); ?>
                                    </a>
                                </li>
                                <?php
                                $selectedCategory = $this->_('Choose Category');
                                $maxChars = 20;

                                /** @var \Ppb\Db\Table\Row\Category $category */
                                foreach ($categories as $category) {
                                    $shortCategoryName = $this->_($category['name']);
                                    $length = strlen($shortCategoryName);
                                    $shortCategoryName = substr($shortCategoryName, 0, $maxChars) . (($length > $maxChars) ? '...' : '');

                                    if ($category['id'] == $request->getParam('parent_id')) {
                                        $selectedCategory = $shortCategoryName;
                                    }
                                    ?>
                                    <li>
                                        <a href="<?php echo $this->url($category->link()); ?>"
                                           data-id="<?php echo $category['id']; ?>"
                                           data-category-name="<?php echo $shortCategoryName; ?>">
                                            <?php echo $this->_($category['name']); ?>
                                        </a>
                                    </li>
                                <?php } ?>
                            </ul>

                            <a id="category-select-btn" class="form-control input-medium" href="#">
                                <span><?php echo $selectedCategory; ?></span> <i class="fa fa-angle-down"></i>
                            </a>
                        </div>

                        <div class="form-group">
                            <input id="basic-search" type="text" name="keywords" class="form-control input-search"
                                   value="<?php echo $this->request()->getRequest()->getParam('keywords'); ?>"
                                   placeholder="<?php echo $this->_('Search...'); ?>">
                        </div>
                        <button type="submit"
                                class="btn btn-primary"><?php echo $this->_('Search'); ?></button>
                    </form>
                </div>
            </div>

            <!-- end logo left -->

        </div>
    </div>
    <!-- end branding header -->

    <!-- header advert -->
    <?php
    $parentId = $request->getParam('parent_id');
    $advert = $this->advert()->findBySection('header', false, array($parentId));

    if ($advert !== null) {
        ?>
        <p class="text-center">
            <?php echo $this->advert($advert)->display(); ?>
        </p>
    <?php } ?>

    <!-- search bar -->
    <div class="search-bar">
        <div class="container">
            <!-- browse menu -->
            <div class="browse-menu col-md-3 hidden-sm">
                <a id="browse-btn" href="#"><?php echo $this->_('Categories'); ?> <i class="fa fa-angle-down"></i></a>
            </div>
            <ul id="browse-content">
                <?php
                /** @var \Ppb\Db\Table\Row\Category $category */
                foreach ($categories as $category) {
                    $counter = $category->getCounter();

                    if ($counter > 0 || !$this->settings['hide_empty_categories']) {
                        ?>
                        <li>
                            <a href="<?php echo $this->url($category->link()); ?>">
                                <?php echo $this->_($category['name']); ?>
                            </a>

                            <?php
                            if ($this->settings['category_counters']) {
                                echo '<small>(' . $counter . ')</small>';
                            }
                            ?>

                        </li>
                    <?php } ?>
                <?php } ?>
            </ul>
            <!-- end browse menu -->

            <!-- main navigation -->
            <div class="col-md-9 col-sm-12">
                <ul class="nav navbar-nav">
                    <?php foreach ($this->menu->pages as $page) { ?>
                        <?php if ((!$page->settings || (intval($this->settings[(string)$page->settings['name']]) == $page->settings['value'])) &&
                            ($this->navigation()->accept($page)) && $page->hidden != 'true'
                        ) {
                            ?>
                            <li <?php echo ($page->isActive(true)) ? 'class="active"' : ''; ?>>
                                <a href="<?php echo $this->url($page->params); ?>">
                                    <?php echo $page->label; ?>
                                </a>
                            </li>
                        <?php } ?>
                    <?php } ?>
                </ul>

                <?php if ($this->settings['enable_shopping_cart']) { ?>
                    <ul class="nav navbar-nav navbar-right">
                        <li>
                            <?php echo $this->cartDropdown(); ?>
                        </li>
                    </ul>
                <?php } ?>


            </div>
            <!-- end main navigation -->

        </div>
        <!-- end advanced search -->
    </div>
    <!-- end search bar -->

</div>
