<?php
/**
 * Copyright © 2016 Magento. 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\Catalog\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();
$msrpPrice = $block->renderAmount(
    $priceType->getCustomAmount($product->getMsrp() ?: $product->getTypeInstance()->getChildrenMsrp($product)),
    [
        'price_id' => $block->getPriceId() ? $block->getPriceId() : 'old-price-' . $productId,
        'include_container' => false,
        'skip_adjustments' => true
    ]
);
$priceElementIdPrefix = $block->getPriceElementIdPrefix() ? $block->getPriceElementIdPrefix() : 'product-price-';

$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)
                ),
        ]]
    );
}
?>
<?php if ($product->getMsrp()): ?>
    <span class="old-price map-old-price"><?php /* @escapeNotVerified */ echo $msrpPrice ?></span>
<?php endif; ?>

<?php if ($priceType->isShowPriceOnGesture()): ?>
    <?php
        $priceElementId = $priceElementIdPrefix . $productId . $block->getIdSuffix();
        $popupId = 'msrp-popup-' . $productId . $block->getRandomString(20);
        $data = ['addToCart' => [
            'origin'=> 'msrp',
            'popupId' => '#' . $popupId,
            'productName' => $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
            );
        }
    ?>
    <span id="<?php /* @escapeNotVerified */ echo $block->getPriceId() ? $block->getPriceId() : $priceElementId ?>" style="display:none"></span>
    <a href="javascript:void(0);"
       id="<?php /* @escapeNotVerified */ echo($popupId);?>"
       class="action map-show-info"
       data-mage-init='<?= /* @escapeNotVerified */ $this->helper('Magento\Framework\Json\Helper\Data')->jsonEncode($data)?>'><?php /* @escapeNotVerified */ echo __('Click for price'); ?>
    </a>
<?php else: ?>
    <span class="msrp-message">
     <?php /* @escapeNotVerified */ echo $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="<?php /* @escapeNotVerified */ echo $helpLinkId;?>"
       class="action map-show-info"
       data-mage-init='{"addToCart":{"origin": "info",
                                     "helpLinkId": "#<?php /* @escapeNotVerified */ echo $helpLinkId;?>",
                                     "productName": "<?php /* @escapeNotVerified */ echo $product->getName() ?>",
                                     "closeButtonId": "#map-popup-close"}}'><span><?php /* @escapeNotVerified */ echo __("What's this?"); ?></span>
    </a>
<?php endif; ?>
