<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
/**
 * @var \Magento\Shipping\Block\Adminhtml\View\Form $block
 * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
 * @var \Magento\Framework\Escaper $escaper
 */

use Macopedia\OrlenShipping\Model\Carrier\OrlenShipping;

?>

<?php
/** @var \Magento\Shipping\Helper\Data $shippingHelper */
$shippingHelper = $block->getData('shippingHelper');
/** @var \Magento\Tax\Helper\Data $taxHelper */
$taxHelper = $block->getData('taxHelper');

$shipment = $block->getShipment();
$order = $shipment->getOrder();
?>

<?php
$shipmentAdminDate = $block->formatDate($shipment->getCreatedAt(), \IntlDateFormatter::MEDIUM);
?>

<?php // MACOPEDIA CUSTOM START ?>
<?php if (str_contains($order->getShippingMethod(), OrlenShipping::CODE)): ?>
    <div class="admin__page-section shipment-view-information">
        <div class="admin__page-section-title">
            <span class="title"><?= $escaper->escapeHtml(__('ORLEN Paczka')) ?></span>
        </div>
        <div class="admin__page-section-content">
            <div class="admin__page-section-item shipment-information">
                <div class="admin__page-section-item-content">
                    <table class="admin__table-secondary shipment-information-table">
                        <tr>
                            <th><?= $escaper->escapeHtml(__('Sender Orders')) ?></th>
                            <td><?= $escaper->escapeHtml($shipment->getData('sender_orders')) ?></td>
                        </tr>
                        <tr>
                            <th><?= $escaper->escapeHtml(__('Box Size')) ?></th>
                            <?php $boxSizeValue = $shippingHelper->getBoxSizeByValue($shipment->getData('box_size')) ?>
                            <td><?= $escaper->escapeHtml($boxSizeValue) ?></td>
                        </tr>
                        <tr>
                            <th><?= $escaper->escapeHtml(__('Protocol Code')) ?></th>
                            <?php $boxSizeValue = $shipment->getData('protocol_code') ?>
                            <td><?= $escaper->escapeHtml($boxSizeValue) ?></td>
                        </tr>
                        <tr>
                            <th><?= $escaper->escapeHtml(__('Pack Value')) ?></th>
                            <?php $boxSizeValue = $shipment->getData('pack_value') ?>
                            <td><?= $escaper->escapeHtml($boxSizeValue) ?></td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
<?php endif; ?>
<?php // MACOPEDIA CUSTOM END ?>

<div class="admin__page-section shipment-view-information">
    <div class="admin__page-section-title">
        <span class="title"><?= $escaper->escapeHtml(__('Shipment Information')) ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item shipment-information">
            <div class="admin__page-section-item-title">
                <?php $confirmationEmailStatusMessage = $shipment->getEmailSent()
                    ? __('The shipment confirmation email was sent')
                    : __('The shipment confirmation email is not sent');
                ?>
                <span class="title">
                    <?= $escaper->escapeHtml(__('Shipment # %1', $shipment->getIncrementId())) ?>
                    (<span><?= $escaper->escapeHtml($confirmationEmailStatusMessage) ?></span>)
                </span>
            </div>
            <div class="admin__page-section-item-content">
                <table class="admin__table-secondary shipment-information-table">
                    <tr>
                        <th><?= $escaper->escapeHtml(__('Shipment Date')) ?></th>
                        <td><?= $escaper->escapeHtml($shipmentAdminDate) ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<?= $block->getChildHtml('order_info'); ?>

<section class="admin__page-section order-shipment-billing-shipping">
    <div class="admin__page-section-title">
        <span class="title"><?= $escaper->escapeHtml(__('Payment &amp; Shipping Method')); ?></span>
    </div>
    <div class="admin__page-section-content">
        <div class="admin__page-section-item order-payment-method">
            <div class="admin__page-section-item-title">
                <span class="title"><?= $escaper->escapeHtml(__('Payment Information')); ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <div><?= $block->getChildHtml('order_payment') ?></div>
                <div class="order-payment-currency">
                    <?= $escaper->escapeHtml(
                        __('The order was placed using %1.', $order->getOrderCurrencyCode())
                    ); ?>
                </div>
            </div>
        </div>

        <div class="admin__page-section-item order-shipping-address">
            <div class="admin__page-section-item-title">
                <span class="title"><?= $escaper->escapeHtml(__('Shipping and Tracking Information')); ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <div class="shipping-description-wrapper">
                    <?php if ($shipment->getTracksCollection()->count()): ?>
                        <p>
                            <a href="#" id="linkId" title="<?= $escaper->escapeHtml(__('Track this shipment')); ?>">
                                <?= $escaper->escapeHtml(__('Track this shipment')); ?>
                            </a>
                            <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
                                'onclick',
                                'event.preventDefault();' .
                                "popWin('{$block->escapeJs($shippingHelper->getTrackingPopupUrlBySalesModel(
                                    $shipment
                                    ))}','trackshipment','width=800,height=600,resizable=yes,scrollbars=yes')",
                                'a#linkId'
                            ) ?>
                        </p>
                    <?php endif; ?>
                    <div class="shipping-description-title">
                        <?= $escaper->escapeHtml($order->getShippingDescription()); ?>
                    </div>

                    <?= $escaper->escapeHtml(__('Total Shipping Charges')); ?>:

                    <?php if ($taxHelper->displayShippingPriceIncludingTax()): ?>
                        <?php $excl = $block->displayShippingPriceInclTax($order); ?>
                    <?php else: ?>
                        <?php $excl = $block->displayPriceAttribute('shipping_amount', false, ' '); ?>
                    <?php endif; ?>
                    <?php $incl = $block->displayShippingPriceInclTax($order); ?>

                    <?= /* @noEscape */ $excl; ?>
                    <?php if ($taxHelper->displayShippingBothPrices() && $incl != $excl): ?>
                        (<?= $escaper->escapeHtml(__('Incl. Tax')); ?> <?= /* @noEscape */ $incl; ?>)
                    <?php endif; ?>
                </div>

                <p>
                    <?php if ($block->canCreateShippingLabel()): ?>
                        <?= /* @noEscape */ $block->getCreateLabelButton(); ?>
                    <?php endif ?>
                    <?php if ($shipment->getShippingLabel()): ?>
                        <?= /* @noEscape */ $block->getPrintLabelButton(); ?>
                    <?php endif ?>
                    <?php if ($shipment->getPackages()): ?>
                        <?= /* @noEscape */ $block->getShowPackagesButton(); ?>
                    <?php endif ?>
                </p>
                <?= $block->getChildHtml('shipment_tracking'); ?>

                <?= $block->getChildHtml('shipment_packaging'); ?>
                <?php $scriptString = <<<script

                    require([
                        'jquery',
                        'prototype'
                    ], function (jQuery) {
                        var setCallbacks = function () {
                            window.packaging.setConfirmPackagingCallback(function () {
                                window.packaging.sendCreateLabelRequest();
                            });
                            window.packaging.setLabelCreatedCallback(function () {
                                setLocation("{$block->escapeJs($block->getUrl(
                                        'adminhtml/order_shipment/view',
                                        ['shipment_id' => $shipment->getId()]
                                    ))}");
                            });
                        };

                        if (jQuery(document).data('packagingInited')) {
                            setCallbacks();
                        } else {
                            jQuery(document).on('packaging:inited', setCallbacks);
                        }
                    });

script;
                ?>
                <?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
            </div>
        </div>
    </div>
</section>
<div><?= $block->getChildHtml('extra_shipment_info') ?></div>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= $escaper->escapeHtml(__('Items Shipped')); ?></span>
    </div>
    <?= $block->getChildHtml('shipment_items'); ?>
</section>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?= $escaper->escapeHtml(__('Order Total')); ?></span>
    </div>
    <div class="admin__page-section-content">
        <?= $block->getChildHtml('shipment_packed'); ?>

        <div class="admin__page-section-item order-comments-history">
            <div class="admin__page-section-item-title">
                <span class="title"><?= $escaper->escapeHtml(__('Shipment History')); ?></span>
            </div>
            <div class="admin__page-section-item-content"><?= $block->getChildHtml('order_comments'); ?></div>
        </div>
    </div>
</section>
