<?php
/**
 * BSS Commerce Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://bsscommerce.com/Bss-Commerce-License.txt
 *
 * @category  BSS
 * @package   Bss_AjaxCart
 * @author    Extension Team
 * @copyright Copyright (c) 2022 BSS Commerce Co. ( http://bsscommerce.com )
 * @license   http://bsscommerce.com/Bss-Commerce-License.txt
 */

declare(strict_types=1);

use Bss\AjaxCart\Block\Ajax\Template;
use Magento\Framework\Escaper;

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

$helper = $block->getAjaxHelper();
$imageHelper = $block->getImageHelper();
$resultItem = $block->getItem();
$product = $resultItem->getProduct();
$resultPrice = $helper->getProductTaxDisplayType() == \Magento\Tax\Model\Config::DISPLAY_TYPE_EXCLUDING_TAX ?
    $resultItem->getPrice() : $resultItem->getPriceInclTax();
$weeTax = $block->getWeeHelper()->getWeeeTaxAppliedAmount($resultItem);
if ($weeTax) {
    $resultPrice += $weeTax;
}
$cart = $block->getCartHelper()->getCart();
$showContinue = $helper->isShowContinueBtn();
$showCartInfo = $helper->isShowCartInfo();
$showCheckoutLink = $helper->isShowCheckoutLink();
$countDown = ($helper->getCountDownTime() > 0) ? $helper->getCountDownTime() : 0;
$animation = $helper->getConfigAnimation();
?>
<script>
    function initPopupAjaxCart() {
        return {
            countDown: <?= $block->escapeJsQuote($countDown); ?>,
            viewCartUrl: "<?= $escaper->escapeUrl($block->getUrl('checkout/cart')); ?>",
            typeCountDown: <?= $helper->getCountDownActive()?>,
            checkContinueAction: false,
            init() {
                if (window.count) {
                    clearInterval(window.count);
                }
                let self = this;
                if (this.countDown > 0 && this.typeCountDown > 0) {
                    window.count = setInterval(() => {
                        if (--self.countDown <= 0) {
                            clearInterval(window.count);

                            if (self.typeCountDown === 1) {
                                window.dispatchEvent(new CustomEvent("ajax-cart-popup-close"));
                            } else if (!self.checkContinueAction) {
                                window.location.href = self.viewCartUrl;
                            }
                        }
                    }, 1000);
                }
            }
        }
    }
</script>
<div class="popup" x-data="initPopupAjaxCart()" x-init="init()">

    <div class="lg:flex">
        <div >
            <div class="block  mb-8 ">
                <div class="header text-center py-5 border-b-2 mb-3">
                    <div class="text-2xl font-sen font-bold">
                        <?= $escaper->escapeHtml(__('Added to Basket')); ?>
                    </div>
                </div>
                <div class="flex justify-center p-4 gap-2">
                    <div class="w-1/4">
                        <?php if ($helper->isShowProductImage()) : ?>
                            <div class="product-image">
                                <img src="<?= $escaper->escapeUrl(
                                    $imageHelper->init(
                                        $product,
                                        'category_page_list',
                                        ['height' => $helper->getImageSize(), 'width' => $helper->getImageSize()]
                                    )->getUrl()
                                ); ?>" class="w-full">
                                <?php if ($helper->isShowProductPrice() && $product->getTypeId() != \Magento\GroupedProduct\Model\Product\Type\Grouped::TYPE_CODE) : ?>
                                    <div class="my-2">
                        <span>
                            <?= $escaper->escapeHtml(
                                $block->getPricingHelper()->currency(
                                    $resultPrice,
                                    true,
                                    false
                                )
                            );
                            ?>
                        </span>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                    <div class="w-3/4">
                        <div class="">
                            <a class="text-black text-xl font-sen font-semibold"
                               href="<?= $escaper->escapeUrl($product->getProductUrl()); ?>"
                               title="<?php echo $block->escapeHtml($resultItem->getName()); ?>">
                                <?php echo $block->escapeHtml($resultItem->getName()); ?>

                            </a>
                            <?php echo $block->getRelatedAdded() ? $escaper->escapeHtml(__(' and related products')) : ''; ?>
                        </div>

                        <!--price will show here-->
                        <?php
                        $finalPrice = $product->getFinalPrice();
                        $regularPrice = $product->getPrice();
                        $pricingHelper = $block->getPricingHelper(); // This should already be available in your block
                        ?>

                        <?php if ($finalPrice < $regularPrice): ?>
                            <span class="text-red-600 font-bold font-sen text-xl">
                             <?= $escaper->escapeHtml($pricingHelper->currency($finalPrice, true, false)); ?>
                           </span>
                            <span class="line-through text-gray-500">
                                <?= $escaper->escapeHtml($pricingHelper->currency($regularPrice, true, false)); ?>
                           </span>

                        <?php else: ?>
                            <span class="font-bold font-sen">
                                <?= $escaper->escapeHtml($pricingHelper->currency($finalPrice, true, false)); ?>
                            </span>
                        <?php endif; ?>
                    </div>

                </div>

                <!-- show product list-->
                <?php
                $shownProductIds = [];
                $quoteItems = $cart->getQuote()->getAllVisibleItems();
                ?>
                <div class="cart-items-list mt-4 text-left hidden">
                    <ul class="space-y-2">
                        <?php foreach ($quoteItems as $item):
                            $cartProduct = $item->getProduct();
                            $productId = $cartProduct->getId();

                            // Skip duplicates
                            if (in_array($productId, $shownProductIds)) {
                                continue;
                            }
                            $shownProductIds[] = $productId;

                            // Check if this is the just-added product
                            $isNewlyAdded = ($productId == $resultItem->getProduct()->getId());
                            ?>
                            <li class="flex items-center space-x-2">
                                <input
                                    type="checkbox"
                                    disabled
                                    <?php if ($isNewlyAdded): ?>checked<?php endif; ?>
                                />
                                <label>
                                    <?= $escaper->escapeHtml($cartProduct->getName()); ?>
                                </label>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>

                <div class="p-4">

                    <?php if ($showCartInfo || $showCheckoutLink) : ?>
                        <div class="border-t-2 flex items-center mb-2 pb-2 pt-4 px-4">
                        <span class="relative">
                          <svg xmlns="http://www.w3.org/2000/svg" width="33" height="28" viewBox="0 0 33 28" fill="none">
                           <path d="M8.72292 19.3842H8.72416C8.72519 19.3842 8.72622 19.384 8.72726 19.384H23.108C23.462 19.384 23.7733 19.1385 23.8706 18.7826L27.0433 7.17295C27.1117 6.92266 27.0638 6.65359 26.914 6.44584C26.7641 6.23809 26.5296 6.11586 26.2807 6.11586H6.89365L6.32665 3.4482C6.24588 3.06877 5.92407 2.79883 5.55226 2.79883H0.793181C0.355072 2.79883 0 3.17005 0 3.62809C0 4.08612 0.355072 4.45735 0.793181 4.45735H4.91607C5.01645 4.93007 7.62941 17.2234 7.77978 17.9306C6.93682 18.3137 6.34544 19.1925 6.34544 20.2133C6.34544 21.585 7.41293 22.701 8.72499 22.701H23.108C23.5461 22.701 23.9012 22.3298 23.9012 21.8718C23.9012 21.4138 23.5461 21.0425 23.108 21.0425H8.72499C8.2877 21.0425 7.93181 20.6704 7.93181 20.2133C7.93181 19.7567 8.28646 19.3853 8.72292 19.3842ZM25.2291 7.77438L22.5096 17.7255H9.36118L7.24603 7.77438H25.2291Z" fill="#0A0A0B"/>
                           <path d="M7.93359 25.1885C7.93359 26.5602 9.00108 27.6762 10.3131 27.6762C11.6252 27.6762 12.6927 26.5602 12.6927 25.1885C12.6927 23.8167 11.6252 22.7007 10.3131 22.7007C9.00108 22.7007 7.93359 23.8167 7.93359 25.1885ZM10.3131 24.3592C10.7504 24.3592 11.1063 24.7313 11.1063 25.1885C11.1063 25.6456 10.7504 26.0177 10.3131 26.0177C9.87585 26.0177 9.51995 25.6456 9.51995 25.1885C9.51995 24.7313 9.87585 24.3592 10.3131 24.3592Z" fill="#0A0A0B"/>
                           <path d="M19.1406 25.1885C19.1406 26.5602 20.2081 27.6762 21.5202 27.6762C22.8322 27.6762 23.8997 26.5602 23.8997 25.1885C23.8997 23.8167 22.8322 22.7007 21.5202 22.7007C20.2081 22.7007 19.1406 23.8167 19.1406 25.1885ZM21.5202 24.3592C21.9574 24.3592 22.3133 24.7313 22.3133 25.1885C22.3133 25.6456 21.9574 26.0177 21.5202 26.0177C21.0829 26.0177 20.727 25.6456 20.727 25.1885C20.727 24.7313 21.0829 24.3592 21.5202 24.3592Z" fill="#0A0A0B"/>
                           <circle cx="27" cy="6" r="6" fill="#D70000"/>
                          </svg>
                          <span class="absolute -top-1 bg-red font-medium px-1 rounded-full text-white text-xs right-0"> <?= $cart->getItemsQty() ?></span>
                      </span>
                            <span class="text-xl font-semibold font-sen">
                           My Basket
                       </span>
                        </div>
                        <div class="cart-info px-4">
                            <?php if ($showCartInfo) : ?>
                                <div class="items-count  hidden">
                    <span>
                        <?php
                        if ($cart->getItemsQty() > 1) {
                            echo $escaper->escapeHtml(__('There are '));
                        } else {
                            echo $escaper->escapeHtml(__('There is '));
                        }
                        ?>
                        <a class="text-[#006bb4]"
                           href="<?php echo $escaper->escapeUrl($block->getUrl('checkout/cart')); ?>">
                            <?php
                            echo $escaper->escapeHtml($cart->getItemsQty());
                            if ($cart->getItemsQty() > 1) {
                                echo $escaper->escapeHtml(__(' items'));
                            } else {
                                echo $escaper->escapeHtml(__(' item'));
                            }
                            ?>
                        </a>
                        <?php echo $escaper->escapeHtml(__(' in your cart.')); ?>
                    </span>
                                </div>
                                <div class="font-bold mb-4 subtotal text-lg font-sen">
                                    <?php $displayType = $helper->getSubtotalDisplayType(); ?>
                                    <?php $quote = $block->getCartHelper()->getQuote(); ?>
                                    <?php
                                    $cartSubTotalInclTax = $block->escapeHtml(
                                        $block->getPricingHelper()->currency(
                                            $quote->getGrandTotal(),
                                            true,
                                            false
                                        )
                                    );
                                    $cartSubTotalExclTax = $block->escapeHtml(
                                        $block->getPricingHelper()->currency(
                                            $quote->getSubtotal(),
                                            true,
                                            false
                                        )
                                    );
                                    ?>
                                    <span>
                        <?php
                        if ($displayType == 1) {
                            echo "<div class='flex justify-between items-center px-2 py-1'>";
                            echo "<div>".$escaper->escapeHtml(__('Cart Subtotal: '))."</div>";
                            echo "<div>".$cartSubTotalExclTax."</div>";
                            echo "</div>";
                        } else if ($displayType == 2) {
                            echo "<div class='flex justify-between items-center px-2 py-1'>";
                            echo "<div>".$escaper->escapeHtml(__('Cart Subtotal: '))."</div>";
                            echo "<div>".$cartSubTotalInclTax."</div>";
                            echo "</div>";
                        }
                        ?>
                    </span>
                                    <?php if ($displayType == 3): ?>
                                        <span>
                            <?php echo $escaper->escapeHtml(__('Cart Subtotal: ')); ?>
                            <?php echo $cartSubTotalInclTax; ?>
                        </span><br>
                                        <span>
                            <?php echo $cartSubTotalExclTax . $escaper->escapeHtml(__(' Excl. Tax')); ?>
                        </span>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                            <div class="md:flex items-center gap-2 ">
                                <?php if ($showCheckoutLink) : ?>
                                    <div class="checkout-link text-base w-full mb-2 md:mb-0 font-sen">
                                        <a href="<?= $escaper->escapeUrl($block->getUrl('checkout')); ?>" class="btn btn-primary rounded-none ">
                                            <span><?= $escaper->escapeHtml(__('Go to checkout')); ?></span>
                                        </a>
                                    </div>
                                <?php endif; ?>
                                <?php if ($showContinue) : ?>
                                    <div class=" lg:float-right lg:text-white flex justify-center lg:block w-full">
                                        <button @click="$dispatch('ajax-cart-popup-close'),checkContinueAction=true"
                                                class="btn bg-gray-200 rounded-none text-base continue text-black w-full font-sen"
                                                type="button"
                                                title="<?= $escaper->escapeHtml(__($helper->getBtnContinueText())); ?>"
                                        >
                <span class="mr-1">
                    <?= $escaper->escapeHtml(__($helper->getBtnContinueText())); ?>
                </span>
                                            <?php if ($helper->getCountDownActive() == \Bss\AjaxCart\Model\Config\Source\Countdown::POPUP_COUNTDOWN_CONTINUE_BTN
                                                && $helper->getCountDownTime() > 0) : ?>
                                                <span x-html="countDown"></span>
                                            <?php endif; ?>
                                        </button>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>

            </div>
        </div>
        <div class="md:flex">
            <?php echo $block->getChildHtml('ajaxcart.popup.suggest'); ?>
        </div>


    </div>

    <div class="action clearfix hidden">
        <div class="btn-viewcart lg:float-left text-white lg:w-1/2 mb-3 lg:mb-0 flex justify-center lg:block btn-viewcart
        <?php echo (!$showContinue) ? 'full-width' : ''; ?>">
            <a
                href="<?= $escaper->escapeUrl($block->getUrl('checkout/cart')); ?>"
                title="<?= $escaper->escapeHtml(__($helper->getBtnViewcartText())); ?>"
                class="view-cart"
            >
                <button class="btn text-base viewcart"
                        type="button"
                        @click="$dispatch('ajax-cart-popup-close')"
                        title="<?= $escaper->escapeHtml(__($helper->getBtnViewcartText())); ?>">
                    <span class="mr-1">
                        <?= $escaper->escapeHtml(__($helper->getBtnViewcartText())); ?>
                    </span>
                    <?php if ($helper->getCountDownActive() == \Bss\AjaxCart\Model\Config\Source\Countdown::POPUP_COUNTDOWN_VIEW_CART_BTN
                        && $helper->getCountDownTime() > 0) : ?>
                        <span
                            class="<?php echo ($helper->getCountDownActive() == \Bss\AjaxCart\Model\Config\Source\Countdown::POPUP_COUNTDOWN_VIEW_CART_BTN) ? 'countdown' : ''; ?>">
                            <span x-html="countDown"></span>
                        </span>
                    <?php endif; ?>
                </button>
            </a>
        </div>

        <?php if ($showContinue) : ?>
            <div class="btn-continue lg:float-right lg:text-white flex justify-center lg:block btn-continue">
                <button @click="$dispatch('ajax-cart-popup-close'),checkContinueAction=true"
                        class="btn text-base continue"
                        type="button"
                        title="<?= $escaper->escapeHtml(__($helper->getBtnContinueText())); ?>"
                >
                <span class="mr-1">
                    <?= $escaper->escapeHtml(__($helper->getBtnContinueText())); ?>
                </span>
                    <?php if ($helper->getCountDownActive() == \Bss\AjaxCart\Model\Config\Source\Countdown::POPUP_COUNTDOWN_CONTINUE_BTN
                        && $helper->getCountDownTime() > 0) : ?>
                        <span x-html="countDown"></span>
                    <?php endif; ?>
                </button>
            </div>
        <?php endif; ?>
    </div>
</div>

