<?php
/**
 * @version 7.7
 */

/* @var \Ppb\Service\Table\Relational\Categories $categoriesService */
?>
<nav class="navbar navbar-filter">
    <div class="navbar-left">

        <a class="btn btn-default pull-left spacing-right"
           href="<?php echo $this->url(array('action' => 'create-advert')); ?>">
            <?php echo $this->_('Create Advert'); ?>
        </a>
    </div>
</nav>
<form method="post" action="" class="form-horizontal">

    <div class="table-responsive">
        <table class="table table-striped table-hover">
            <caption>
                <strong><?php echo $this->paginator->getPages()->totalItemCount; ?></strong> <?php echo $this->_('adverts found.'); ?>

                <?php if ($this->paginator->getPages()->totalItemCount) { ?>
                    <?php echo $this->_('Showing results'); ?>
                    <strong><?php echo $this->paginator->getPages()->firstItemNumber; ?></strong>
                    <?php echo $this->_('to'); ?>
                    <strong><?php echo $this->paginator->getPages()->lastItemNumber; ?></strong>
                <?php } ?>
            </caption>
            <?php if ($this->paginator->getPages()->totalItemCount) { ?>
                <thead>
                <tr>
                    <th class="size-tiny"></th>
                    <th><?php echo $this->_('Advert Details'); ?></th>
                    <th class="size-small"><?php echo $this->_('Section'); ?></th>
                    <th class="size-small"><?php echo $this->_('Stats'); ?></th>
                    <th class="size-tiny"><?php echo $this->_('Active'); ?></th>
                    <th class="size-tiny"></th>
                </tr>
                </thead>
            <?php } ?>
            <tbody>
            <?php
            $checkboxCounter = 0;

            /** @var \Cube\Db\Table\Row $advert */
            foreach ($this->paginator as $advert) {
                ?>
                <tr>
                    <td>
                        <a class="btn btn-sm btn-default ajax-popup"
                           href="<?php echo $this->url(array('action' => 'preview-advert', 'id' => $advert['id'])); ?>"
                           title="<?php echo $this->_('Preview Advert'); ?>">
                            <i class="fa fa-picture-o"></i>
                        </a>
                    </td>
                    <td>
                        <p>
                            <?php echo $advert['name']; ?>

                            <span class="label label-default pull-right"><?php echo $advert['type']; ?></span>
                        </p>
                        <div>
                            <small><strong><?php echo $this->_('Language:'); ?></strong> <?php echo $this->fieldDisplay($advert['language'], null, $this->_('All')); ?></small>
                        </div>
                        <div>
                            <small>
                                <strong><?php echo $this->_('Categories:'); ?></strong>

                                <?php
                                if ($advert['category_ids']) {
                                    $select = $categoriesService->getTable()->select()
                                        ->where('id IN (?)', \Ppb\Utility::unserialize($advert['category_ids']));
                                    $multiOptions = $categoriesService->getMultiOptions($select);

                                    echo implode('; ', $multiOptions);
                                }
                                else {
                                    echo $this->_('All Categories');
                                } ?>
                            </small>
                        </div>
                    </td>
                    <td>
                        <?php echo $this->sections[$advert['section']]; ?>
                    </td>
                    <td>
                        <div>
                            <small>
                                <?php echo $this->_('Added:'); ?> <?php echo $this->date(max(array($advert['created_at'], $advert['updated_at'])),
                                    true); ?>
                            </small>
                        </div>
                        <div>
                            <small>
                                <?php echo $this->_('Views:'); ?> <?php echo $advert['nb_views']; ?>
                            </small>
                        </div>
                        <?php if ($advert['type'] == 'image') { ?>
                            <div>
                                <small>
                                    <?php echo $this->_('Clicks:'); ?> <?php echo $advert['nb_clicks']; ?>
                                </small>
                            </div>
                        <?php } ?>
                    </td>
                    <td>
                        <?php echo $this->formElement('hidden', 'id')
                            ->setMultiple()
                            ->setValue($advert['id'])
                            ->render();
                        ?>
                        <?php echo $this->formElement('checkbox', 'active')
                            ->setValue($advert['active'])
                            ->setMultiOptions(array(1 => null))
                            ->setBrackets('[' . $checkboxCounter . ']')
                            ->setMultiple()
                            ->render();
                        ?>
                    </td>
                    <td>
                        <div class="btn-group-vertical">
                            <a class="btn btn-default btn-sm"
                               href="<?php echo $this->url(array('action' => 'edit-advert', 'id' => $advert['id'])); ?>"
                               title="<?php echo $this->_('Edit Advert'); ?>">
                                <i class="fa fa-folder-open"></i>
                            </a>
                            <a class="btn btn-danger btn-sm confirm-box"
                               href="<?php echo $this->url(array('action' => 'delete-advert', 'id' => $advert['id']),
                                   null, true); ?>"
                               title="<?php echo $this->_('Delete Advert'); ?>"
                               data-message="<?php echo $this->_('Are you sure you want to delete this advert?'); ?>">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </td>
                </tr>
                <?php $checkboxCounter ++; ?>
            <?php } ?>
            <tr>
                <td colspan="8">
                    <?php echo $this->formElement('submit', 'submit', $this->_('Update'))
                        ->setAttributes(array('class' => 'btn btn-primary btn-lg'))
                        ->render();
                    ?></td>
            </tr>
            </tbody>
        </table>
    </div>
</form>

<?php echo $this->pagination($this->paginator, 'sliding', 'partials/pagination.phtml'); ?>