<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

use Magento\Checkout\Block\Onepage\Link;
use Magento\Framework\Escaper;
use Magento\Framework\App\ObjectManager;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\Pricing\Helper\Data as PricingHelper;

/** @var Escaper $escaper */
/** @var Link $block */

// Get the checkout session and pricing helper using ObjectManager
$checkoutSession = ObjectManager::getInstance()->get(CheckoutSession::class);
$pricingHelper = ObjectManager::getInstance()->get(PricingHelper::class);

// Get the quote from the checkout session
$quote = $checkoutSession->getQuote();

// Calculate the total discount amount by summing up the discount amounts of all items
// Calculate the total discount amount by summing up the discount amounts of all items
$discountAmount = 0;
foreach ($quote->getAllVisibleItems() as $item) {
    $qty = $item->getQty();
    $productOriginalPrice = $item->getProduct()->getPrice();
    $discountedPrice = $item->getPriceInclTax();
    $totalSaving = ($productOriginalPrice - $discountedPrice) * $qty;
    $discountAmount += $totalSaving;
}

// Format the discount amount with currency symbol
$formattedDiscount = $pricingHelper->currency($discountAmount, true, false);
?>
<div class="flex items-center justify-between mt-0 mb-3">
    <span  class="text-red text-base font-medium ">You’ve saved </span>
    <span  class="text-red text-base font-medium "><?= $formattedDiscount ?></span>

</div>
<?php if ($block->isPossibleOnepageCheckout()): ?>
    <a
       href="<?= $escaper->escapeHtmlAttr($escaper->escapeUrl($block->getCheckoutUrl())) ?>"
       title="<?= $escaper->escapeHtmlAttr(__('Proceed to Checkout')) ?>"
       class="checkout btn btn-primary btn-size-lg justify-center py-3 rounded-none"
       id="checkout-link-button"
    >
        <?= $escaper->escapeHtml(__('Proceed to Checkout')) ?>
    </a>
<?php endif ?>
