<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

/**
 * @var $block \Magento\Sales\Block\Order\Totals
 * @see \Magento\Sales\Block\Order\Totals
 */
/** @var \Magento\Framework\Pricing\Helper\Data $priceHelper */
$priceHelper = $this->helper('Magento\Framework\Pricing\Helper\Data');
/** @var \Magetop\Advancepreview\Helper\OrderHelper $previewOrderHelper */
$previewOrderHelper = $this->helper('\Magetop\Advancepreview\Helper\OrderHelper');
$order = $block->getSource();
$priceItem = 0;
$token = '';
if($order && $order->getItems()) {
    $items = $order->getItems();
    foreach ($items as $item) {
        $infoRequest = $previewOrderHelper->getPreviewOrderItemDetail($item);
        if (isset($infoRequest['txt_project_final_price'])) {
            $token = isset($infoRequest['uenc']) ? $infoRequest['uenc'] : '';
            $priceItem += round($infoRequest['txt_project_final_price'],2);
        }
    }
}
$action     = $block->getRequest()->getActionName();
//$route      = $block->getRequest()->getRouteName();
//echo $action . ' _ '. $route;
?>
<div style="width: 338%; clear: both; display: block; margin: 20px 0;">
<?php if($action == 'buyWithout' || $action == 'createOrderFromWishlist') { ?>
    <?php

    $token = sha1($token)?>
    <a href="<?php echo $block->getUrl().'advance_preview/index/index/order_id/'.$order->getId().'/type/2/token/'.$token; ?>"><?php echo __('Download order PDF') ?></a>

<?php } ?>

<table <?php if($action == 'buyWithout' || $action == 'createOrderFromWishlist') { ?> style="float: right" <?php } ?>>
<?php foreach ($block->getTotals() as $_code => $_total) : ?>
    <?php //echo $_code; ?>
    <?php if($_code == 'shipping' || $_code == 'subtotal') { continue; } ?>
    <?php if ($_total->getBlockName()) : ?>
        <?= $block->getChildHtml($_total->getBlockName(), false) ?>
    <?php else :?>
        <tr class="<?= $block->escapeHtmlAttr($_code) ?>">
            <th <?= /* @noEscape */ $block->getLabelProperties() ?> scope="row">
                <?php if ($_total->getStrong()) : ?>
                    <strong><?= $block->escapeHtml($_total->getLabel()) ?></strong>
                <?php else : ?>
                    <?= $block->escapeHtml($_total->getLabel()) ?>
                <?php endif ?>
            </th>
            <td <?= /* @noEscape */ $block->getValueProperties() ?> data-th="<?= $block->escapeHtmlAttr($_total->getLabel()) ?>">
                <?php if ($_total->getStrong()) : ?>
                    <strong><?php echo $priceHelper->currency($priceItem,true, false); ?></strong>
                <?php else : ?>
                    <?php echo $priceHelper->currency($priceItem,true, false); ?>
                <?php endif?>
            </td>
        </tr>
    <?php endif; ?>
<?php endforeach?>
</table>
</div>