<?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"
               href="<?php echo $this->url(array('controller' => 'custom-fields', 'action' => 'add', 'type' => $this->type)); ?>">
                <?php echo $this->_('Create Custom Field'); ?>
            </a>
        </div>

        <form class="navbar-form navbar-right"
              action="<?php echo $this->url(null, null, true, array('label', 'page', 'submit')); ?>" method="get">
            <div class="form-group">
                <?php echo $this->formElement('text', 'label', $this->label)
                    ->setAttributes(array('placeholder' => $this->_('Label'), 'class' => 'form-control input-medium'))
                    ->render();
                ?>
            </div>
            <?php echo $this->formElement('submit', 'submit', 'Search')
                ->setAttributes(array('class' => 'btn btn-default'))
                ->render();
            ?>
        </form>
    </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->_('custom fields 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->_('Custom Field Preview'); ?></th>
                        <th class="size-mini"><?php echo $this->_('Order ID'); ?></th>
                        <th class="size-tiny"><?php echo $this->_('Active'); ?></th>
                        <th class="size-mini"><?php echo $this->_('Options'); ?></th>
                    </tr>
                    </thead>
                <?php } ?>
                <tbody>
                <?php
                $checkboxCounter = 0;
                foreach ($this->paginator as $item) { ?>
                    <tr>
                        <td>
                            <?php echo $this->partial('partials/form-element.phtml', array(
                                'element' => $this->formElement($item, 'custom_field_' . $item['id']),
                            )); ?>
                            <div class="row">
                                <div class="col-sm-9 col-sm-offset-3">
                                    <div>
                                        <?php if ($item['product_attribute']) { ?>
                                            <span class="label label-success">
                                                <?php echo $this->_('Product Attribute'); ?>
                                            </span>
                                        <?php }
                                        else if ($item['required']) { ?>
                                            <span class="label label-primary">
                                                <?php echo $this->_('Required'); ?>
                                            </span>
                                        <?php } ?>

                                        <?php if ($item['searchable']) { ?>
                                            <span class="label label-info">
                                                <?php echo $this->_('Searchable'); ?>
                                            </span>
                                        <?php } ?>
                                    </div>

                                    <div>
                                        <small>
                                            <strong><?php echo $this->_('Categories:'); ?></strong>

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

                                                echo implode('; ', $multiOptions);
                                            }
                                            else {
                                                echo $this->_('All Categories');
                                            } ?>
                                        </small>
                                    </div>

                                </div>
                            </div>
                        </td>
                        <td>
                            <?php echo $this->formElement('text', 'order_id')
                                ->setAttributes(array('class' => 'form-control'))
                                ->setMultiple()
                                ->setValue($item['order_id'])
                                ->render();
                            ?>
                        </td>
                        <td>
                            <?php echo $this->formElement('hidden', 'id')
                                ->setMultiple()
                                ->setValue($item['id'])
                                ->render();
                            ?>
                            <?php echo $this->formElement('checkbox', 'active')
                                ->setValue($item['active'])
                                ->setMultiOptions(array(1 => null))
                                ->setBrackets('[' . $checkboxCounter . ']')
                                ->setMultiple()
                                ->render();
                            ?>
                        </td>

                        <td>
                            <div class="btn-group">
                                <a class="btn btn-default btn-sm"
                                   href="<?php echo $this->url(array('controller' => 'custom-fields', 'action' => 'edit', 'id' => $item['id'])); ?>"
                                   title="<?php echo $this->_('Edit Custom Field'); ?>">
                                    <i class="fa fa-folder-open"></i>
                                </a>
                                <a class="btn btn-danger btn-sm confirm-box"
                                   href="<?php echo $this->url(array('controller' => 'custom-fields', 'action' => 'delete', 'id' => $item['id']), null, true); ?>"
                                   title="<?php echo $this->_('Delete Custom Field'); ?>"
                                   data-message="<?php echo $this->_('Are you sure that you want to delete this custom field?'); ?>">
                                    <i class="fa fa-times"></i>
                                </a>
                            </div>
                        </td>
                    </tr>
                    <?php $checkboxCounter++; ?>
                <?php } ?>
                <tr>
                    <td colspan="4">
                        <?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'); ?>