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

<?php
use Magento\Framework\App\ObjectManager;
// @codingStandardsIgnoreFile
/** @var \Magento\Catalog\Pricing\Render\FinalPriceBox $block */

/** ex: \Magento\Catalog\Pricing\Price\RegularPrice */
/** @var \Magento\Framework\Pricing\Price\PriceInterface $priceModel */
$priceModel = $block->getPriceType('regular_price');

/** ex: \Magento\Catalog\Pricing\Price\FinalPrice */
/** @var \Magento\Framework\Pricing\Price\PriceInterface $finalPriceModel */
$finalPriceModel = $block->getPriceType('final_price');
$idSuffix = $block->getIdSuffix() ? $block->getIdSuffix() : '';
$schema = ($block->getZone() == 'item_view') ? true : false;

// Get product and check for click_for_price and call_for_price attributes
$product = $block->getSaleableItem();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$product = $productRepository->getById($product->getId());
$clickForPrice = $product->getData('click_for_price');
$callForPrice = $product->getData('call_for_price');
$revealPrice = $block->getRequest()->getParam('reveal_price');
// Only proceed with price display if neither click_for_price nor call_for_price is true, or if reveal_price is set
/** @var \Magento\Framework\View\Element\Template $block */

$mode = $block->getRequest()->getParam('product_list_mode');

if (!$mode) {
    $productListBlock = $block->getLayout()->getBlock('category.products.list');
    if ($productListBlock && method_exists($productListBlock, 'getMode')) {
        $mode = $productListBlock->getMode();
    } else {
        $mode = 'grid'; // fallback default mode
    }
}
$priceClass = ($mode === 'list') ? '' : 'justify-center';

if ((!$clickForPrice && !$callForPrice) || $revealPrice):

?>

    <?php if ($block->hasSpecialPrice()) :?>
    <div class="flex items-center <?= $priceClass; ?> gap-1">
            <span class="special-price text-red">
                <?= /* @noEscape */ $block->renderAmount($finalPriceModel->getAmount(), [
                    'display_label'     => __('Special Price'),
                    'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
                    'price_type'        => 'finalPrice',
                    'include_container' => true,
                    'schema' => $schema
                ]); ?>
            </span>
        <span class="ml-1">Excl. VAT</span>

        <span class="hidden old-price">
                <?= /* @noEscape */ $block->renderAmount($priceModel->getAmount(), [
                    'price_id'          => $block->getPriceId('old-price-' . $idSuffix),
                    'price_type'        => 'oldPrice',
                    'include_container' => true,
                    'skip_adjustments'  => false
                ]); ?>
            </span>
    </div>
<?php else :?>
    <?= /* @noEscape */ $block->renderAmount($finalPriceModel->getAmount(), [
        'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
        'price_type'        => 'finalPrice',
        'include_container' => true,
        'schema' => $schema
    ]); ?>
<?php endif; ?>

    <?php if ($block->showMinimalPrice()) :?>
    <?php if ($block->getUseLinkForAsLowAs()) :?>
        <a href="<?= $block->escapeUrl($block->getSaleableItem()->getProductUrl()) ?>" class="minimal-price-link">
            <?= /* @noEscape */ $block->renderAmountMinimal() ?>
        </a>
    <?php else :?>
        <span class="minimal-price-link">
            <?= /* @noEscape */ $block->renderAmountMinimal() ?>
        </span>
    <?php endif?>
<?php endif; ?>
<?php else: ?>
    <div class="call-for-price text-red font-semibold">
        <?= $clickForPrice ? __('Click for Price') : __('Call for Price') ?>
    </div>
<?php endif; ?>
