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

// @codingStandardsIgnoreFile

?>
<?php /** @var $block \Magento\Shipping\Block\Adminhtml\Order\Packaging */ ?>
<?php
$shippingMethod = $block->getShipment()->getOrder()->getShippingMethod();
$sizeSource = $block->getSourceSizeModel()->toOptionArray();
$girthEnabled = $block->isDisplayGirthValue() && $block->isGirthAllowed() ? 1 : 0;
?>
<script>
    require([
        "jquery",
        "prototype",
        "Magento_Shipping/order/packaging",
        "Magento_Ui/js/modal/modal"
    ], function(jQuery){

        packaging = new Packaging(<?php /* @escapeNotVerified */ echo $block->getConfigDataJson() ?>);
        packaging.changeContainerType($$('select[name=package_container]')[0]);
        packaging.checkSizeAndGirthParameter(
            $$('select[name=package_container]')[0],
            <?php /* @escapeNotVerified */ echo $girthEnabled; ?>
        );
        packaging.setConfirmPackagingCallback(function(){
            packaging.setParamsCreateLabelRequest($('edit_form').serialize(true));
            packaging.sendCreateLabelRequest();
        });
        packaging.setLabelCreatedCallback(function(response){
            setLocation("<?php /* @escapeNotVerified */ echo $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];
            if (item && !isNaN(item.value)) {
                return item.value;
            }
        });
        jQuery('#packaging_window').modal({
            type: 'slide',
            title: '<?php /* @escapeNotVerified */ echo __('Create Packages') ?>',
            buttons: [{
                text: '<?php /* @escapeNotVerified */ echo __('Cancel') ?>',
                'class': 'action-secondary',
                click: function () {
                    packaging.cancelPackaging();
                    this.closeModal();
                    }
                }, {
                text: '<?php /* @escapeNotVerified */ echo __('Save') ?>',
                'attr': {'disabled':'disabled', 'data-action':'save-packages'},
                'class': 'action-primary _disabled',
                click: function () {
                    packaging.confirmPackaging();
                    }
                }, {
                    text: '<?php /* @escapeNotVerified */ echo __('Add Package') ?>',
                    'attr': {'data-action':'add-packages'},
                    'class': 'action-secondary',
                    click: function () {
                        packaging.newPackage();
                    }
            }]
        });
    });
</script>
<?php include ($block->getTemplateFile('Magento_Shipping::order/packaging/popup_content.phtml')) ?>
