<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
//phpcs:disable PSR2.Methods.FunctionCallSignature.SpaceBeforeOpenBracket
//phpcs:disable Magento2.Security.IncludeFile.FoundIncludeFile

/**
 * @var $block \Magento\Shipping\Block\Adminhtml\Order\Packaging
 * @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
 */
?>
<?php
$shippingMethod = $block->getShipment()->getOrder()->getShippingMethod();
$sizeSource = $block->getSourceSizeModel()->toOptionArray();
$girthEnabled = $block->isDisplayGirthValue() && $block->isGirthAllowed() ? 1 : 0;
?>

<?php $scriptString = <<<script

    require([
        "jquery",
        "prototype",
        "Magento_Shipping/order/packaging",
        "Magento_Ui/js/modal/modal"
    ], function(jQuery){

        window.packaging = new Packaging({$block->getConfigDataJson()});
        packaging.changeContainerType($$('select[name=package_container]')[0]);
        packaging.checkSizeAndGirthParameter(
            \$$('select[name=package_container]')[0],
            {$girthEnabled}
        );
        packaging.setConfirmPackagingCallback(function(){
            packaging.setParamsCreateLabelRequest($('edit_form').serialize(true));
            packaging.sendCreateLabelRequest();
        });
        packaging.setLabelCreatedCallback(function(response){
            setLocation("{$block->escapeJs($block->getUrl(
                'sales/order/view',
                ['order_id' => $block->getShipment()->getOrderId()]
            ))}");
        });
        packaging.setCancelCallback(function() {
            if ($('create_shipping_label')) {
                packaging.cleanPackages();
                $('create_shipping_label').checked = false;
                toggleCreateLabelCheckbox();
            }
        });
        packaging.setItemQtyCallback(function(itemId){
            var item = $$('[name="shipment[items]['+itemId+']"]')[0],
                itemTitle = $('order_item_' + itemId + '_title');
            if (!itemTitle && !item) {
                return 0;
            }
            if (item && !isNaN(item.value)) {
                return item.value;
            }
        });
        jQuery('#packaging_window').modal({
            type: 'slide',
            title: '{$block->escapeJs(__('Create Packages'))}',
            buttons: [{
                text: '{$block->escapeJs(__('Cancel'))}',
                'class': 'action-secondary',
                click: function () {
                    packaging.cancelPackaging();
                    this.closeModal();
                    }
                }, {
                text: '{$block->escapeJs(__('Save'))}',
                'attr': {'disabled':'disabled', 'data-action':'save-packages'},
                'class': 'action-primary _disabled',
                click: function () {
                    packaging.confirmPackaging();
                    }
                }, {
                    text: '{$block->escapeJs(__('Add Package'))}',
                    'attr': {'data-action':'add-packages'},
                    'class': 'action-secondary',
                    click: function () {
                        packaging.newPackage();
                    }
            }]
        });
        jQuery(document).trigger('packaging:inited');
        jQuery(document).data('packagingInited', true);
    });

script;
?>
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
<?php include($block->getTemplateFile('Magento_Shipping::order/packaging/popup_content.phtml')) ?>
