<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Marketplace
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
?>
<?php $items = $block->getItems(); ?>
<div class="table-wrapper order-items">
    <table class="data table table-order-items" id="my-orders-table" summary="<?= $block->escapeHtml(__('Items Shipped')) ?>">
        <caption class="table-caption"><?= $block->escapeHtml(__('Items Shipped')) ?></caption>
        <thead>
            <?php if ($block->isPagerDisplayed()): ?>
                <tr>
                    <td colspan="<?= $block->escapeHtml($colSpan) ?>" data-block="order-items-pager-top" class="order-pager-wrapper order-pager-wrapper-top">
                        <?= $block->getPagerHtml() ?>
                    </td>
                </tr>
            <?php endif ?>
            <tr>
                <th class="col name"><?= $block->escapeHtml(__('Product Name')) ?></th>
                <th class="col sku"><?= $block->escapeHtml(__('SKU')) ?></th>
                <th class="col qty"><?= $block->escapeHtml(__('Qty Shipped')) ?></th>
            </tr>
        </thead>
        <?php
        $i = 0;
        $count = count($items);
        foreach ($items as $item) {
            if ($item->getParentItem()) {
                continue;
            }
            $i++;
            $result = [];
            if ($options = $item->getProductOptions()) {
                if (isset($options['options'])) {
                    $result = array_merge($result, $options['options']);
                }
                if (isset($options['additional_options'])) {
                    $result = array_merge($result, $options['additional_options']);
                }
                if (isset($options['attributes_info'])) {
                    $result = array_merge($result, $options['attributes_info']);
                }
            }
            // for bundle product
            $bundleitems = array_merge([$item], $item->getChildrenItems());
            $count = count($bundleitems);
            $index = 0;
            $prevOptionId = '';
            ?>
            <tbody>
                <?php
                if ($item->getProductType()!='bundle') { ?>
                    <tr class="border" id="order-item-row-<?= $block->escapeHtml($item->getId()) ?>">
                        <?php
                        if ($item->getProductType()=='downloadable') { ?>
                            <td class="col name" data-th="<?= $block->escapeHtml(__('Product Name')); ?>">
                                <strong class="product name product-item-name"><?= $block->escapeHtml($item->getName()) ?></strong>
                                <?php if ($options = $block->getItemOptions($item)): ?>
                                    <dl class="item-options links">
                                        <?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'])): ?>
                                                        <?= /* @noEscape */ $formatedOptionValue['full_view'] ?>
                                                    <?php else: ?>
                                                        <?= /* @noEscape */ $formatedOptionValue['value'] ?>
                                                    <?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; ?>
                                <?php /* downloadable */?>
                                <?php if ($links = $block->getDownloadableLinks($item->getId())): ?>
                                    <dl class="item-options links">
                                        <dt><?= $block->escapeHtml($block->getLinksTitle($item->getId())) ?></dt>
                                        <?php foreach ($links->getPurchasedItems() as $link): ?>
                                            <dd><?= $block->escapeHtml($link->getLinkTitle()); ?></dd>
                                        <?php endforeach; ?>
                                    </dl>
                                <?php endif; ?>
                                <?php /* EOF downloadable */?>

                                <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
                                <?php if ($addInfoBlock):?>
                                    <?= $addInfoBlock->setItem($item->getOrderItem())->toHtml(); ?>
                                <?php endif; ?>
                                <?= $block->escapeHtml($item->getDescription()) ?>
                                <?php $addInfoBlock = $block->getOrderItemAdditionalInfoBlock(); ?>
                                <?php if ($addInfoBlock): ?>
                                    <?= $addInfoBlock->setItem($item)->toHtml() ?>
                                <?php endif;?>
                            </td>
                            <?php
                        } else { ?>
                            <td class="col name" data-th="<?= $block->escapeHtml(__('Product Name')); ?>">
                                <strong class="product name product-item-name"><?= $block->escapeHtml($item->getName()) ?></strong>
                                <?php if ($options = $result): ?>
                                    <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'])): ?>
                                                    <?= /* @noEscape */ $formatedOptionValue['full_view'] ?>
                                                <?php else: ?>
                                                    <?= /* @noEscape */ $formatedOptionValue['value'] ?>
                                                <?php endif; ?>
                                            </dd>
                                        <?php else: ?>
                                            <dd>
                                                <?= /* @noEscape */ nl2br($block->escapeHtml((isset($option['print_value']) ? $option['print_value'] : $option['value']))) ?>
                                            </dd>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                    </dl>
                                <?php endif; ?>
                                <?php $addInfoBlock = $block->getOrderItemAdditionalInfoBlock(); ?>
                                <?php if ($addInfoBlock): ?>
                                    <?= $addInfoBlock->setItem($item)->toHtml() ?>
                                <?php endif;?>
                            </td>
                        <?php }?>
                        <td class="col sku" data-th="<?= $block->escapeHtml(__('SKU')); ?>"><?= $block->escapeHtml($item->getSku()) ?></td>
                        <td class="col qty" data-th="<?= $block->escapeHtml(__('Qty Shipped')); ?>">
                            <?= $block->escapeHtml($item->getQtyShipped()*1) ?>
                        </td>
                    </tr>
                    <?php
                } else { ?>
                    <?php foreach ($bundleitems as $bundleitem): ?>
                        <tr<?= (++$index==$count)?' class="border bundle"':' class="bundle"' ?> id="order-item-row-<?= $block->escapeHtml($bundleitem->getId()) ?>">
                            <td class="col value" data-th="<?= $block->escapeHtml(__('Product Name')); ?>">
                                <strong class="product name product-item-name">
                                    <?= $block->escapeHtml($bundleitem->getName()) ?>
                                </strong>
                                <?php
                                $result = [];
                                if ($options = $bundleitem->getProductOptions()) {
                                    if (isset($options['options'])) {
                                        $result = array_merge($result, $options['options']);
                                    }
                                    if (isset($options['additional_options'])) {
                                        $result = array_merge($result, $options['additional_options']);
                                    }
                                    if (isset($options['attributes_info'])) {
                                        $result = array_merge($result, $options['attributes_info']);
                                    }
                                    if (isset($options['bundle_options'])) {
                                        $result = array_merge($result, $options['bundle_options']);
                                    }
                                }
                                ?>
                                <?php if ($options = $result): ?>
                                    <dl class="item-options">
                                    <?php foreach ($options as $option): ?>
                                        <dt><?= $block->escapeHtml($option['label']) ?></dt>
                                        <?php foreach ($option['value'] as $optionValue) { ?>
                                            <dd>
                                                <?= /* @noEscape */ $optionValue['qty'].' x '.$optionValue['title']?>
                                            </dd>
                                        <?php } ?>
                                    <?php endforeach; ?>
                                    </dl>
                                <?php endif; ?>
                                <?php $addInfoBlock = $block->getOrderItemAdditionalInfoBlock(); ?>
                                <?php if ($addInfoBlock): ?>
                                    <?= $addInfoBlock->setItem($bundleitem)->toHtml() ?>
                                <?php endif;?>
                            </td>
                            <td class="col sku" data-th="<?= $block->escapeHtml(__('SKU')); ?>"><?= $block->escapeHtml($bundleitem->getSku()) ?></td>
                            <td class="col qty item_status" data-th="<?= $block->escapeHtml(__('Qty Shipped')) ?>">
                                <?= $block->escapeHtml($bundleitem->getQtyShipped()*1) ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php
                } ?>
            </tbody>
            <?php
        } ?>
        <tfoot>
            <?php if ($block->isPagerDisplayed()): ?>
                <tr>
                    <td colspan="<?= /* @noEscape */ $colSpan ?>" data-block="order-items-pager-bottom" class="order-pager-wrapper order-pager-wrapper-bottom">
                        <?= $block->getPagerHtml() ?>
                    </td>
                </tr>
            <?php endif ?>
            <?= $block->getChildHtml('marketplace_order_totals') ?>
        </tfoot>
    </table>
</div>
