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

// @codingStandardsIgnoreFile

?>

<?php
/** @var \Magento\Catalog\Pricing\Render\PriceBox $block */

/** @var \Magento\Catalog\Pricing\Price\TierPrice $tierPriceModel */
$tierPriceModel = $block->getPrice();
$tierPrices = $tierPriceModel->getTierPriceList();
$msrpShowOnGesture = $block->getPriceType('msrp_price')->isShowPriceOnGesture();
$product = $block->getSaleableItem();
?>
<?php if (count($tierPrices)) : ?>
    <ul class="<?php /* @escapeNotVerified */ echo($block->hasListClass() ? $block->getListClass() : 'prices-tier items'); ?>">
    <?php foreach ($tierPrices as $index => $price) : ?>
        <li class="item">
            <?php
                $productId = $product->getId();
                $isSaleable = $product->isSaleable();
                $popupId = 'msrp-popup-' . $productId . $block->getRandomString(20);
                if ($msrpShowOnGesture && $price['price']->getValue() < $product->getMsrp()):
                    $addToCartUrl = '';
                    if ($isSaleable) {
                        $addToCartUrl = $this->helper('\Magento\Checkout\Helper\Cart')
                            ->getAddUrl($product, ['qty' => $price['price_qty']]);
                    }
                    $tierPriceData = [
                        'addToCartUrl' => $addToCartUrl,
                        'name' => $product->getName(),
                        'realPrice' => $block->renderAmount(
                            $price['price'],
                            [
                                'price_id'          => $index,
                                'id_suffix'         => '-' . $index,
                                'include_container' => true
                            ]
                        ),
                        'msrpPrice' => $block->renderAmount(
                            $block->getPriceType('msrp_price')->getAmount(),
                            [
                                'price_id'          => $index,
                                'id_suffix'         => '-' . $index,
                                'include_container' => true
                            ]
                        ),
                    ];
                    if ($block->getCanDisplayQty($product)) {
                        $tierPriceData['qty'] = $price['price_qty'];
                    }
                ?>
                <?php /* @escapeNotVerified */ echo __('Buy %1 for: ', $price['price_qty']); ?>
                <a href="javascript:void(0);"
                   id="<?php /* @escapeNotVerified */ echo($popupId);?>"
                   data-tier-price="<?php echo $block->escapeHtml($block->jsonEncode($tierPriceData)); ?>">
                    <?php /* @escapeNotVerified */ echo __('Click for price'); ?></a>
                <?php else:
                    $priceAmountBlock = $block->renderAmount(
                        $price['price'],
                        [
                            'price_id'          => $index,
                            'id_suffix'         => '-' . $index,
                            'include_container' => true,
                            'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_OPTION
                        ]
                    );
                ?>
                <?php /* @escapeNotVerified */ echo ($block->getShowDetailedPrice() !== false)
                        ? __(
                            'Buy %1 for %2 each and <strong class="benefit">save<span class="percent tier-%3">&nbsp;%4</span>%</strong>',
                            $price['price_qty'],
                            $priceAmountBlock,
                            $index,
                            $tierPriceModel->getSavePercent($price['price'])
                        )
                        : __('Buy %1 for %2 each', $price['price_qty'], $priceAmountBlock);
                ?>
            <?php endif; ?>
        </li>
    <?php endforeach; ?>
    </ul>
    <?php if ($msrpShowOnGesture):?>
        <script type="text/x-magento-init">
            {
                ".product-info-main": {
                    "addToCart": {
                        "origin": "tier",
                        "addToCartButton": "#product_addtocart_form [type=submit]",
                        "inputQty": "#qty",
                        "attr": "[data-tier-price]",
                        "productForm": "#product_addtocart_form",
                        "productId": "<?php /* @escapeNotVerified */ echo $productId; ?>",
                        "productIdInput": "input[type=hidden][name=product]",
                        "isSaleable": "<?php /* @escapeNotVerified */ echo $isSaleable; ?>"
                    }
                }
            }
        </script>
    <?php endif;?>
<?php endif; ?>
