<div class="wrap">
    <h1>
        <?php _e('Units list', 'scout-units-list'); ?>
        <?php if (current_user_can('sul_manage_units')): ?>
            <a href="<?php $this->link(['action' => 'admin-form']); ?>" class="page-title-action">
                <?php _e('Add new', 'scout-units-list'); ?>
            </a>
        <?php endif; ?>
    </h1>
    <?php echo $this->partial('Partial/Messages', [
        'messages' => $this->messages,
    ]); ?>
    <div class="tablenav top">
        <?php echo $this->partial('Partial/PaginationAdmin', [
            'packNo' => $this->units->packNo,
            'packsCount' => $this->units->packsCount,
            'totalSize' => $this->units->totalSize,
        ]); ?>
        <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('Unit name', 'scout-units-list'); ?></th>
                <th scope="col" id="type" class="manage-column"><?php _e('Type', 'scout-units-list'); ?></th>
                <th scope="col" id="parent" class="manage-column"><?php _e('Parent unit', 'scout-units-list'); ?></th>
                <th scope="col" id="completeness" class="manage-column"><?php _e('Completeness', '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)); ?>">
            <?php if (count($this->units) > 0): ?>
                <?php foreach ($this->units as $unit):
                    $typeName = ScoutUnitsList\Form\UnitAdminForm::getTypeName($unit->getType());
                    $configureLink = $this->getLink([
                        'action' => 'leader-form',
                        'id' => $unit->getId(),
                    ]);
                    $managePersonsLink = $this->getLink([
                        'action' => 'person-manage',
                        'id' => $unit->getId(),
                    ]);
                    $editLink = $this->getLink([
                        'action' => 'admin-form',
                        'id' => $unit->getId(),
                    ]);
                    $parentUnit = $unit->getParent();
                ?>
                    <tr data-delete-form-name="<?php echo $this->escape($unit->getName()); ?>" data-item-id="<?php echo $unit->getId(); ?>">
                        <td class="name column-name has-row-actions column-primary" data-colname="<?php _e('Unit name', 'scout-units-list'); ?>">
                            <strong><a href="<?php echo $configureLink; ?>" aria-label="<?php echo sprintf(__('&#8220;%s&#8221; (Edit)', 'scout-units-list'), $unit->getName()); ?>"><?php echo $this->escape($unit->getName()); ?></a></strong>
                            <div class="row-actions">
                                <span class="configure"><a href="<?php echo $configureLink; ?>"><?php _e('Configure', 'scout-units-list'); ?></a></span>
                                <?php if (current_user_can('sul_manage_persons')): ?>
                                    | <span class="manage-persons"><a href="<?php echo $managePersonsLink; ?>"><?php _e('Manage persons', 'scout-units-list'); ?></a></span>
                                <?php endif; ?>
                                <?php if (current_user_can('sul_manage_units')): ?>
                                    | <span class="edit"><a href="<?php echo $editLink; ?>"><?php _e('Edit', 'scout-units-list'); ?></a></span>
                                    | <span class="trash"><a class="sul-delete"><?php _e('Delete', 'scout-units-list'); ?></a></span>
                                <?php endif; ?>
                            </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="type column-type" data-colname="<?php _e('Type', 'scout-units-list'); ?>"><?php echo $this->escape($typeName); ?></td>
                        <td class="parent column-parent" data-colname="<?php _e('Parent unit', 'scout-units-list'); ?>"><?php echo isset($parentUnit) ? $this->escape($parentUnit->getName()) : ''; ?></td>
                        <td class="completeness column-completeness" data-colname="<?php _e('Completeness', 'scout-units-list'); ?>"><?php echo round($unit->getCompletenessRatio() * 100); ?>%</td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr class="no-items">
                    <td colspan="4" class="colspanchange"><?php _e('No units found.', 'scout-units-list'); ?></td>
                </tr>
            <?php endif; ?>
	</tbody>
	<tfoot>
            <tr>
                <th scope="col" class="manage-column column-primary"><?php _e('Unit name', 'scout-units-list'); ?></th>
                <th scope="col" class="manage-column"><?php _e('Type', 'scout-units-list'); ?></th>
                <th scope="col" class="manage-column"><?php _e('Parent unit', 'scout-units-list'); ?></th>
                <th scope="col" class="manage-column"><?php _e('Completeness', 'scout-units-list'); ?></th>
            </tr>
	</tfoot>
    </table>
    <div class="tablenav bottom">
        <?php echo $this->partial('Partial/PaginationAdmin', [
            'packNo' => $this->units->packNo,
            'packsCount' => $this->units->packsCount,
            'totalSize' => $this->units->totalSize,
        ]); ?>
        <br class="clear">
    </div>
</div>
