<?php

/** @var DefaultItems $block */

use FiloBlu\Refilo\Block\Order\Email\Items\PreorderItem;
use Magento\GiftMessage\Helper\Message;
use Magento\Sales\Block\Order\Email\Items\DefaultItems;
use Magento\Sales\Model\Order\Item;

/** @var PreorderItem $preorder */
$preorder = $block->getData('filoblu_preorder');

/** @var Item $_item */
$_item = $block->getItem();

$blockData = $this->helper(\FiloBlu\Refilo\Helper\Data::class);
$_emailTemplateImage = $blockData->getOrderEmailTemplateImage();
$_wh = $blockData->getOrderEmailTemplateImageSize() != 0 ?  $blockData->getOrderEmailTemplateImageSize() : 60;

$_order = $_item->getOrder();
$_store = $_order->getStore();

$_imageHelper = $this->helper('Magento\Catalog\Helper\Image');
$_baseImageUrl = $_store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'catalog/product';

?>
<tr>
    <td class="item-info<?= ($block->getItemOptions() ? ' has-extra' : '') ?>">
        <div style="display:flex;">
        <?php if( $_emailTemplateImage != 0 ): ?>
                <div style="margin-right: 10px;">
                    <img src="<?= $_imageHelper->init($_item->getProduct(), 'small_image', ['type'=>'small_image'])->keepAspectRatio(true)->resize($_wh,$_wh)->getUrl();?>" alt="<?= __('Product Image');?>">
                </div>
            <?php endif; ?>
            <div class="wrapper" style="margin:0;">
                <p class="product-name"><?= $block->escapeHtml($_item->getName()) ?></p>
                <p class="sku"><?= $block->escapeHtml(__('SKU')) ?>: <?= $block->escapeHtml($block->getSku($_item)) ?></p>
                <?php
                if ($preorder->hasPreorderDate($_item)) : ?>
                    <p> <?= $block->escapeHtml(__('Pre-order products')) . " " . $preorder->getShippingDate($_item) ?></p>
                <?php
                elseif ($preorder->hasPreorderWithoutDate($_item)) : ?>
                    <p> <?= $block->escapeHtml(__('back_orders_label')) ?></p>
                <?php endif; ?>
                <?php if ($block->getItemOptions()) : ?>
                    <dl class="item-options">
                        <?php foreach ($block->getItemOptions() as $option) : ?>
                            <dt><strong><em><?= $block->escapeHtml($option['label']) ?></em></strong></dt>
                            <dd>
                                <?= /* @noEscape */  nl2br($block->escapeHtml($option['value'])) ?>
                            </dd>
                        <?php endforeach; ?>
                    </dl>
                <?php endif; ?>
                <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
                <?php if ($addInfoBlock) :?>
                    <?= $addInfoBlock->setItem($_item)->toHtml() ?>
                <?php endif; ?>
                <?= $block->escapeHtml($_item->getDescription()) ?>
            </div>
        </div>
    </td>
    <td class="item-qty"><?= (float)$_item->getQtyOrdered() ?></td>
    <td class="item-price">
        <?= /* @noEscape */
        $block->getItemPrice($_item) ?>
    </td>
</tr>
<?php
if ($_item->getGiftMessageId()
    && $_giftMessage = $this->helper(Message::class)
        ->getGiftMessage($_item->getGiftMessageId())
) : ?>
    <tr>
        <td colspan="3" class="item-extra">
            <table class="message-gift">
                <tr>
                    <td>
                        <h3><?= $block->escapeHtml(__('Gift Message')) ?></h3>
                        <strong><?= $block->escapeHtml(__('From:')) ?></strong> <?= $block->escapeHtml(
                            $_giftMessage->getSender()
                        ) ?>
                        <br/><strong><?= $block->escapeHtml(__('To:')) ?></strong> <?= $block->escapeHtml(
                            $_giftMessage->getRecipient()
                        ) ?>
                        <br/><strong><?= $block->escapeHtml(__('Message:')) ?></strong>
                        <br/><?= $block->escapeHtml($_giftMessage->getMessage()) ?>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
<?php
endif; ?>
