<?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\Popup\Suggest;
use Magento\Framework\Escaper;

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

$exist = $block->getProductCollection()->getSize();
$templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
$items = $block->getProductCollection();
$image = 'related_products_list';
?>
<?php if ($exist) : ?>

    <?php echo $block->getChildHtml('ajaxcart.popup.ajax.suggest'); ?>
    <div id="ajax-suggest" class="block products-ordered mb-4 bg-gray-100 p-4 order-2 md:order-1"
         x-data="initAjaxSuggest()" x-init="init()">
        <div class="block-title title mb-2 text-center py-5">
            <div id="block-products-ordered-heading" role="heading" aria-level="2" class="text-2xl font-bold font-sen">
                <?= $escaper->escapeHtml($block->getTitle()); ?>
            </div>
        </div>
        <div class="block-content content ">
            <div class="products-grid products-products-ordered ">
                <div>
                    <?php $iterator = 1; ?>
                    <?php $numberItem = count($items); ?>
                    <?php foreach ($items as $_item) : ?>
                        <?php echo($iterator++ == 1) ? '<div class="item mb-2">' : '</div><div class="item mb-2">' ?>
                        <div class="product-item-info flex align-items-center gap-4 p-3 bg-white">
                            <a
                                href="<?= $escaper->escapeUrl($block->getProductUrl($_item)); ?>"
                                class="product photo product-item-photo w-1/4"
                            >
                                <?= $block->getImage($_item, $image)->toHtml(); ?>
                            </a>
                          <div class="mt-2 w-3/4">
                              <div class="product details product-item-details mt-2 min-h-[100px]">
                                 <div>
                                     <strong class="product name product-item-name">
                                         <a
                                             class="product-item-link"
                                             title="<?= $escaper->escapeHtml($_item->getName()); ?>"
                                             href="<?= $escaper->escapeUrl($block->getProductUrl($_item)); ?>"
                                         >
                                             <?= $escaper->escapeHtml($_item->getName()); ?>
                                         </a>
                                     </strong>
                                 </div>
                                  <div class="flex items-center justify-between ">
                                      <?php /* @escapeNotVerified */ echo $block->getProductPrice($_item); ?>
                                      <div class="btn btn-primary justify-center text-sm my-2 rounded-none">
                                          <?php if ($_item->isSaleable()) : ?>
                                              <?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)) : ?>
                                                  <button
                                                      class="action tocart primary"
                                                      type="button"
                                                      title="<?= $escaper->escapeHtml(__('Add to Cart')); ?>"
                                                      data-product-id="<?php echo $block->escapeHtml($_item->getId()); ?>"
                                                  >
                                            <span><?php
                                                echo $block->escapeHtml(__('Add to Cart')); ?></span>
                                                  </button>
                                              <?php else : ?>
                                                  <?php
                                                  $postDataHelper = $block->getPostDataHelper();
                                                  $postData = $postDataHelper->getPostData(
                                                      $block->getAddToCartUrl($_item),
                                                      ['product' => $_item->getEntityId()]
                                                  );
                                                  ?>
                                                  <button
                                                      @click.prevent="addToCart(<?= $escaper->escapeHtmlAttr($postData) ?>)"
                                                      class="action tocart primary"
                                                      type="button" title="<?= $escaper->escapeHtml(__('Add to Cart')); ?>"
                                                  >
                                                      <span><?= $escaper->escapeHtml(__('Add to Cart')); ?></span>
                                                  </button>
                                              <?php endif; ?>
                                          <?php else : ?>
                                              <?php if ($_item->getIsSalable()) : ?>
                                                  <div class="stock available">
                                                      <span><?= $escaper->escapeHtml(__('In stock')); ?></span>
                                                  </div>
                                              <?php else : ?>
                                                  <div class="stock unavailable">
                                                      <span><?= $escaper->escapeHtml(__('Out of stock')); ?></span>
                                                  </div>
                                              <?php endif; ?>
                                          <?php endif; ?>
                                      </div>
                                  </div>
                                  <?php if ($templateType) : ?>
                                      <?php echo $block->getReviewsSummaryHtml($_item, $templateType) ?>
                                  <?php endif; ?>
                              </div>

                          </div>
                        </div>
                        <?php echo($iterator == $numberItem + 1) ? '</li>' : '' ?>
                    <?php endforeach ?>
                </div>
            </div>
        </div>
    </div>

<?php endif; ?>
