<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis
?>
<?php /** @var $block \Magento\Bundle\Block\Sales\Order\Items\Renderer */ ?>

<?php $parentItem = $block->getItem() ?>

<?php $items = array_merge([$parentItem->getOrderItem()], $parentItem->getOrderItem()->getChildrenItems()) ?>
<?php $shipItems = $block->getChildren($parentItem) ?>

<?php if ($block->getItemOptions() || $parentItem->getDescription() || $this->helper(Magento\GiftMessage\Helper\Message::class)->isMessagesAllowed('order_item', $parentItem) && $parentItem->getGiftMessageId()) : ?>
    <?php $_showlastRow = true ?>
<?php else : ?>
    <?php $_showlastRow = false ?>
<?php endif; ?>

<?php $_prevOptionId = '' ?>

<?php foreach ($items as $_item) : ?>

    <?php if ($_item->getParentItem()) : ?>
        <?php $attributes = $block->getSelectionAttributes($_item) ?>
        <?php if ($_prevOptionId != $attributes['option_id']) : ?>
            <tr class="bundle-option-label">
                <td colspan="2">
                    <strong><em><?= $block->escapeHtml($attributes['option_label']) ?></em></strong>
                </td>
            </tr>
            <?php $_prevOptionId = $attributes['option_id'] ?>
        <?php endif; ?>
    <?php endif; ?>
    <?php if (!$_item->getParentItem()) : ?>
        <tr class="bundle-item bundle-parent">
            <td class="item-info">
                <p class="product-name"><?= $block->escapeHtml($_item->getName()) ?></p>
                <p class="sku"><?= $block->escapeHtml(__('SKU')) ?>: <?= $block->escapeHtml($block->getSku($_item)) ?></p>
            </td>
    <?php else : ?>
        <tr class="bundle-item bundle-option-value">
            <td class="item-info">
                <p><?= $block->getValueHtml($_item) ?></p>
            </td>
    <?php endif; ?>
            <td class="item-qty">
                <?php if (($block->isShipmentSeparately() && $_item->getParentItem()) || (!$block->isShipmentSeparately() && !$_item->getParentItem())) : ?>
                    <?php if (isset($shipItems[$_item->getId()])) : ?>
                        <?= (float)$shipItems[$_item->getId()]->getQty() * 1 ?>
                    <?php elseif ($_item->getIsVirtual()) : ?>
                        <?= $block->escapeHtml(__('N/A')) ?>
                    <?php else : ?>
                        0
                    <?php endif; ?>
                <?php else : ?>
                    &nbsp;
                <?php endif; ?>
            </td>
        </tr>

<?php endforeach; ?>

<?php if ($_showlastRow) : ?>
    <tr>
        <td colspan="2" class="item-extra">
            <?php if ($block->getItemOptions()) : ?>
            <dl>
                <?php foreach ($block->getItemOptions() as $option) : ?>
                <dt><strong><em><?= $block->escapeHtml($option['label']) ?></em></strong></dt>
                            <dd><?= $block->escapeHtml($option['value']) ?></dd>
                <?php endforeach; ?>
            </dl>
            <?php endif; ?>
            <?= $block->escapeHtml($_item->getDescription()) ?>
        </td>
    </tr>
<?php endif; ?>
