<?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 $_order = $block->getItem()->getOrder(); ?>

<?php $items = $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
        // As part of invoice item renderer logic, the order is set on each invoice item.
        // In the case of a bundle product, this template takes over rendering its children,
        // so it is necessary to pass the order along to each child.
        $_item->setOrder($_order);
    ?>

    <?php if ($_item->getOrderItem()->getParentItem()) : ?>
        <?php $attributes = $block->getSelectionAttributes($_item) ?>
        <?php if ($_prevOptionId != $attributes['option_id']) : ?>
            <tr class="bundle-option-label">
                <td colspan="3">
                    <strong><?= $block->escapeHtml($attributes['option_label']) ?></strong>
                </td>
            </tr>
            <?php $_prevOptionId = $attributes['option_id'] ?>
        <?php endif; ?>
    <?php endif; ?>
    <?php if (!$_item->getOrderItem()->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->canShowPriceInfo($_item)) : ?>
                    <?= (float)$_item->getQty() * 1 ?>
                <?php else : ?>
                    &nbsp;
                <?php endif; ?>
            </td>
            <td class="item-price">
                <?php if ($block->canShowPriceInfo($_item)) : ?>
                    <?= /* @noEscape */ $block->getItemPrice($_item) ?>
                <?php else : ?>
                    &nbsp;
                <?php endif; ?>
            </td>
        </tr>

<?php endforeach; ?>

<?php if ($_showlastRow) : ?>
    <tr>
        <td colspan="3" 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; ?>
