<?php
/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Jet Theme Backend for Magento 2 (System)
 */
/**
 * @var $block Amasty\JetTheme\Block\Widget\TabWidget
 * @var $escaper Magento\Framework\Escaper
 */

use Magento\Catalog\Block\Product\ReviewRendererInterface;
use Magento\Catalog\Helper\Product\Compare;
use Magento\Framework\App\Action\Action;
use Magento\Wishlist\Helper\Data;

$tabsData = $block->getTabsData();
$widgetTitle = $block->getWidgetTitle();
$widgetUniqueId = $block->getWidgetUniqueId();
$sliderMaxWidth = $block->getSliderMaxWidth();
$slidesToShow = $block->getVisibleQty();
$showArrows = $block->isShowArrows();
$displayArrowsType = $block->displayArrowsType();
$showDots = $block->isShowDots();
$infiniteLoop = $block->isInfinityLoop();
$autoPlay = $block->isAutoPlay();
$autoPlaySpeed = $block->autoPlaySpeed();
$isSimulateTouch = $block->isSimulateTouch();

$mSlidesToShow = $block->getDesktopVisibleQty(); // 640px - 768px
$sSlidesToShow = $block->getTabletVisibleQty(); // 480px - 640px
$xsSlidesToShow = $block->getMobileVisibleQty(); // mobile - 480px

$isAjaxAddToCartEnabled = $block->isAjaxAddToCartEnabled();
$isAjaxAddToCompareEnabled = $block->isEnabledAjaxAddToCompare();
$isAjaxAddToWishlistEnabled = $block->isEnabledAjaxAddToWishlist();

$type = 'widget-product-grid';
$mode = 'grid';
$image = 'amasty_jet_theme_widget_image';
$isEnableOnMobile = false;
$isShowCart = true;
$templateType = ReviewRendererInterface::SHORT_VIEW;

// phpcs:disable
$compareHelper = $this->helper(Compare::class);
$wishlistHelper = $this->helper(Data::class);
$postDataHelper = $this->helper(Magento\Framework\Data\Helper\PostHelper::class);
$_helper = $this->helper(Magento\Catalog\Helper\Output::class);
// phpcs:enable
?>

<div class="amtheme-tabs-widget"
     <?= /* @noEscape */ $sliderMaxWidth ? ('style="max-width: ' . $sliderMaxWidth . 'px"') : '' ?>
     data-mage-init='{"mage/tabs": {"openedState": "-am-active", "active": 0}}'>
    <?php if ($widgetTitle): ?>
        <h3 class="amtheme-heading">
            <?= $escaper->escapeHtml(__($widgetTitle)) ?>
        </h3>
    <?php endif; ?>
    <div class="amtheme-tabs-buttons">
        <?php foreach ($tabsData as $key => $tab): ?>
            <?php if ($tab['title']): ?>
                <div class="amtheme-title" data-role="collapsible">
                    <a class="title"
                       title="<?= $escaper->escapeHtmlAttr(__($tab['title'])) ?>"
                       aria-label="<?= $escaper->escapeHtmlAttr(__($tab['title'])) ?>"
                       href="amtheme-tabs-widget-<?= $escaper->escapeHtmlAttr($widgetUniqueId) ?>-<?= $escaper->escapeHtmlAttr($key) ?>"
                       data-toggle="trigger">
                        <?= $escaper->escapeHtml(__($tab['title'])) ?>
                    </a>
                </div>
            <?php endif; ?>
        <?php endforeach; ?>
    </div>
    <div class="amtheme-tabs-content">
        <?php foreach ($tabsData as $key => $tab): ?>
            <?php if ($tab['title']): ?>
                <div class="block content"
                     id="amtheme-tabs-widget-<?= $escaper->escapeHtmlAttr($widgetUniqueId) ?>-<?= $escaper->escapeHtmlAttr($key) ?>"
                     <?= /* @noEscape */  $key ? 'style="display: none"' : '' ?>
                     data-role="content">
                    <div class="products products-<?= /* @noEscape */ $mode ?> <?= /* @noEscape */ $mode ?> -display-block">
                        <ol class="products product-items amtheme-products-secondary amtheme-slick-slider -full-width <?= $escaper->escapeHtmlAttr(!$block->isShowArrowsSetting() ?: '-slick-arrows-' . $displayArrowsType) ?>"
                            data-mage-init='{"slickSlider": {
                                "isOnMobile": "<?= /* @noEscape */ $isEnableOnMobile ?>",
                                "updateOnResize": true,
                                "responsiveSettings": {
                                    "responsive": [
                                        {
                                            "breakpoint": 768,
                                            "settings": {
                                                "slidesToShow": <?= /* @noEscape */ $mSlidesToShow ?>,
                                                "draggable": "true"
                                            }
                                        },
                                        {
                                            "breakpoint": 640,
                                            "settings": {
                                                "slidesToShow": <?= /* @noEscape */ $sSlidesToShow ?>,
                                                "arrows": "false",
                                                "draggable": "true"
                                            }
                                        },
                                        {
                                            "breakpoint": 480,
                                            "settings": {
                                                "slidesToShow": <?= /* @noEscape */ $xsSlidesToShow ?>,
                                                "arrows": "false",
                                                "draggable": "true"
                                            }
                                        }
                                    ]
                                },
                                "sliderOptions": {
                                    "slidesToShow": <?= /* @noEscape */ $slidesToShow ?>,
                                    "slidesToScroll": 1,
                                    "infinite": <?= /* @noEscape */ $infiniteLoop ?>,
                                    "arrows": <?= /* @noEscape */ $showArrows ?>,
                                    "dots": <?= /* @noEscape */ $showDots ?>,
                                    "autoplay": <?= /* @noEscape */ $autoPlay ?>,
                                    "autoplaySpeed": <?= /* @noEscape */ $autoPlaySpeed ?>,
                                    "draggable": <?= /* @noEscape */ $isSimulateTouch ?>,
                                    "lazyLoad": "ondemand"
                                }
                            }}'>
                            <?php $iterator = 1; ?>
                            <?php foreach ($tab['products'] as $item): ?>
                                <?= /* @noEscape */ ($iterator++ == 1) ? '<li class="item product product-item">' : '</li><li class="item product product-item">' ?>
                                <div class="product-item-info">
                                    <a title="<?= $escaper->escapeHtmlAttr($item->getName()) ?>"
                                       aria-label="<?= $escaper->escapeHtmlAttr($item->getName()) ?>"
                                       href="<?= $escaper->escapeUrl($block->getProductUrl($item)) ?>"
                                       class="product-item-photo">
                                        <?= $block->getImage($item, $image)->toHtml() ?>
                                    </a>
                                    <div class="product details product-item-details">
                                        <span class="product product-item-name -amtheme-name">
                                            <a title="<?= $escaper->escapeHtmlAttr($item->getName()) ?>"
                                               aria-label="<?= $escaper->escapeHtmlAttr($item->getName()) ?>"
                                               href="<?= $escaper->escapeUrl($block->getProductUrl($item)) ?>"
                                               class="product-item-link">
                                                <?= $escaper->escapeHtml($item->getName()) ?>
                                            </a>
                                        </span>
                                        <?php if ($templateType): ?>
                                            <?= /* @noEscape */ $block->getReviewsSummaryHtml($item, $templateType) ?>
                                        <?php endif; ?>
                                        <div class="product-info-wrapper">
                                            <?= /* @noEscape */ $block->getProductPriceHtml($item, $type) ?>
                                            <div class="amtheme-icon-wrapper">
                                                <?php if ($wishlistHelper->isAllow()): ?>
                                                    <?= /* @noEscape */ $block->getWishlistItemHtml($item); ?>
                                                <?php endif; ?>
                                                <?php if ($block->getAddToCompareUrl()): ?>
                                                    <a href="#"
                                                       data-post='<?= /* @noEscape */ $compareHelper->getPostDataParams($item) ?>'
                                                       class="action tocompare amtheme-circle-icon"
                                                        <?php if ($isAjaxAddToCompareEnabled): ?>
                                                            data-amtheme-js="ajax-product-compare"
                                                        <?php endif; ?>
                                                       title="<?= $escaper->escapeHtmlAttr(__('Add to Compare')) ?>"
                                                       aria-label="<?= $escaper->escapeHtmlAttr(__('Add to Compare')) ?>"
                                                       data-action="add-to-compare"
                                                       role="button">
                                                        <svg class="amtheme-icon -hover-bg">
                                                            <use xlink:href="#icon-compare"/>
                                                        </svg>
                                                    </a>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                        <?= /* @noEscape */ $block->getProductDetailsHtml($item) ?>
                                        <?php
                                        $postData = $postDataHelper->getPostData(
                                            $escaper->escapeUrl($block->getAddToCartUrl($item)),
                                            ['product' => (int)$item->getEntityId()]
                                        )
                                        ?>
                                        <div class="product actions product-item-actions">
                                            <div class="actions-primary">
                                                <?php if ($item->isSaleable()): ?>
                                                    <?php $postParams = $block->getAddToCartPostParams($item); ?>
                                                    <form data-role="tocart-form"
                                                          data-product-sku="<?= $escaper->escapeHtmlAttr($item->getSku()) ?>"
                                                          action="<?= $escaper->escapeUrl($postParams['action']) ?>"
                                                          method="post">
                                                        <input type="hidden"
                                                               name="product"
                                                               value="<?= $escaper->escapeHtmlAttr($postParams['data']['product']) ?>">
                                                        <input type="hidden"
                                                               name="<?= /* @noEscape */
                                                               Action::PARAM_NAME_URL_ENCODED ?>"
                                                               value="<?= /* @noEscape */
                                                               $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
                                                        <?= $block->getBlockHtml('formkey') ?>
                                                        <button type="submit"
                                                            <?php if ($isAjaxAddToCartEnabled): ?>
                                                                data-amtheme-js="ajax-tocart"
                                                                data-post='<?= $escaper->escapeHtmlAttr(__($postData)) ?>'
                                                            <?php endif; ?>
                                                                aria-label="<?= $escaper->escapeHtmlAttr(__('Add to Cart')) ?>"
                                                                title="<?= $escaper->escapeHtmlAttr(__('Add to Cart')) ?>"
                                                                class="action tocart primary">
                                                            <?= $escaper->escapeHtml(__('Add to Cart')) ?>
                                                        </button>
                                                    </form>
                                                <?php else: ?>
                                                    <?php if ($item->getIsSalable()): ?>
                                                        <div class="stock available"><?= $escaper->escapeHtml(__('In stock')) ?></div>
                                                    <?php else: ?>
                                                        <div class="stock unavailable"><?= $escaper->escapeHtml(__('Out of stock')) ?></div>
                                                    <?php endif; ?>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <?= ($iterator == count($tab['products']) + 1) ? '</li>' : '' ?>
                            <?php endforeach; ?>
                        </ol>
                    </div>
                </div>
            <?php endif; ?>
        <?php endforeach; ?>
    </div>
</div>

<?php if ($isAjaxAddToCartEnabled): ?>
    <script type="text/x-magento-init">
        {
            "[data-amtheme-js=ajax-tocart]": {
                "amAjaxCart": {
                    "actionUrl": "<?= /* @noEscape */ $block->getAjaxAddToCartUrl() ?>",
                    "classes":{
                        "addToCartButtonDisable": "disabled"
                    }
                }
            }
        }
    </script>
<?php endif; ?>

<?php if ($isAjaxAddToCompareEnabled): ?>
    <script type="text/x-magento-init">
        {
            "[data-amtheme-js=ajax-product-compare]": {
                "amAjaxCompare": {
                    "actionUrl": "<?= /* @noEscape */ $block->getAjaxAddToCompare()?>"
                }
            }
        }
    </script>
<?php endif; ?>

<?php if ($isAjaxAddToWishlistEnabled):?>
    <script type="text/x-magento-init">
        {
            "[data-amtheme-js=ajax-wishlist]": {
                "amAjaxWishlist": {
                    "actionUrl": "<?= /* @noEscape */ $block->getAjaxAddToWishlist(); ?>"
                }
            }
        }
    </script>
<?php endif; ?>
