<div class="wrap">
    <h1>
        <?php printf(__('Manage persons in unit "%s"', 'scout-units-list'), $this->unit->getName()); ?>
    </h1>
    <?php echo $this->partial('Partial/Messages', [
        'messages' => $this->messages,
    ]); ?>
    <div class="tablenav top">
        <div class="tablenav-pages">
            <span class="displaying-num">
                <?php $persons = $this->unit->getPersons(); $personsNumber = count($persons);
                echo sprintf(_n('%s item', '%s items', $personsNumber), $personsNumber); ?>
            </span>
        </div>
        <br class="clear">
    </div>
    <table class="wp-list-table widefat fixed striped pages sul-list">
	<thead>
            <tr>
                <th scope="col" id="name" class="manage-column column-primary"><?php _e('Position name', 'scout-units-list'); ?></th>
                <th scope="col" id="user" class="manage-column"><?php _e('User', 'scout-units-list'); ?></th>
                <th scope="col" id="leader" class="manage-column"><?php _e('Unit leader', 'scout-units-list'); ?></th>
                <th scope="col" id="order" class="manage-column"><?php _e('Order number', 'scout-units-list'); ?></th>
            </tr>
	</thead>
	<tbody id="the-list" data-delete-form-prototype="<?php echo $this->escape(str_replace([
            '%colspan%',
            '%25deleteId%25',
        ], [
            4,
            '%deletedId%',
        ], $this->deleteFormPrototype)); ?>" data-sortable-action="sul_personsSort" data-unit-id="<?php echo $this->unit->getId(); ?>">
            <?php if (count($persons) > 0): ?>
                <?php foreach ($persons as $person):
                    $position = $person->getPosition();
                    $user = $person->getUser();
                    $positionName = $position->getNameFor($user);
                    $userLink = isset($user) ? $this->getLink([
                        'user_id' => $user->getId(),
                    ], 'user-edit.php') : null;
                    $orderLink = $person->getOrderId() ? $this->getLink([
                        'item' => $person->getOrderId(),
                    ], 'upload.php') : null;
                ?>
                    <tr data-delete-form-name="<?php echo $this->escape($positionName); ?>" data-item-id="<?php echo $person->getId(); ?>">
                        <td class="name column-name has-row-actions column-primary" data-colname="<?php _e('Position name', 'scout-units-list'); ?>">
                            <strong><?php echo $this->escape($positionName); ?></strong>
                            <div class="row-actions">
                                <span class="trash"><a class="sul-delete"><?php _e('Delete', 'scout-units-list'); ?></a></span>
                            </div>
                            <button type="button" class="toggle-row"><span class="screen-reader-text"><?php _e('Show more details', 'scout-units-list'); ?></span></button>
                        </td>
                        <td class="user column-user" data-colname="<?php _e('User', 'scout-units-list'); ?>"><?php echo $this->partial('Partial/UserName', [
                            'link' => $userLink,
                            'person' => $person,
                            'user' => $user,
                        ]); ?></td>
                        <td class="leader column-leader" data-colname="<?php _e('Unit leader', 'scout-units-list'); ?>"><?php echo $position->isLeader() ? __('Yes', 'scout-units-list') : __('No', 'scout-units-list'); ?></td>
                        <td class="order column-order" data-colname="<?php _e('Order number', 'scout-units-list'); ?>"><?php if ($orderLink): ?><a href="<?php echo $orderLink; ?>" target="_blank"><?php echo $this->escape($person->getOrderNo()); ?></a><?php else: echo $this->escape($person->getOrderNo()); endif; ?></td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr class="no-items">
                    <td colspan="4" class="colspanchange"><?php _e('No persons found.', 'scout-units-list'); ?></td>
                </tr>
            <?php endif; ?>
	</tbody>
	<tfoot>
            <tr>
                <th scope="col" class="manage-column column-primary"><?php _e('Position name', 'scout-units-list'); ?></th>
                <th scope="col" class="manage-column"><?php _e('User', 'scout-units-list'); ?></th>
                <th scope="col" class="manage-column"><?php _e('Unit leader', 'scout-units-list'); ?></th>
                <th scope="col" class="manage-column"><?php _e('Order number', 'scout-units-list'); ?></th>
            </tr>
	</tfoot>
    </table>
    <div class="tablenav bottom">
        <div class="tablenav-pages">
            <span class="displaying-num">
                <?php echo sprintf(_n('%s item', '%s items', $personsNumber), $personsNumber); ?>
            </span>
        </div>
        <br class="clear">
    </div>
    <?php if (isset($this->form)): $form = $this->form; $form->start([
        'attr' => [
            'class' => 'sul-form',
        ],
    ]); ?>
        <h2>
            <?php printf(__('Add person to unit "%s"', 'scout-units-list'), $this->unit->getName()); ?>
        </h2>
        <?php $form->errors(); ?>
        <table class="form-table">
            <tbody>
                <?php $form->row('userId', 'Form/RowAdmin'); ?>
                <?php $form->row('positionId', 'Form/RowAdmin'); ?>
                <?php $form->row($this->orderCategoriesDefined ? 'orderId' : 'orderNo', 'Form/RowAdmin'); ?>
            </tbody>
        </table>
        <?php if ($this->orderCategoriesDefined): ?>
            <?php $form->widget('orderNo'); ?>
        <?php endif; ?>
        <?php $form->row('submit', 'Form/RowSubmitAdmin'); ?>
    <?php $form->end(); endif; ?>
</div>
