<?php
/**
 * @version 7.9 [rev.7.9.03]
 */
?>
<nav class="navbar navbar-filter">
    <div class="navbar-left">
        <?php if ($section != 'admin' || $this->loggedInUser['role'] == \Ppb\Service\Users::ADMIN_ROLE_PRIMARY) { ?>
            <a class="btn btn-default"
               href="<?php echo $this->url(array('controller' => 'users', 'action' => 'add', 'view' => $this->section)); ?>">
                <?php echo $this->_('Create Account'); ?>
            </a>
        <?php } ?>

        <?php if ($this->section != 'admin') { ?>
            <?php
            $container = $this->navigation()->getContainer();

            echo $this->navigation()
                ->setContainer($container->findOneBy('label', $this->_('Filter Site Users')))
                ->setPartial('navigation/browse-filter.phtml')
                ->menu();

            // reset navigation container (workaround for the navigation on the layout page)
            $this->navigation()->setContainer($container);
            ?>
        <?php } ?>
    </div>

    <form class="navbar-form navbar-right"
          action="<?php echo $this->url(null, null, true, array('username', 'email', 'page', 'submit')); ?>"
          method="get">
        <div class="form-group">
            <?php echo $this->formElement('text', 'id', $this->id)
                ->setAttributes(array('placeholder' => $this->_('User ID'), 'class' => 'form-control input-small'))
                ->render();
            ?>
            <?php echo $this->formElement('text', 'username', $this->username)
                ->setAttributes(array('placeholder' => $this->_('Username'), 'class' => 'form-control input-default'))
                ->render();
            ?>
            <?php echo $this->formElement('text', 'email', $this->email)
                ->setAttributes(array('placeholder' => $this->_('Email Address'), 'class' => 'form-control input-default'))
                ->render();
            ?>
        </div>
        <?php echo $this->formElement('submit', 'submit', $this->_('Search'))
            ->setAttributes(array('class' => 'btn btn-default'))
            ->render();
        ?>
    </form>
</nav>
<div class="table-responsive">
    <table class="table table-striped table-hover">
        <caption>
            <strong><?php echo $this->paginator->getPages()->totalItemCount; ?></strong> <?php echo $this->_('users 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-small"><?php echo $this->_('Username'); ?></th>
                <th><?php echo $this->_('User Details / Settings'); ?></th>
                <th class="size-large"><?php echo $this->_('Status'); ?></th>
                <th class="size-tiny"></th>
            </tr>
            </thead>
        <?php } ?>
        <tbody>
        <?php
        /** @var \Ppb\Db\Table\Row\User $user */
        foreach ($this->paginator as $user) {
            ?>
            <tr>
                <td>
                    <?php echo $user['username']; ?>
                </td>
                <td>
                    <div>
                        <strong><?php echo $this->_('Name'); ?></strong>: <?php echo $this->userDetails($user)->displayFullName(); ?>
                    </div>
                    <div>
                        <strong><?php echo $this->_('Email'); ?></strong>: <?php echo $user['email']; ?>
                    </div>
                    <div class="pdb-10">
                        <strong><?php echo $this->_('Reg. Date'); ?></strong>: <?php echo $this->date($user['created_at']); ?>
                    </div>

                    <?php if (!in_array($user['role'], $adminRoles)) { ?>
                        <div class="btn-group">
                            <a class="btn btn-default"
                               title="<?php echo $this->_('user details'); ?>"
                               data-toggle="collapse"
                               data-target="#info<?php echo $user['id']; ?>">
                                <i class="fa fa-male"></i>
                            </a>
                            <?php if ($this->settings['private_site']) { ?>
                                <a class="btn confirm-box <?php echo ($user['is_seller']) ? 'btn-success' : 'btn-danger'; ?>"
                                   href="<?php echo $this->url(array('controller' => 'users', 'action' => 'change-status', 'option' => 'is_seller', 'id' => $user['id'], 'flag' => !$user['is_seller']),
                                       null, true); ?>"
                                   title="<?php echo $this->_('Change selling privileges'); ?>"
                                   data-message="<?php echo sprintf($this->_("%s selling privileges for user '%s'?"),
                                       ($user['is_seller']) ? $this->_('Disable') : $this->_('Activate'),
                                       $user['username']); ?>">
                                    <i class="fa fa-gavel"></i>
                                </a>
                            <?php } ?>

                            <a class="btn btn-default jq-popup-form"
                               href="<?php echo $this->url(array('action' => 'user-options', 'id' => $user['id'])); ?>"
                               data-close-redirect="<?php echo $this->url(array(
                                   'action' => 'browse', 'view' => 'site', 'username' => $user['username'])); ?>"
                               title="<?php echo $this->_('Edit User Options'); ?>">
                                <i class="fa fa-cogs"></i>
                            </a>

                            <a class="btn btn-default"
                               href="<?php echo $this->url(array('controller' => 'tools', 'action' => 'accounting', 'id' => $user['id'])); ?>"
                               title="<?php echo $this->_('View Account History'); ?>">
                                <i class="fa fa-table"></i>
                            </a>

                            <a class="btn btn-default"
                               href="<?php echo $this->url(array('controller' => 'listings', 'action' => 'browse', 'show' => 'by-user', 'user_id' => $user['id'])); ?>"
                               title="<?php echo $this->_('View Listings'); ?>">
                                <i class="fa fa-files-o"></i>
                            </a>
                            <a class="btn btn-default confirm-box"
                               href="<?php echo $this->url(array('action' => 'login-as', 'id' => $user['id'])); ?>"
                               data-message="<?php echo sprintf($this->_("You will be logged in as '%s'. Proceed?"),
                                   $user['username']); ?>"
                               title="<?php echo $this->_('Login as User'); ?>">
                                <i class="fa fa-unlock-alt"></i>
                            </a>

                            <?php if ($user->userPaymentMode() == 'account' && $user['balance'] > 0) { ?>
                                <a class="btn btn-default confirm-box"
                                   href="<?php echo $this->url(array('action' => 'payment-reminder', 'id' => $user['id'])); ?>"
                                   data-message="<?php echo sprintf($this->_("Send payment reminder to user '%s'?"),
                                       $user['username']); ?>"
                                   title="<?php echo $this->_('Send Payment Reminder'); ?>">
                                    <i class="fa fa-money"></i>
                                </a>
                            <?php } ?>

                            <a class="btn btn-default jq-popup-form"
                               href="<?php echo $this->url(array('action' => 'send-message', 'receiver_id' => $user['id'])); ?>"
                               title="<?php echo $this->_('Send Message To User'); ?>">
                                <i class="fa fa-envelope-o"></i>
                            </a>


                        </div>
                    <?php } ?>

                    <div id="info<?php echo $user['id']; ?>" class="collapse">
                        <hr>
                        <div>
                            <b><?php echo $this->_('Address'); ?></b>: <?php echo $this->fieldDisplay($this->userDetails()->displayAddress(', ')); ?>
                        </div>
                        <div>
                            <b><?php echo $this->_('Phone'); ?></b>: <?php echo $this->fieldDisplay($user['phone']); ?>
                        </div>
                        <?php if ($user['birthdate'] > 0) { ?>
                            <div>
                                <b><?php echo $this->_('Birthdate'); ?></b>: <?php echo $this->date($user['birthdate'], true); ?>
                            </div>
                        <?php } ?>
                        <?php if ($user['last_login'] > 0) { ?>
                            <div>
                                <b><?php echo $this->_('Last Login'); ?></b>: <?php echo $this->date($user['last_login']); ?>
                            </div>
                        <?php } ?>

                        <div>
                            <b><?php echo $this->_('IP Address'); ?></b>:
                            <em>
                                <?php echo $this->fieldDisplay($user['ip_address'],
                                    '<a href="http://db-ip.com/' . $user['ip_address'] . '" target="_blank">' . $user['ip_address'] . '</a>'); ?>
                            </em>
                        </div>
                    </div>
                </td>
                <td>
                    <?php echo $this->userDetails()->status(); ?>
                </td>
                <td>
                    <?php if (!in_array($user['role'],
                            $adminRoles) || $this->loggedInUser['role'] == \Ppb\Service\Users::ADMIN_ROLE_PRIMARY
                    ) {
                        ?>
                        <div class="btn-group-vertical">
                            <?php if (!$user['approved']) { ?>
                                <a class="btn btn-success btn-sm confirm-box"
                                   href="<?php echo $this->url(array('controller' => 'users', 'action' => 'change-status', 'option' => 'approve', 'id' => $user['id']),
                                       null, true); ?>"
                                   title="<?php echo $this->_('Approve Account'); ?>"
                                   data-message="<?php echo $this->_('Are you sure you want to approve this account?'); ?>">
                                    <i class="fa fa-check-square-o"></i>
                                </a>
                            <?php } ?>
                            <?php if (!$user['mail_activated']) { ?>
                                <a class="btn btn-info btn-sm confirm-box"
                                   href="<?php echo $this->url(array('controller' => 'users', 'action' => 'change-status', 'option' => 'verify-email', 'id' => $user['id']),
                                       null, true); ?>"
                                   title="<?php echo $this->_('Verify Email Address'); ?>"
                                   data-message="<?php echo $this->_("Are you sure you want to verify this user's email address?"); ?>">
                                    <i class="fa fa-envelope-o"></i>
                                </a>
                            <?php } ?>

                            <a class="btn btn-default btn-sm"
                               href="<?php echo $this->url(array('controller' => 'users', 'action' => 'manage', 'view' => $this->section, 'id' => $user['id'])); ?>"
                               title="<?php echo $this->_('Edit User'); ?>">
                                <i class="fa fa-folder-open"></i>
                            </a>

                            <?php
                            if (!$user['active']) {
                                $flag = 1;
                                $msg = $this->_('activate');
                                $title = $this->_('Activate Account');
                                $cls = 'btn-success';
                                $btn = '<i class="fa fa-check-circle"></i>';
                            }
                            else {
                                $flag = 0;
                                $msg = $this->_('suspend');
                                $cls = 'btn-default';
                                $title = $this->_('Suspend Account');
                                $btn = '<i class="fa fa-ban"></i>';
                            }
                            ?>
                            <a class="btn <?php echo $cls; ?> btn-sm confirm-box"
                               href="<?php echo $this->url(array('controller' => 'users', 'action' => 'change-status', 'option' => 'activate', 'flag' => $flag, 'id' => $user['id']),
                                   null, true); ?>"
                               title="<?php echo $title; ?>"
                               data-message="<?php echo sprintf($this->_('Are you sure you want to %s this account?'),
                                   $msg); ?>">
                                <?php echo $btn; ?>
                            </a>

                            <a class="btn btn-danger btn-sm confirm-box"
                               href="<?php echo $this->url(array('controller' => 'users', 'action' => 'delete', 'id' => $user['id']),
                                   null, true); ?>"
                               title="<?php echo $this->_('Delete User'); ?>"
                               data-message="<?php echo $this->_('Are you sure you want to delete this account?'); ?>">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    <?php } ?>
                </td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
</div>

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