<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */
?>
<?php // @codingStandardsIgnoreFile ?>
<?php
    /** @var \Temando\Shipping\Block\Adminhtml\ComponentContainer $block */
    /** @var \Temando\Shipping\ViewModel\Order\OrderShip $viewModel */
    $viewModel = $block->getViewModel();
?>
<?php if ($viewModel->hasSalesShipment() && !$viewModel->getShippingApiAccess()->getSessionToken()) : ?>
    <p class="message message-warning">
        To add and manage shipments first <a href="<?= $block->escapeUrl($viewModel->getConfigUrl()) ?>">Activate Magento Shipping</a>.
    </p>
<?php elseif ($viewModel->hasSalesShipment()) : ?>
    <div id="temando-componentry"
         data-bind="scope:'temando-componentry'"
         data-component-init='[
         {
            "entrypoint":"OrderShip",
            "assetsUrl":"<?= $block->escapeUrl($block->getAssetsUrl()); ?>",
            "elements": {
                "m2PageActionsButtonsId": "temando-page-actions-buttons"
            },
            "data" : {
                "temandoApiEndpoint": "<?= $block->escapeUrl($viewModel->getShippingApiAccess()->getApiEndpoint()); ?>",
                "temandoApiToken": "<?= $block->escapeHtml($viewModel->getShippingApiAccess()->getSessionToken()); ?>",
                "temandoApiTokenTtl": "<?= $block->escapeHtml($viewModel->getShippingApiAccess()->getSessionTokenExpiry()); ?>",
                "m2ApiTemandoTokenRefreshEndpoint": "<?= $block->escapeUrl($viewModel->getShippingApiAccess()->getSessionTokenRefreshEndpoint()); ?>",
                "m2ApiAccessToken": "<?= $block->escapeHtml($viewModel->getCoreApiAccess()->getAccessToken()); ?>",
                "m2ApiAccessTokenTtl" : <?= /* @noEscape */ $viewModel->getCoreApiAccess()->getSessionExpirationTime(); ?>,
                "m2ApiOrderShippedEndpoint": "<?= $block->escapeUrl($viewModel->getShipEndpoint()); ?>",
                "m2OrderData": <?= /* @noEscape */ str_replace("'", '\u0027', $viewModel->getOrderData()); ?>,
                "m2OrderDataVersion": "1",
                "defaultCurrency": "<?= $block->escapeHtml($viewModel->getDefaultCurrency()); ?>",
                "defaultDimensionsUnit": "<?= $block->escapeHtml($viewModel->getDefaultDimensionsUnit()); ?>",
                "defaultWeightUnit": "<?= $block->escapeHtml($viewModel->getDefaultWeightUnit()); ?>",
                "experienceCode": "<?= $block->escapeHtml($viewModel->getSelectedExperience()); ?>",
                "orderId": "<?= $block->escapeHtml($viewModel->getExtOrderId()); ?>",
                "orderMeta": <?= /* @noEscape */ $viewModel->getOrderMeta() ?>,
                "ipAddress": "<?= $block->escapeHtml($block->getIpAddress()); ?>",
                "lang": "<?= $block->escapeHtml($block->getLanguage()); ?>",
                "locale": "<?= $block->escapeHtml($block->getLocale()); ?>",
                "shipmentDetailPageUrlTpl": "<?= $block->escapeUrl($viewModel->getShipmentViewPageUrl()); ?>"
            }
         }]'
    >
        <!-- ko template: getTemplate() --><!-- /ko -->
        <script type="text/x-magento-init">
        {
            "#temando-componentry": {
                "Magento_Ui/js/core/app":  <?= /* @noEscape */ $block->getJsLayout(); ?>
            }
        }
        </script>
    </div>
<?php endif; ?>
