<?php
$escape = $this->plugin('escapeHtml');
$attachments = $block ? $block->attachments() : [];
$attachmentRowTemplate = '
 <div class="attachment">
    <span class="sortable-handle"></span>
    <ul class="actions">
        <li><a href="#" class="attachment-options-icon o-icon-configure button" aria-label="' . $escape($this->translate('Open attachment options')) . '" title="' . $escape($this->translate('Open attachment options')) . '"></a></li>
        <li class="delete"><a href="#" class="o-icon-delete button" aria-label="' . $escape($this->translate('Delete attachment')) . '" title="' .$escape($this->translate('Delete attachment')) . '"></a></li>
        <li class="undo"><a href="#" class="o-icon-undo button" aria-label="' . $escape($this->translate('Restore attachment')) . '" title="' . $escape($this->translate('Restore attachment')) . '"></a></li>
    </ul>
    <div class="item-title">'. $escape($this->translate('No item selected')) . '</div>
    <input type="hidden" class="item" name="o:block[__blockIndex__][o:attachment][__attachmentIndex__][o:item][o:id]">
    <input type="hidden" class="media" name="o:block[__blockIndex__][o:attachment][__attachmentIndex__][o:media][o:id]">
    <input type="hidden" class="caption" name="o:block[__blockIndex__][o:attachment][__attachmentIndex__][o:caption]">
</div>';
?>
<div class="attachments-form<?php echo $this->itemOnly ? ' attachments-item-only' : ''; ?>" data-item-query="<?php echo $escape(json_encode($this->itemQuery)); ?>">
    <a href="#" class="collapse"><h4><?php echo $this->translate('Attachments'); ?></h4></a>
    <div class="attachments collapsible" data-template="<?php echo $escape($attachmentRowTemplate); ?>">
        <?php foreach ($attachments as $attachment): ?>
        <?php
        $itemId = null;
        $mediaId = null;
        $thumbnail = null;
        $caption = $attachment->caption();
        $title = $this->translate('No item selected');

        $item = $attachment->item();
        if ($item) {
            $itemId = $item->id();
            $title = $item->displayTitle();
        }

        $media = $attachment->media();
        if ($media) {
            $mediaId = $media->id();
        } else if ($item) {
            $media = $item->primaryMedia();
        }
        if ($media) {
            $thumbnail = '<img src="' . $escape($media->thumbnailUrl('square')) . '" title="' .  $escape($media->displayTitle()) . '" alt="' . $escape($media->mediaType()) . ' thumbnail">';
        }
        ?>
        <div class="attachment">
            <span class="sortable-handle"></span>
            <ul class="actions">
                <?php if (!$this->itemOnly): ?><li><a href="#" class="attachment-options-icon o-icon-configure button" aria-label="<?php echo $escape($this->translate('Open attachment options')); ?>" title="<?php echo $escape($this->translate('Open attachment options')); ?>"></a></li><?php endif; ?>
                <li class="delete"><a href="#" class="o-icon-delete button" aria-label="<?php echo $escape($this->translate('Delete attachment')); ?>" title="<?php echo $escape($this->translate('Delete attachment')); ?>"></a></li>
                <li class="undo"><a href="#" class="o-icon-undo button" aria-label="<?php echo $escape($this->translate('Restore attachment')); ?>" title="<?php echo $escape($this->translate('Restore attachment')); ?>"></a></li>
            </ul>
            <div class="item-title"><?php echo $thumbnail . $escape($title); ?></div>
            <input type="hidden" class="item" name="o:block[__blockIndex__][o:attachment][__attachmentIndex__][o:item][o:id]" value="<?php echo $escape($itemId); ?>">
            <input type="hidden" class="media" name="o:block[__blockIndex__][o:attachment][__attachmentIndex__][o:media][o:id]" value="<?php echo $escape($mediaId); ?>">
            <input type="hidden" class="caption" name="o:block[__blockIndex__][o:attachment][__attachmentIndex__][o:caption]" value="<?php echo $escape($caption); ?>">
        </div>
        <?php endforeach; ?>
        <button type="button" class="attachment-add"><?php echo $this->translate('Add Attachment'); ?></button>
    </div>
</div>
