<?php
/**
 * Copyright 2014 Adobe
 * All Rights Reserved.
 */
// phpcs:disable Magento2.Templates.ThisInTemplate
// phpcs:disable Generic.Files.LineLength
?>
<?php
/**
 * @var \Magento\Bundle\Block\Sales\Order\Items\Renderer $block
 * @var \Magento\Framework\Locale\LocaleFormatter $localeFormatter
 */
?>

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

<?php $_prevOptionId = '' ?>

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

    <?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 if ($_item->getParentItem()): ?>
        <?php $attributes = $block->getSelectionAttributes($_item) ?>
        <?php if ($_prevOptionId != $attributes['option_id']): ?>
            <tr class="options-label">
                <td colspan="3" class="col label"><div class="option label"><?= $block->escapeHtml($attributes['option_label']) ?></div></td>
            </tr>
            <?php $_prevOptionId = $attributes['option_id'] ?>
        <?php endif; ?>
    <?php endif; ?>
    <tr id="order-item-row-<?= $block->escapeHtmlAttr($_item->getId()) ?>" class="<?php if ($_item->getParentItem()): ?>item-options-container<?php else: ?>item-parent<?php endif; ?>"<?php if ($_item->getParentItem()): ?> data-th="<?= $block->escapeHtmlAttr($attributes['option_label']) ?>"<?php endif; ?>>
        <?php if (!$_item->getParentItem()): ?>
            <td class="col name" data-th="<?= $block->escapeHtmlAttr(__('Product Name')) ?>">
                <strong class="product name product-item-name"><?= $block->escapeHtml($_item->getName()) ?></strong>
            </td>
        <?php else: ?>
            <td class="col value" data-th="<?= $block->escapeHtmlAttr(__('Product Name')) ?>"><?= $block->getValueHtml($_item) ?></td>
        <?php endif; ?>
        <td class="col sku" data-th="<?= $block->escapeHtmlAttr(__('SKU')) ?>"><?= $block->escapeHtml($_item->getSku()) ?></td>
        <td class="col qty" data-th="<?= $block->escapeHtmlAttr(__('Qty Shipped')) ?>">
            <?php if (($block->isShipmentSeparately() && $_item->getParentItem()) || (!$block->isShipmentSeparately() && !$_item->getParentItem())): ?>
                <?php if (isset($shipItems[$_item->getId()])): ?>
                    <?= /* @noEscape */ $localeFormatter->formatNumber((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 && (($_options = $block->getItemOptions()) || $block->escapeHtml($_item->getDescription()))): ?>
    <tr>
        <td class="col options" colspan="3">
            <?php if ($_options = $block->getItemOptions()): ?>
                <dl class="item-options">
                    <?php foreach ($_options as $_option): ?>
                        <dt><?= $block->escapeHtml($_option['label']) ?></dt>
                        <?php if (!$block->getPrintStatus()): ?>
                            <?php $_formatedOptionValue = $block->getFormatedOptionValue($_option) ?>
                            <dd<?php if (isset($_formatedOptionValue['full_view'])): ?> class="tooltip wrapper"<?php endif; ?>>
                                <?= /* @noEscape */ $_formatedOptionValue['value'] ?>
                                <?php if (isset($_formatedOptionValue['full_view'])): ?>
                                    <div class="tooltip content">
                                        <dl class="item options">
                                            <dt><?= $block->escapeHtml($_option['label']) ?></dt>
                                            <dd><?= /* @noEscape */ $_formatedOptionValue['full_view'] ?></dd>
                                        </dl>
                                    </div>
                                <?php endif; ?>
                            </dd>
                        <?php else: ?>
                            <dd><?= $block->escapeHtml((isset($_option['print_value']) ? $_option['print_value']: $_option['value'])) ?></dd>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </dl>
            <?php endif; ?>
            <?= $block->escapeHtml($_item->getDescription()) ?>
        </td>
    </tr>
<?php endif; ?>
