<?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\Adminhtml\Sales\Order\Items\Renderer */ ?>

<?php $_item = $block->getItem() ?>
<?php $items = $block->getChildren($_item); ?>
<?php $_count = count($items) ?>
<?php $_index = 0 ?>

<?php $_prevOptionId = '' ?>

<?php if ($block->getOrderOptions() || $_item->getDescription()) : ?>
    <?php $_showlastRow = true ?>
<?php else : ?>
    <?php $_showlastRow = false ?>
<?php endif; ?>

<?php foreach ($items as $_item) : ?>
    <?php $block->setPriceDataObject($_item) ?>
    <?php if ($_item->getOrderItem()->getParentItem()) : ?>
        <?php $attributes = $block->getSelectionAttributes($_item) ?>
        <?php if ($_prevOptionId != $attributes['option_id']) : ?>
        <tr>
            <td class="col-product"><div class="option-label"><?= $block->escapeHtml($attributes['option_label']) ?></div></td>
            <td class="col-product">&nbsp;</td>
            <td class="col-qty last">&nbsp;</td>
        </tr>
            <?php $_prevOptionId = $attributes['option_id'] ?>
        <?php endif; ?>
    <?php endif; ?>
    <tr class="<?= (++$_index == $_count && !$_showlastRow) ? 'border' : '' ?>">
        <?php if (!$_item->getOrderItem()->getParentItem()) : ?>
        <td class="col-product">
            <div class="product-title"><?= $block->escapeHtml($_item->getName()) ?></div>
            <div class="product-sku-block">
                <span><?= $block->escapeHtml(__('SKU')) ?>:</span>
                <?= /* @noEscape */ implode('<br />', $this->helper(Magento\Catalog\Helper\Data::class)->splitSku($_item->getSku())) ?>
            </div>
        </td>
        <?php else : ?>
        <td class="col-product"><div class="option-value"><?= $block->getValueHtml($_item) ?></div></td>
        <?php endif; ?>
        <td class="col-ordered-qty">
            <?php if ($block->isShipmentSeparately($_item)) : ?>
                <?= $block->getColumnHtml($_item, 'qty') ?>
            <?php else : ?>
                &nbsp;
            <?php endif; ?>
        </td>
        <td class="col-qty last">
            <?php if ($block->isShipmentSeparately($_item)) : ?>
                <input type="text"
                       class="input-text admin__control-text"
                       name="shipment[items][<?= $block->escapeHtmlAttr($_item->getOrderItemId()) ?>]"
                       value="<?= (float)$_item->getQty() * 1 ?>" />
            <?php else : ?>
                &nbsp;
            <?php endif; ?>
        </td>
    </tr>
<?php endforeach; ?>
<?php if ($_showlastRow) : ?>
    <tr class="border">
        <td class="col-product">
            <?php if ($block->getOrderOptions($_item->getOrderItem())) : ?>
                <dl class="item-options">
                <?php foreach ($block->getOrderOptions($_item->getOrderItem()) as $option) : ?>
                    <dt><?= $block->escapeHtml($option['label']) ?></dt>
                    <dd>
                    <?php if (isset($option['custom_view']) && $option['custom_view']) : ?>
                        <?= $block->escapeHtml($option['value']) ?>
                    <?php else : ?>
                        <?= $block->escapeHtml($block->truncateString($option['value'], 55, '', $_remainder)) ?>
                        <?php if ($_remainder) :?>
                            ... <span id="<?= $block->escapeHtmlAttr($_id = 'id' . uniqid()) ?>"><?= $block->escapeHtml($_remainder) ?></span>
                            <script>
                                require(['prototype'], function(){
                                    <?php $escapedId = $block->escapeJs($_id) ?>
                                    $('<?= /* @noEscape */ $escapedId ?>').hide();
                                    $('<?= /* @noEscape */ $escapedId ?>').up().observe('mouseover', function(){$('<?= /* @noEscape */ $escapedId ?>').show();});
                                    $('<?= /* @noEscape */ $escapedId ?>').up().observe('mouseout',  function(){$('<?= /* @noEscape */ $escapedId ?>').hide();});
                                });
                            </script>
                        <?php endif;?>
                    <?php endif;?>
                    </dd>
                <?php endforeach; ?>
                </dl>
                        <?php else : ?>
                &nbsp;
                        <?php endif; ?>
            <?= $block->escapeHtml($_item->getDescription()) ?>
        </td>
        <td class="col-ordered-qty">&nbsp;</td>
        <td class="col-qty last">&nbsp;</td>
    </tr>
<?php endif; ?>
