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

// @codingStandardsIgnoreFile

/**
 * Template for displaying product price at product view page, gift registry and wish-list
 *
 * @var $block \Magento\Msrp\Pricing\Render\PriceBox
 */
?>
<?php

/** @var Magento\Msrp\Pricing\Price\MsrpPriceInterface $priceType */
$priceType = $block->getPrice();

/** @var $product \Magento\Catalog\Model\Product */
$product = $block->getSaleableItem();
$productId = $product->getId();

$amount = $block->getMsrpPriceCalculator()->getMsrpPriceValue($product);

$msrpPrice = $block->renderAmount(
    $priceType->getCustomAmount($amount),
    [
        'price_id' => $block->getPriceId() ? $block->getPriceId() : 'old-price-' . $productId,
        'include_container' => false,
        'skip_adjustments' => true
    ]
);
$priceElementIdPrefix = $block->getPriceElementIdPrefix() ? $block->getPriceElementIdPrefix() : 'product-price-';
?>

<?php if ($amount): ?>
    <span class="old-price map-old-price"><?= /* @escapeNotVerified */ $msrpPrice ?></span>
    <span class="map-fallback-price normal-price"><?= /* @escapeNotVerified */ $msrpPrice ?></span>
<?php endif; ?>

<?php if ($priceType->isShowPriceOnGesture()): ?>
    <?php

    $addToCartUrl = '';
    if ($product->isSaleable()) {
        /** @var Magento\Catalog\Block\Product\AbstractProduct $addToCartUrlGenerator */
        $addToCartUrlGenerator = $block->getLayout()->getBlockSingleton('Magento\Catalog\Block\Product\AbstractProduct');
        $addToCartUrl = $addToCartUrlGenerator->getAddToCartUrl(
            $product,
            ['_query' => [
                \Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED =>
                    $this->helper('Magento\Framework\Url\Helper\Data')->getEncodedUrl(
                        $addToCartUrlGenerator->getAddToCartUrl($product)
                    ),
            ]]
        );
    }

    $priceElementId = $priceElementIdPrefix . $productId . $block->getIdSuffix();
    $popupId = 'msrp-popup-' . $productId . $block->getRandomString(20);
    $data = ['addToCart' => [
        'origin'=> 'msrp',
        'popupId' => '#' . $popupId,
        'productName' => $block->escapeJs($block->escapeHtml($product->getName())),
        'productId' => $productId,
        'productIdInput' => 'input[type="hidden"][name="product"]',
        'realPrice' => $block->getRealPriceHtml(),
        'isSaleable' => $product->isSaleable(),
        'msrpPrice' => $msrpPrice,
        'priceElementId' => $priceElementId,
        'closeButtonId' => '#map-popup-close',
        'addToCartUrl' => $addToCartUrl,
        'paymentButtons' => '[data-label=or]'
    ]];
    if ($block->getRequest()->getFullActionName() === 'catalog_product_view') {
        $data['addToCart']['addToCartButton'] = '#product_addtocart_form [type=submit]';
    } else {
        $data['addToCart']['addToCartButton'] = sprintf(
            'form:has(input[type="hidden"][name="product"][value="%s"]) button[type="submit"]',
            (int) $productId . ',' .
            sprintf('.block.widget .price-box[data-product-id=%s]+.product-item-actions button.tocart',
            (int) $productId));
    }
    ?>
    <span id="<?= /* @escapeNotVerified */ $block->getPriceId() ? $block->getPriceId() : $priceElementId ?>" style="display:none"></span>
    <a href="javascript:void(0);"
       id="<?= /* @escapeNotVerified */ ($popupId) ?>"
       class="action map-show-info"
       data-mage-init='<?= /* @noEscape */ $this->helper('Magento\Framework\Json\Helper\Data')->jsonEncode($data) ?>'><?= /* @escapeNotVerified */ __('Click for price') ?>
    </a>
<?php else: ?>
    <span class="msrp-message">
     <?= /* @escapeNotVerified */ $priceType->getMsrpPriceMessage() ?>
    </span>
<?php endif; ?>

<?php if ($block->getZone() == \Magento\Framework\Pricing\Render::ZONE_ITEM_VIEW): ?>
    <?php $helpLinkId = 'msrp-help-' . $productId . $block->getRandomString(20); ?>
    <a href="javascript:void(0);"
       id="<?= /* @escapeNotVerified */ $helpLinkId ?>"
       class="action map-show-info"
       data-mage-init='{"addToCart":{"origin": "info",
                                     "helpLinkId": "#<?= /* @escapeNotVerified */ $helpLinkId ?>",
                                     "productName": "<?= $block->escapeJs($block->escapeHtml($product->getName())) ?>",
                                     "closeButtonId": "#map-popup-close"}}'><span><?= /* @escapeNotVerified */ __("What's this?") ?></span>
    </a>
<?php endif; ?>