<?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 $order = $block->getOrder() ?>
<?php $items = $block->getItems(); ?>
<?php
$paymentCode = '';
$colSpan = 8;
if ($order->getPayment()) {
    $colSpan = 9;
    $paymentCode = $order->getPayment()->getMethod();
}
?>
<div class="table-wrapper order-items">
    <table class="data table table-order-items" id="my-orders-table" summary="<?= $block->escapeHtml(__('Items Ordered')) ?>">
        <caption class="table-caption"><?= $block->escapeHtml(__('Items Ordered')) ?></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 price"><?= $block->escapeHtml(__('Price')) ?></th>
                <th class="col qty"><?= $block->escapeHtml(__('Qty')) ?></th>
                <th class="col price"><?= $block->escapeHtml(__('Total Price')) ?></th>
                <?php
                if ($paymentCode == 'mpcashondelivery') {?>
                    <th class="col price"><?= $block->escapeHtml(__('COD Charges')) ?></th>
                    <?php
                } ?>
                <th class="col price"><?= $block->escapeHtml(__('Admin Commission')) ?></th>
                <th class="col price"><?= $block->escapeHtml(__('Vendor Total')) ?></th>
                <th class="col price"><?= $block->escapeHtml(__('Subtotal')) ?></th>
            </tr>
        </thead>
        <?php
        $i = 0;
        $count = count($items);
        foreach ($items as $item) {
            if ($item->getParentItem()) {
                continue;
            }
            $i++;
            $rowTotal = $item->getMageproPrice() * $item->getQtyOrdered();
            $sellerItemCost = $item->getActualSellerAmount();
            $sellerItemCommission = $item->getTotalCommission();
            $currencyRate = $item->getCurrencyRate();

            $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><?= /* @escapeNotVerified */ $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 price" data-th="<?= $block->escapeHtml(__('Price')); ?>">
                            <span class="price-excluding-tax" data-label="<?= $block->escapeHtml(__('Excl. Tax')); ?>">
                                <span class="cart-price">
                                    <?= /* @noEscape */ $order->formatPrice($item->getPrice()); ?>
                                </span>
                            </span>
                        </td>
                        <td class="col qty" data-th="<?= $block->escapeHtml(__('Qty')); ?>">
                            <ul class="items-qty">
                                <?php if ($item->getQtyOrdered() > 0): ?>
                                    <li class="item">
                                        <span class="title"><?= $block->escapeHtml(__('Ordered')); ?></span>
                                        <span class="content"><?= $block->escapeHtml($item->getQtyOrdered()*1) ?></span>
                                    </li>
                                <?php endif; ?>
                                <?php if ($item->getQtyInvoiced() > 0): ?>
                                    <li class="item">
                                        <span class="title"><?= $block->escapeHtml(__('Invoiced')); ?></span>
                                        <span class="content"><?= $block->escapeHtml($item->getQtyInvoiced()*1) ?></span>
                                    </li>
                                <?php endif; ?>
                                <?php if ($item->getQtyShipped() > 0): ?>
                                    <li class="item">
                                        <span class="title"><?= $block->escapeHtml(__('Shipped')); ?></span>
                                        <span class="content"><?= $block->escapeHtml($item->getQtyShipped()*1) ?></span>
                                    </li>
                                <?php endif; ?>
                                <?php if ($item->getQtyCanceled() > 0): ?>
                                    <li class="item">
                                        <span class="title"><?= $block->escapeHtml(__('Canceled')); ?></span>
                                        <span class="content"><?= $block->escapeHtml($item->getQtyCanceled()*1) ?></span>
                                    </li>
                                <?php endif; ?>
                                <?php if ($item->getQtyRefunded() > 0): ?>
                                    <li class="item">
                                        <span class="title"><?= $block->escapeHtml(__('Refunded')); ?></span>
                                        <span class="content"><?= $block->escapeHtml($item->getQtyRefunded()*1) ?></span>
                                    </li>
                                <?php endif; ?>
                            </ul>
                        </td>
                        <td class="col price" data-th="<?= $block->escapeHtml(__('Total Price')); ?>"><?= /* @noEscape */ $order->formatPrice($block->getOrderedPricebyorder($currencyRate, $rowTotal));?></td>
                        <?php
                        if ($paymentCode == 'mpcashondelivery') {?>
                            <td class="col price" data-th="<?= $block->escapeHtml(__('COD Charges')) ?>"><?= /* @noEscape */ $order->formatPrice($block->getOrderedPricebyorder($currencyRate, $item->getCodCharges()));?></td>
                            <?php
                        } ?>
                        <td class="col price" data-th="<?= $block->escapeHtml(__('Admin Commission')); ?>"><?= /* @noEscape */ $order->formatPrice($block->getOrderedPricebyorder($currencyRate, $sellerItemCommission));?></td>
                        <td class="col price" data-th="<?= $block->escapeHtml(__('Vendor Total')); ?>"><?= /* @noEscape */ $order->formatPrice($block->getOrderedPricebyorder($currencyRate, $sellerItemCost));?></td>
                        <td class="col subtotal" data-th="<?= $block->escapeHtml(__('Subtotal')); ?>"><?= /* @noEscape */ $order->formatPrice($block->getOrderedPricebyorder($currencyRate, $rowTotal));?></td>
                    </tr>
                    <?php
                } else { ?>
                    <?php foreach ($bundleitems as $bundleitem): ?>
                        <?php
                        $bundleItemTotals = $block->getSellerOrdersList(
                            $order->getId(),
                            $bundleitem->getProductId(),
                            $bundleitem->getId()
                        );
                        $codCharges = 0;
                        foreach ($bundleItemTotals as $bundleItemTotal) {
                            $rowTotal = $bundleItemTotal->getMageproPrice() * $bundleitem->getQtyOrdered();
                            $sellerItemCost = $bundleItemTotal->getActualSellerAmount();
                            $sellerItemCommission = $bundleItemTotal->getTotalCommission();
                            $currencyRate = $bundleItemTotal->getCurrencyRate();
                            if ($paymentCode == 'mpcashondelivery') {
                                $codCharges = $bundleItemTotal->getCodCharges();
                            }
                        }
                        ?>
                        <?php $attributes_option = $block->getSelectionAttribute($bundleitem); ?>
                        <?php if ($bundleitem->getParentItem()): ?>
                            <?php $attributes = $attributes_option ?>
                            <?php if ($prevOptionId != $attributes['option_id']): ?>
                            <tr class="options-label">
                                <td class="col label" colspan="<?= /* @noEscape */ $colSpan ?>" data-th="<?= $block->escapeHtml(__('Product Name')); ?>">
                                    <?= /* @noEscape */ $attributes['option_label'] ?>
                                </td>
                            </tr>
                                <?php $prevOptionId = $attributes['option_id'] ?>
                            <?php endif; ?>
                        <?php endif; ?>
                        <tr<?= (++$index==$count)?' class="border bundle"':' class="bundle"' ?> id="order-item-row-<?= $block->escapeHtml($bundleitem->getId()) ?>">
                            <?php
                            if (!$bundleitem->getParentItem()) {?>
                                <td><h3 class="product-name"><?= $block->escapeHtml($bundleitem->getName()) ?></h3></td>
                                <td class="col sku" data-th="<?= $block->escapeHtml(__('SKU')); ?>"><?= $block->escapeHtml($bundleitem->getSku()) ?></td>
                                <td class="col price" data-rwd-label="<?= $block->escapeHtml(__('Price')) ?>">
                                    <span class="price-excluding-tax" data-label="<?= $block->escapeHtml(__('Excl. Tax')); ?>">
                                        <?= /* @noEscape */ $order->formatPrice($item->getPrice());?>
                                    </span>
                                </td>
                                <td class="col qty item_status" data-rwd-label="<?= $block->escapeHtml(__('Status')) ?>">
                                    <ul class="items-qty">
                                        <?php if ($item->getQtyOrdered() > 0): ?>
                                            <li class="item">
                                                <span class="title"><?= $block->escapeHtml(__('Ordered')); ?></span>
                                                <span class="content"><?= $block->escapeHtml($item->getQtyOrdered()*1) ?></span>
                                            </li>
                                        <?php endif; ?>
                                    </ul>
                                </td>
                                <td class="lin-hide">&nbsp;</td>
                                <td class="lin-hide">&nbsp;</td>
                                <td class="lin-hide">&nbsp;</td>
                                <td class="lin-hide">&nbsp;</td>
                                <?php
                            } else { ?>
                                <?php
                                $i++;
                                ?>
                                <td class="col value" data-th="<?= $block->escapeHtml(__('Product Name')); ?>">
                                    <?= $block->getValueHtml($bundleitem)?>
                                    <?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 price" data-rwd-label="<?= $block->escapeHtml(__('Price')) ?>"><?= /* @noEscape */ $order->formatPrice($bundleitem->getPrice());?></td>
                                <td class="col qty item_status" data-rwd-label="<?= $block->escapeHtml(__('Status')) ?>">
                                    <ul class="items-qty">
                                        <?php if ($bundleitem->getQtyOrdered() > 0): ?>
                                            <li class="item">
                                                <span class="title"><?= $block->escapeHtml(__('Ordered')); ?></span>
                                                <span class="content"><?= $block->escapeHtml($bundleitem->getQtyOrdered()*1) ?></span>
                                            </li>
                                        <?php endif; ?>
                                        <?php if ($bundleitem->getQtyInvoiced() > 0): ?>
                                            <li class="item">
                                                <span class="title"><?= $block->escapeHtml(__('Invoiced')); ?></span>
                                                <span class="content"><?= $block->escapeHtml($bundleitem->getQtyInvoiced()*1) ?></span>
                                            </li>
                                        <?php endif; ?>
                                        <?php if ($bundleitem->getQtyShipped() > 0 && !$block->isShipmentSeparately()): ?>
                                            <li class="item">
                                                <span class="title"><?= $block->escapeHtml(__('Shipped')); ?></span>
                                                <span class="content"><?= $block->escapeHtml($bundleitem->getQtyShipped()*1) ?></span>
                                            </li>
                                        <?php endif; ?>
                                        <?php if ($bundleitem->getQtyCanceled() > 0): ?>
                                            <li class="item">
                                                <span class="title"><?= $block->escapeHtml(__('Canceled')); ?></span>
                                                <span class="content"><?= $block->escapeHtml($bundleitem->getQtyCanceled()*1) ?></span>
                                            </li>
                                        <?php endif; ?>
                                        <?php if ($bundleitem->getQtyRefunded() > 0): ?>
                                            <li class="item">
                                                <span class="title"><?= $block->escapeHtml(__('Refunded')); ?></span>
                                                <span class="content"><?= $block->escapeHtml($bundleitem->getQtyRefunded()*1) ?></span>
                                            </li>
                                        <?php endif; ?>
                                    </ul>
                                </td>
                                <td class="col price" data-th="<?= $block->escapeHtml(__('Total Price')); ?>"><?= /* @noEscape */ $order->formatPrice($block->getOrderedPricebyorder($currencyRate, $rowTotal));?></td>
                                <?php
                                if ($paymentCode == 'mpcashondelivery') {?>
                                    <td class="col price" data-th="<?= $block->escapeHtml(__('COD Charges')) ?>"><?= /* @noEscape */ $order->formatPrice($block->getOrderedPricebyorder($currencyRate, $codCharges));?></td>
                                    <?php
                                } ?>
                                <td class="col price" data-th="<?= $block->escapeHtml(__('Admin Commission')); ?>"><?= /* @noEscape */ $order->formatPrice($block->getOrderedPricebyorder($currencyRate, $sellerItemCommission));?></td>
                                <td class="col price" data-th="<?= $block->escapeHtml(__('Vendor Total')); ?>"><?= /* @noEscape */ $order->formatPrice($block->getOrderedPricebyorder($currencyRate, $sellerItemCost));?></td>
                                <td class="col subtotal" data-th="<?= $block->escapeHtml(__('Subtotal')); ?>"><?= /* @noEscape */ $order->formatPrice($block->getOrderedPricebyorder($currencyRate, $rowTotal));?></td>
                                <?php
                            }?>
                        </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>
