<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' => 'add-newsletter')); ?>">
            <?php echo $this->_('Create Newsletter'); ?>
        </a>

    </div>

    <form class="navbar-form navbar-right"
          action="<?php echo $this->url(null, null, true, array('title', 'submit')); ?>"
          method="get">
        <div class="form-group">
            <?php echo $this->formElement('text', 'title', $this->title)
                ->setAttributes(array('placeholder' => $this->_('Newsletter Title'), '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->_('newsletters 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><?php echo $this->_('Title'); ?></th>
                <th class="size-large"><?php echo $this->_('Send To'); ?></th>
                <th class="size-mini"><?php echo $this->_('Date'); ?></th>
                <th class="size-mini"><?php echo $this->_('Options'); ?></th>
            </tr>
            </thead>
        <?php } ?>
        <tbody>
        <?php
        /** @var \Cube\Db\Table\Row $newsletter */
        foreach ($this->paginator as $newsletter) {
            ?>
            <tr>
                <td>
                    <?php echo $newsletter['title']; ?>
                </td>
                <td>
                    <?php if ($newsletter['updated_at']) { ?>
                        <small class="text-success">
                            <?php echo $this->_('Sent on'); ?> <?php echo $this->date($newsletter['updated_at']); ?>
                        </small>
                    <?php
                    }
                    else if ($newsletter['recipients']) {
                        ?>
                        <small class="text-primary">
                            <?php echo $this->_('Sending in progress'); ?> <br>
                            <?php echo $newsletter['recipients']; ?> <?php echo $this->_('recipients remaining'); ?>
                        </small>
                    <?php
                    }
                    else {
                        ?>
                        <div class="btn-group">
                            <button class="btn btn-default"><?php echo $this->_('Select Recipients'); ?></button>
                            <button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                <span class="caret"></span>
                            </button>

                            <ul class="dropdown-menu">
                                <?php foreach ($recipients as $key => $recipient) { ?>
                                    <li>
                                        <a href="<?php echo $this->url(array('send' => $key, 'id' => $newsletter['id']), null, true); ?>">
                                            <?php echo $recipient['name']; ?>
                                        </a>
                                    </li>
                                <?php } ?>
                            </ul>

                        </div>

                    <?php } ?>
                </td>
                <td>
                    <?php echo $this->date(max(array($newsletter['created_at'], $newsletter['updated_at'])),
                        true); ?>
                </td>
                <td>
                    <div class="btn-group">
                        <a class="btn btn-default btn-sm"
                           href="<?php echo $this->url(array('action' => 'edit-newsletter', 'id' => $newsletter['id'])); ?>"
                           title="<?php echo $this->_('Edit Newsletter'); ?>">
                            <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-newsletter', 'id' => $newsletter['id']),
                               null, true); ?>"
                           title="<?php echo $this->_('Delete Newsletter'); ?>"
                           data-message="<?php echo $this->_('Are you sure you want to delete this newsletter?'); ?>">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
</div>

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