<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\Escaper;
use Magento\GiftMessage\Helper\Message as GiftMessageHelper;
use Magento\Sales\Block\Order\Items;

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundHelper

/** @var Items $block */
/** @var Escaper $escaper */

$helper = $this->helper(GiftMessageHelper::class);
$cols = $block->getData('is_context_shipment') ? 3 : 5;
?>
<div class="order-items">
    <?php if ($block->isPagerDisplayed()): ?>
        <?= $block->getPagerHtml() ?>
    <?php endif ?>
    <div class="hidden grid-cols-<?= $escaper->escapeHtmlAttr($cols) ?> text-sm text-secondary mt-2">
        <div class="p-2 col-span-2"><?= $escaper->escapeHtml(__('Product Name')) ?></div>
        <?php if (!$block->getData('is_context_shipment')): ?>
            <div class="p-2"><?= $escaper->escapeHtml(__('Price')) ?></div><?php endif; ?>
        <div class="p-2"><?= $escaper->escapeHtml(__('Qty')) ?></div>
        <?php if (!$block->getData('is_context_shipment')): ?>
            <div class="p-2 text-right"><?= $escaper->escapeHtml(__('Subtotal')) ?></div><?php endif; ?>
    </div>
    <?php $items = $block->getOrder()->getAllItems(); ?>
    <?php $giftMessage = '' ?>
    <?php foreach ($items as $item): ?>
        <?php
        $parentItem = $item->getParentItem();
        if ($parentItem && $parentItem->getData('product_type') === Configurable::TYPE_CODE) {
            continue;
        }
        ?>
        <div class="<?= ($parentItem) ?
            'child-item ml-2 px-4 mb-2 border-l-4 border-container text-sm' :
            'parent-item mb-2 border-b-2 md:border-b-0'
        ?>">
            <?= $block->getItemHtml($item) ?>
        </div>
        <?php if ($helper->isMessagesAllowed('order_item', $item) && $item->getGiftMessageId()): ?>
            <?php $giftMessage = $helper->getGiftMessageForEntity($item); ?>
        <div class="col options mb-2">
            <a href="#"
               id="order-item-gift-message-link-<?= (int)$item->getId() ?>"
               class="action show"
               aria-controls="order-item-gift-message-<?= (int)$item->getId() ?>"
               data-item-id="<?= (int)$item->getId() ?>">
                <?= $escaper->escapeHtml(__('Gift Message')) ?>
            </a>
            <?php $giftMessage =
                $helper->getGiftMessageForEntity($item); ?>
            <div class="order-gift-message" id="order-item-gift-message-<?= (int)$item->getId() ?>" role="region"
                 aria-expanded="false" tabindex="-1">
                <a href="#"
                   title="<?= $escaper->escapeHtml(__('Close')) ?>"
                   aria-controls="order-item-gift-message-<?= (int)$item->getId() ?>"
                   data-item-id="<?= (int)$item->getId() ?>"
                   class="action close">
                    <?= $escaper->escapeHtml(__('Close')) ?>
                </a>
                <dl class="item-options">
                    <dt class="item-sender">
                        <strong class="label">
                            <?= $escaper->escapeHtml(__('From')) ?>
                        </strong>
                        <?= $escaper->escapeHtml($giftMessage->getSender()) ?>
                    </dt>
                    <dt class="item-recipient">
                        <strong class="label">
                            <?= $escaper->escapeHtml(__('To')) ?>
                        </strong>
                        <?= $escaper->escapeHtml($giftMessage->getRecipient()) ?>
                    </dt>
                    <dd class="item-message">
                        <?= /* @noEscape */ $helper->getEscapedGiftMessage($item) ?>
                    </dd>
                </dl>
            </div>
        </div>
    <?php endif ?>
    <?php endforeach; ?>
    <?php if ($block->isPagerDisplayed()): ?>
        <?= $block->getPagerHtml() ?>
    <?php endif ?>
</div>
<div class="flex justify-end ">
    <div class="lg:w-1/3">
        <div class="grid grid-cols-2 text-right p-2 gap-2 hidden">
            <?= $block->getChildHtml('order_totals') ?>
        </div>
    </div>
</div>
