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

<?php
// phpcs:disable Magento2.Templates.ThisInTemplate
// phpcs:disable Generic.WhiteSpace.ScopeIndent

/** @var \Magento\Catalog\Pricing\Render\PriceBox $block */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
/** @var \Magento\Framework\Locale\LocaleFormatter $localeFormatter*/
/** @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="<?= $block->escapeHtmlAttr(($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::class)
                            ->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'];
                    }
                    ?>
                    <?= $block->escapeHtml(__('Buy %1 for: ', $price['price_qty'])) ?>
                    <a href="#"
                       id="<?= $block->escapeHtmlAttr($popupId) ?>"
                       data-tier-price="<?= $block->escapeHtml($block->jsonEncode($tierPriceData)) ?>">
                        <?= $block->escapeHtml(__('Click for price')) ?>
                    </a>
                    <?= /* @noEscape */ $secureRenderer->renderEventListenerAsTag(
                        'onclick',
                        'event.preventDefault()',
                        'a#' . $block->escapeHtmlAttr($popupId)
                    ) ?>
                <?php else:
                    $priceAmountBlock = $block->renderAmount(
                        $price['price'],
                        [
                            'price_id'          => $index,
                            'id_suffix'         => '-' . $index,
                            'include_container' => true,
                            'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_OPTION
                        ]
                    );
                    ?>
                    <?= /* @noEscape */ ($block->getShowDetailedPrice() !== false)
                    ? __(
                        'Buy %1 for %2 each and '.
                        '<strong class="benefit">save<span class="percent tier-%3">&nbsp;%4</span>%</strong>',
                        $localeFormatter->formatNumber($price['price_qty']),
                        $priceAmountBlock,
                        $index,
                        $localeFormatter->formatNumber(
                            $block->formatPercent($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": "<?= (int) $productId ?>",
                        "productIdInput": "input[type=hidden][name=product]",
                        "isSaleable": "<?= (bool) $isSaleable ?>"
                    }
                }
            }
        </script>
    <?php endif;?>
<?php endif; ?>
