<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */

/** @var \Magento\Shipping\Helper\Carrier $carrierHelper */
$carrierHelper = $block->getData('carrierHelper');
?>

<div id="packed_window">
<?php foreach ($block->getPackages() as $packageId => $package): ?>
    <?php $package = new \Magento\Framework\DataObject($package) ?>
    <?php $params = new \Magento\Framework\DataObject($package->getParams()) ?>
    <section class="admin__page-section">
        <div class="admin__page-section-title">
            <span class="title"><?= $block->escapeHtml(__('Package') . ' ' . $packageId) ?></span>
        </div>
        <div class="admin__page-section-content">
            <div class="row row-gutter">
                <div class="col-m-4">
                    <table class="admin__table-secondary">
                        <tbody>
                            <tr>
                                <th><?= $block->escapeHtml(__('Type')) ?></th>
                                <td>
                                    <?= $block->escapeHtml($block->getContainerTypeByCode($params->getContainer())) ?>
                                </td>
                            </tr>
                            <tr>
                            <?php if ($block->displayCustomsValue()): ?>
                                <th><?= $block->escapeHtml(__('Customs Value')) ?></th>
                                <td><?= $block->escapeHtml($block->displayCustomsPrice($params->getCustomsValue())) ?>
                                </td>
                            <?php else: ?>
                                <th><?= $block->escapeHtml(__('Total Weight')) ?></th>
                                <td><?= $block->escapeHtml($params->getWeight() . ' ' .
                                        $carrierHelper->getMeasureWeightName($params->getWeightUnits())) ?>
                                </td>
                            <?php endif; ?>
                            </tr>
                        <?php if ($params->getSize()): ?>
                            <tr>
                                <th><?= $block->escapeHtml(__('Size')) ?></th>
                                <td><?= $block->escapeHtml(ucfirst(strtolower($params->getSize()))) ?></td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>
                <div class="col-m-4">
                    <table class="admin__table-secondary">
                        <tbody>
                            <tr>
                                <th><?= $block->escapeHtml(__('Length')) ?></th>
                                <td>
                                <?php if ($params->getLength() != null): ?>
                                    <?= $block->escapeHtml($params->getLength() . ' ' .
                                        $carrierHelper->getMeasureDimensionName($params->getDimensionUnits())) ?>
                                <?php else: ?>
                                    --
                                <?php endif; ?>
                                </td>
                            </tr>
                            <tr>
                                <th><?= $block->escapeHtml(__('Width')) ?></th>
                                <td>
                                <?php if ($params->getWidth() != null): ?>
                                    <?= $block->escapeHtml($params->getWidth() . ' ' .
                                        $carrierHelper->getMeasureDimensionName($params->getDimensionUnits())) ?>
                                <?php else: ?>
                                    --
                                <?php endif; ?>
                                </td>
                            </tr>
                            <tr>
                                <th><?= $block->escapeHtml(__('Height')) ?></th>
                                <td>
                                <?php if ($params->getHeight() != null): ?>
                                    <?= $block->escapeHtml($params->getHeight() . ' ' .
                                        $carrierHelper->getMeasureDimensionName($params->getDimensionUnits())) ?>
                                <?php else: ?>
                                    --
                                <?php endif; ?>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="col-m-4">
                    <table class="admin__table-secondary">
                        <tbody>
                        <?php if ($params->getDeliveryConfirmation() != null): ?>
                            <tr>
                                <th><?= $block->escapeHtml(__('Signature Confirmation')) ?></th>
                                <td>
                                    <?= $block->escapeHtml(
                                        $block->getDeliveryConfirmationTypeByCode($params->getDeliveryConfirmation())
                                    ) ?></td>
                            </tr>
                        <?php endif; ?>
                        <?php if ($params->getContentType() != null): ?>
                            <tr>
                                <th><?= $block->escapeHtml(__('Contents')) ?></th>
                                <?php if ($params->getContentType() == 'OTHER'): ?>
                                    <td><?= $block->escapeHtml($params->getContentTypeOther()) ?></td>
                                <?php else: ?>
                                    <td>
                                        <?= $block->escapeHtml($block->getContentTypeByCode($params->getContentType()))
                                        ?></td>
                                <?php endif; ?>
                            </tr>
                        <?php endif; ?>
                        <?php if ($params->getGirth()): ?>
                            <tr>
                                <th><?= $block->escapeHtml(__('Girth')) ?></th>
                                <td><?= $block->escapeHtml($params->getGirth() . ' ' .
                                        $carrierHelper->getMeasureDimensionName($params->getGirthDimensionUnits())) ?>
                                </td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="admin__page-section-item-title">
            <span class="title"><?= $block->escapeHtml(__('Items in the Package')) ?></span>
        </div>
        <div class="admin__table-wrapper">
            <table class="data-table admin__table-primary">
                <thead>
                <tr class="headings">
                    <th class="col-product"><span><?= $block->escapeHtml(__('Product')) ?></span></th>
                    <th class="col-weight"><span><?= $block->escapeHtml(__('Weight')) ?></span></th>
                    <?php if ($block->displayCustomsValue()): ?>
                        <th class="col-custom"><span><?= $block->escapeHtml(__('Customs Value')) ?></span></th>
                    <?php endif; ?>
                    <th class="col-qty"><span><?= $block->escapeHtml(__('Qty Ordered')) ?></span></th>
                    <th class="col-qty"><span><?= $block->escapeHtml(__('Qty')) ?></span></th>
                </tr>
                </thead>
                <tbody id="">
                <?php foreach ($package->getItems() as $itemId => $item): ?>
                    <?php $item = new \Magento\Framework\DataObject($item) ?>
                    <tr title="#" id="">
                        <td class="col-product">
                            <?= $block->escapeHtml($item->getName()) ?>
                        </td>
                        <td class="col-weight">
                            <?= $block->escapeHtml($item->getWeight()) ?>
                        </td>
                        <?php if ($block->displayCustomsValue()): ?>
                            <td class="col-custom">
                                <?= $block->escapeHtml($block->displayCustomsPrice($item->getCustomsValue())) ?>
                            </td>
                        <?php endif; ?>
                        <td class="col-qty">
                            <?= $block->escapeHtml($block->getQtyOrderedItem($item->getOrderItemId())) ?>
                        </td>
                        <td class="col-qty">
                            <?= /* @noEscape */ $item->getQty()*1 ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </section>
<?php endforeach; ?>
</div>
<?php $scriptString = <<<script

    function showPackedWindow() {
        jQuery('#packed_window').modal('openModal');
    }

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

<script type="text/x-magento-init">
    {
        "#packed_window": {
            "Magento_Shipping/js/packages":{
                "type":"slide",
                "title":"<?= $block->escapeHtml(__('Packages')) ?>",
                "url": "<?= $block->escapeUrl($block->getPrintButton()) ?>"
            }
        }
    }
</script>
