<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_QuickCart
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */
use Magento\Catalog\Block\Product\ReviewRendererInterface;
use Magento\Framework\App\ActionInterface;
use Mageplaza\QuickCart\Block\AjaxCart\Popup;
use Mageplaza\QuickCart\Model\Config\Source\BlockType;
use Magento\Catalog\ViewModel\Product\Listing\PreparePostData;

/** @var Popup $block */

$image = 'product_page_image_medium';
$item = $block->getItem(); ?>

<div class="mpajaxcart-popup-success" data-mage-init='{"mpajaxSuccess":
<?= /** @noEscape */ $block->getPopupData() ?>}'>
    <div class="mp-product-item-photo">
        <?= /** @noEscape */ $block->getImage($item, $image)->toHtml() ?>
    </div>
    <div class="mp-product-item-info">
        <div class="mpajaxcart-popup-title">
            <div class="mpajaxcart-popup-label">
                <?= /** @noEscape */ $item->getName() ?>
            </div>
            <span class="mpajaxcart-message"><?= /** @noEscape */ __('has been added to your shopping cart.') ?></span>
        </div>

        <?php if ($block->checkShowControl($item)) : ?>
            <div class="mpajaxcart-control">
                <span class="label">Qty</span>
                <input type="button" value="-" class="mpajaxcart-minus">
                <input type="number" name="qty" class="mpajaxcart-qty" maxlength="12"
                       value="<?= /** @noEscape */ $block->getCurrentQty() ?>" title="Qty">
                <input type="button" value="+" class="mpajaxcart-plus">
                <input type="button" value="Update" class="mpajaxcart-update" style="display: none">
            </div>
        <?php endif; ?>

        <span class="mp-status"></span>

        <div class="mpajaxcart-msqty">
            <label class="label"><?= /** @noEscape */ __('Shopping cart:') ?></label>
            <span class="qty"><?= /** @noEscape */ __('%1 items', $block->getItemsQty()) ?></span>
        </div>
        <div class="mpajaxcart-mssubtotal">
            <label class="label"><?= /** @noEscape */ __('Cart Subtotal:') ?></label>
            <span class="subtotal"><?= /** @noEscape */ $block->getSubtotal() ?></span>
        </div>
    </div>
</div>
<div class="mpajaxcart-success-action">
    <button type="submit" title="Continue shopping" class="action primary continue-shopping">
        <?= /** @noEscape */ __('Continue') ?>
        <span class="mpajaxcart-countdown"></span>
    </button>
    <button type="submit" title="View Cart" class="action primary btn-view-cart">
        <?= /** @noEscape */ __('View Cart') ?>
    </button>
    <button type="submit" title="Proceed to Checkout" class="action primary btn-proceed-checkout">
        <?= /** @noEscape */ __('Proceed to Checkout') ?>
    </button>
</div>
<?php
switch ($type = $block->getType()) {
    case BlockType::RELATED:
        if ($exist = $block->getItems()->getSize()) {
            $type  = BlockType::RELATED;
            $class = $type;
            $image = 'related_products_list';
            $title = $block->getHelperData()->getBlockTitle() ?: __('Related Products');
            $items = $block->getItems();
            $limit = 0;
            $shuffle = 0;
            $canItemsAddToCart = $block->canItemsAddToCart();
            $templateType = null;
        }
        break;

    case BlockType::CROSS_SELL:
        if ($exist = count($block->getItems())) {
            $type  = BlockType::CROSS_SELL;
            $class = $type;
            $image = 'cart_cross_sell_products';
            $title = $block->getHelperData()->getBlockTitle() ?: __('More Choices:');
            $items = $block->getItems();
            $templateType = ReviewRendererInterface::SHORT_VIEW;
            $canItemsAddToCart = false;
        }
        break;

    case BlockType::UP_SELL:
        if ($exist = count($block->getItems())) {
            $type  = BlockType::CROSS_SELL;
            $class = $type;
            $image = 'cart_cross_sell_products';
            $title = $block->getHelperData()->getBlockTitle() ?: __('Up Sell Products');
            $items = $block->getItems();
            $limit = 0;
            $shuffle = 0;
            $canItemsAddToCart = $block->canItemsAddToCart();
            $templateType = null;
        }
        break;

    default:
        $exist = null;
}

$_item = null;
?>
<?php if ($type === BlockType::CMS_STATIC_BLOCK) : ?>
    <?= $block->getChildHtml('cms-static-block') ?>
<?php else : ?>
<?php if ($exist) : ?>
<?php if ($type === BlockType::RELATED) : ?>
<div class="block <?= $block->escapeHtmlAttr($class) ?>"
     data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox"}}'
     data-limit="<?= $block->escapeHtmlAttr($limit) ?>" data-shuffle="<?= /* @noEscape */ $shuffle ?>">
    <?php else : ?>
    <div class="block <?= $block->escapeHtmlAttr($class) ?>">
        <?php endif; ?>
        <div class="block-title title">
            <strong id="block-<?= $block->escapeHtmlAttr($class) ?>-heading" role="heading"
                    aria-level="2"><?= $block->escapeHtml($title) ?></strong>
        </div>
        <div class="block-content content" aria-labelledby="block-<?= $block->escapeHtmlAttr($class) ?>-heading">
            <div class="products wrapper grid products-grid products-<?= $block->escapeHtmlAttr($type) ?>">
                <ol class="products list items product-items">
                    <?php foreach ($items as $_item) : ?>
                        <?php $available = ''; ?>
                        <?php if ($type === BlockType::RELATED && !$_item->isComposite() && $_item->isSaleable()) : ?>
                            <?php if (!$_item->getRequiredOptions()) : ?>
                                <?php $available = 'related-available'; ?>
                            <?php endif; ?>
                        <?php endif; ?>
                        <?php if ($type === BlockType::RELATED) : ?>
                            <li class="item product product-item" style="display: none;">
                        <?php else : ?>
                            <li class="item product product-item">
                        <?php endif; ?>
                        <div class="product-item-info <?= /* @noEscape */
                        $available ?>">
                            <?= /* @noEscape */
                            '<!-- ' . $image . '-->' ?>
                            <a href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>"
                               class="product photo product-item-photo">
                                <?= $block->getImage($_item, $image)->toHtml() ?>
                            </a>
                            <div class="product details product-item-details">
                                <strong class="product name product-item-name"><a
                                        class="product-item-link"
                                        title="<?= $block->escapeHtmlAttr($_item->getName()) ?>"
                                        href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>">
                                        <?= $block->escapeHtml($_item->getName()) ?></a>
                                </strong>

                                <?= /* @noEscape */
                                $block->getProductPrice($_item) ?>

                                <?php if ($templateType) : ?>
                                    <?= $block->getReviewsSummaryHtml($_item, $templateType) ?>
                                <?php endif; ?>

                                <div class="product actions product-item-actions">
                                    <div class="actions-primary">
                                        <?php if ($_item->isSaleable()) : ?>
                                            <?php if (!$_item->getTypeInstance()->isPossibleBuyFromList($_item)) : ?>
                                                <button
                                                    class="action tocart primary"
                                                    data-mage-init='{"redirectUrl": {"url": "<?= $block->escapeUrl($block->getAddToCartUrl($_item)) ?>"}}'
                                                    type="button"
                                                    title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>">
                                                    <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                                </button>
                                            <?php else : ?>
                                                <?php
                                                /** @var $viewModel PreparePostData */
                                                $viewModel = $block->getViewModel();
                                                $postArray = $viewModel->getPostData(
                                                    $block->escapeUrl($block->getAddToCartUrl($_item)),
                                                    ['product' => $_item->getEntityId()]
                                                );
                                                $value     = $postArray['data'][ActionInterface::PARAM_NAME_URL_ENCODED];
                                                ?>
                                                <form data-role="tocart-form"
                                                      data-product-sku="<?= $block->escapeHtmlAttr($_item->getSku()) ?>"
                                                      action="<?= $block->escapeUrl($block->getAddToCartUrl($_item)) ?>"
                                                      method="post">
                                                    <input type="hidden" name="product"
                                                           value="<?= /* @noEscape */
                                                           (int) $_item->getEntityId() ?>">
                                                    <input type="hidden"
                                                           name="<?= /* @noEscape */ ActionInterface::PARAM_NAME_URL_ENCODED ?>"
                                                           value="<?= /* @noEscape */ $value ?>">
                                                    <?= $block->getBlockHtml('formkey') ?>
                                                    <button type="submit"
                                                            title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"
                                                            class="action tocart primary">
                                                        <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                                    </button>
                                                </form>
                                            <?php endif; ?>
                                        <?php else : ?>
                                            <?php if ($_item->getIsSalable()) : ?>
                                                <div class="stock available">
                                                    <span><?= $block->escapeHtml(__('In stock')) ?></span>
                                                </div>
                                            <?php else : ?>
                                                <div class="stock unavailable">
                                                    <span><?= $block->escapeHtml(__('Out of stock')) ?></span>
                                                </div>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </div>
                                    <div class="secondary-addto-links actions-secondary"
                                         data-role="add-to-links">
                                        <?php if ($addToBlock = $block->getChildBlock('addto')) : ?>
                                            <?= $addToBlock->setProduct($_item)->getChildHtml() ?>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        </li>
                    <?php endforeach ?>
                </ol>
            </div>
        </div>
    </div>
    <?php endif; ?>
    <?php endif; ?>
