<?php
/**
 * @version 7.9 [rev.7.9.01]
 */
/** @var boolean $inAdmin */
/** @var string $filter */
/** @var string $listingId */
/** @var string $username */

$formPost = ($inAdmin) ? '' : $this->url(array('action' => 'post'));
?>

<?php if ($inAdmin) { ?>
    <nav class="navbar navbar-filter">

        <form class="navbar-form navbar-right"
              action="<?php echo $this->url(null, null, true, array('username', 'listingId', 'page', 'submit')); ?>"
              method="get">
            <div class="form-group">
                <?php echo $this->formElement('text', 'listingId', $listingId)
                    ->setAttributes(array('placeholder' => $this->_('Listing ID'), 'class' => 'form-control input-default'))
                    ->render();
                ?>
                <?php echo $this->formElement('text', 'username', $username)
                    ->setAttributes(array('placeholder' => $this->_('Username'), 'class' => 'form-control input-default'))
                    ->render();
                ?>

            </div>
            <?php echo $this->formElement('submit', 'submit', $this->_('Search'))
                ->setAttributes(array('class' => 'btn btn-default'))
                ->render();
            ?>
        </form>
    </nav>
<?php } ?>

    <form action="<?php echo $formPost; ?>" method="post">
        <?php echo $this->formElement('hidden', 'option', '')->render(); ?>
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <caption>
                    <strong><?php echo $this->paginator->getPages()->totalItemCount; ?></strong> <?php echo $this->_('entries 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>
                        <?php if ($filter == 'pending' || $inAdmin === true) { ?>
                            <th class="size-tiny">
                                <?php echo $this->formElement('checkbox', 'selectAll')
                                    ->setMultiOptions(array(1 => null))
                                    ->render();
                                ?>
                            </th>
                        <?php } ?>
                        <th><?php echo $this->_('Feedback'); ?></th>
                        <th class="size-medium">
                            <?php echo (in_array($filter, array('pending', 'left'))) ? $this->_('To') : $this->_('From'); ?>
                        </th>
                        <?php if ($inAdmin) { ?>
                            <th class="size-medium">
                                <?php echo $this->_('To'); ?>
                            </th>
                        <?php } ?>
                    </tr>
                    </thead>
                <?php } ?>
                <tbody>
                <?php
                /** @var \Ppb\Db\Table\Row\Reputation $reputation */
                foreach ($this->paginator as $reputation) {
                    $listingLink = null;
                    $listingName = null;
                    $listingImage = null;

                    if ($reputation['sale_listing_id'] != null) {
                        /** @var \Ppb\Db\Table\Row\Listing $listing */
                        $saleListing = $reputation->findParentRow('\Ppb\Db\Table\SalesListings');
                        $listing = null;
                        if ($saleListing) {
                            $listing = $saleListing->findParentRow('\Ppb\Db\Table\Listings');
                        }

                        if ($listing) {
                            $listingLink = $listing->link();
                            $listingName = $listing['name'];
                            $listingImage = $listing->getMainImage();
                        }
                    }

                    if (empty($listingName)) {
                        $listingName = ($reputation['listing_name']) ? $reputation['listing_name'] : $this->_('Listing Deleted');
                    }

                    $this->reputation()->setReputation($reputation);
                    ?>
                    <tr>
                        <?php if ($filter == 'pending' || $inAdmin === true) { ?>
                            <td>

                                <?php echo $this->formElement('checkbox', 'id')
                                    ->setMultiOptions(array($reputation['id'] => null))
                                    ->setAttributes(array('class' => 'select-all'))
                                    ->setMultiple()
                                    ->render();
                                ?>
                            </td>
                        <?php } ?>

                        <td>
                            <div class="row">
                                <div class="col-xs-2">
                                    <?php echo $this->thumbnail($listingImage, 50, true,
                                        array('alt' => $listingName)); ?>
                                </div>
                                <div class="col-xs-10">
                                    <div>
                                        <?php if ($listingLink) { ?>
                                            <a href="<?php echo $this->url($listingLink); ?>">
                                                <?php echo $listingName; ?>
                                            </a>
                                            <?php
                                        }
                                        else {
                                            ?>
                                            <em><?php echo $listingName; ?></em>
                                        <?php } ?>
                                    </div>
                                    <?php if ($reputation['posted']) { ?>
                                        <div>
                                            <?php echo $this->reputation()->score(); ?>
                                            <?php echo $this->reputation()->comments($inAdmin); ?>
                                        </div>
                                        <div>
                                            <small
                                                class="muted"><?php echo $this->date($reputation['updated_at']); ?></small>
                                        </div>
                                    <?php } ?>
                                </div>
                            </div>
                        </td>
                        <td>
                            <?php
                            if ((in_array($filter, array('pending', 'left')))) {
                                $userKey = 'User';
                                $userLabel = ($reputation['reputation_type'] == 'sale') ? 'Seller' : 'Buyer';
                            }
                            else {
                                $userKey = 'Poster';
                                $userLabel = ($reputation['reputation_type'] == 'sale') ? 'Buyer' : 'Seller';
                            }

                            /** @var \Ppb\Db\Table\Row\User $user */
                            $user = $reputation->findParentRow('\Ppb\Db\Table\Users', $userKey);

                            echo $this->_($userLabel) . ': ' . $this->userDetails()->setUser($user)->display();
                            ?>
                        </td>
                        <?php if ($inAdmin) { ?>
                            <td>
                                <?php
                                if ((in_array($filter, array('pending', 'left')))) {
                                    $userKey = 'Poster';
                                    $userLabel = ($reputation['reputation_type'] == 'sale') ? 'Buyer' : 'Seller';
                                }
                                else {
                                    $userKey = 'User';
                                    $userLabel = ($reputation['reputation_type'] == 'sale') ? 'Seller' : 'Buyer';
                                }

                                $user = $reputation->findParentRow('\Ppb\Db\Table\Users', $userKey);

                                echo $userLabel . ': ' . $this->userDetails()->setUser($user)->display();
                                ?>
                            </td>
                        <?php } ?>
                    </tr>
                <?php } ?>

                <?php if ($this->paginator->getPages()->totalItemCount) { ?>
                    <tr>
                        <td colspan="6">
                            <div class="btn-group">
                                <?php if (in_array($filter, array('pending'))) { ?>
                                    <button class="btn btn-default"
                                            value="close"><?php echo $this->_('Leave Feedback'); ?></button>
                                <?php } ?>
                                <?php if ($inAdmin) { ?>
                                    <button class="btn btn-default confirm-form" value="delete"
                                            data-message="<?php echo $this->_('Are you sure you want to delete the reputation comments you have selected?'); ?>">
                                        <?php echo $this->_('Delete'); ?>
                                    </button>
                                <?php } ?>
                            </div>
                        </td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
        </div>
    </form>

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