<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/* @var $block \Magento\Catalog\Block\Product\AbstractProduct */
?>
<?php $themeSettingConfig = $this->helper('MGS\ThemeSettings\Helper\Config'); ?>
<?php
switch ($type = $block->getType()) {

    case 'related-rule':
        if ($exist = $block->hasItems()) {
            $type = 'related';
            $class = $type;

            $image = 'related_products_list';
            $titleConfig = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_title');
            $title = $titleConfig ? __($titleConfig) : __('Related Products');
            $items = $block->getAllItems();
            $limit = $block->getPositionLimit();
            $shuffle = (int) $block->isShuffled();
            $canItemsAddToCart = $block->canItemsAddToCart();
			$perRow = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_per_row');
            $showAddTo = true;
            $showCart = false;
            $templateType = null;
            $description = false;
            $useSlider = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_slider');
        }
    break;

    case 'related':
        /** @var \Magento\Catalog\Block\Product\ProductList\Related $block */
        if ($exist = $block->getItems()->getSize()) {
            $type = 'related';
            $class = $type;

            $image = 'related_products_list';
            $titleConfig = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_title');
            $title = $titleConfig ? __($titleConfig) : __('Related Products');
            $items = $block->getItems();
            $limit = 0;
            $shuffle = 0;
            $canItemsAddToCart = $block->canItemsAddToCart();
			$perRow = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_per_row');
            $showAddTo = true;
            $showCart = false;
            $templateType = null;
            $description = false;
            $useSlider = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_slider');

        }
    break;

    case 'upsell-rule':
        if ($exist = $block->hasItems()) {
            $type = 'upsell';
            $class = $type;

            $image = 'upsell_products_list';
            $titleConfig = $themeSettingConfig->getStoreConfig('themesettings/product_details/upsell_title');
            $title = $titleConfig ? __($titleConfig) : __('We found other products you might like!');
            $items = $block->getAllItems();
            $limit = $block->getPositionLimit();
            $shuffle = (int) $block->isShuffled();

			$perRow = $themeSettingConfig->getStoreConfig('themesettings/product_details/upsell_per_row');
            $showAddTo = false;
            $showCart = false;
            $templateType = null;
            $description = false;
            $canItemsAddToCart = false;
            $useSlider = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_slider');
        }
    break;

    case 'upsell':
        /** @var \Magento\Catalog\Block\Product\ProductList\Upsell $block */
        if ($exist = count($block->getItemCollection()->getItems())) {
            $type = 'upsell';
            $class = $type;

            $image = 'upsell_products_list';
            $titleConfig = $themeSettingConfig->getStoreConfig('themesettings/product_details/upsell_title');
            $title = $titleConfig ? __($titleConfig) : __('We found other products you might like!');
            $items = $block->getItemCollection()->getItems();
            $limit = $block->getItemLimit('upsell');
            $shuffle = 0;

			$perRow = $themeSettingConfig->getStoreConfig('themesettings/product_details/upsell_per_row');
            $showAddTo = false;
            $showCart = false;
            $templateType = null;
            $description = false;
            $canItemsAddToCart = false;
            $useSlider = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_slider');
        }
    break;

    case 'crosssell-rule':
        /** @var \Magento\Catalog\Block\Product\ProductList\Crosssell $block */
        if ($exist = $block->hasItems()) {
            $type = 'crosssell';
            $class = $type;

            $image = 'cart_cross_sell_products';
            $title = __('More Choices:');
            $items = $block->getItemCollection();

			$perRow = $themeSettingConfig->getStoreConfig('themesettings/product_details/crosssell_per_row');
            $showAddTo = true;
            $showCart = true;
            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
            $description = false;
            $canItemsAddToCart = false;
            $useSlider = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_slider');
        }
    break;

    case 'crosssell':
        /** @var \Magento\Catalog\Block\Product\ProductList\Crosssell $block */
        if ($exist = count($block->getItems())) {
            $type = 'crosssell';
            $class = $type;

            $image = 'cart_cross_sell_products';
            $title = __('More Choices:');
            $items = $block->getItems();

			$perRow = $themeSettingConfig->getStoreConfig('themesettings/product_details/crosssell_per_row');
            $showAddTo = true;
            $showCart = true;
            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
            $description = false;
            $canItemsAddToCart = false;
            $useSlider = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_slider');
        }
    break;

    case 'new':
        if ($exist = $block->getProductCollection()) {
            $type = 'new';
            $mode = 'grid';
            $type = $type . ' ' . $mode;

            $class = 'widget' . ' ' . $type;

            $image = 'new_products_content_widget_grid';
            $title = __('New Products');
            $items = $exist;

            $showAddTo = true;
            $showCart = true;
            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
            $description = ($mode == 'list') ? true : false;
            $canItemsAddToCart = false;
            $useSlider = $themeSettingConfig->getStoreConfig('themesettings/product_details/related_slider');
        }
    break;

    default:
        $exist = null;
}
?>

<?php if ($exist):?>

    <?php if ($type == 'related' || $type == 'upsell'): ?>
        <?php if ($type == 'related'): ?>
            <div class="block <?= /* @escapeNotVerified */ $class ?>" data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox"}}' data-limit="<?= /* @escapeNotVerified */ $limit ?>" data-shuffle="<?= /* @escapeNotVerified */ $shuffle ?>">
        <?php else: ?>
            <div class="block <?= /* @escapeNotVerified */ $class ?>" data-mage-init='{"upsellProducts":{}}' data-limit="<?= /* @escapeNotVerified */ $limit ?>" data-shuffle="<?= /* @escapeNotVerified */ $shuffle ?>">
        <?php endif; ?>
    <?php else: ?>
        <div class="block <?= /* @escapeNotVerified */ $class ?>">
    <?php endif; ?>
    <div class="block-title">
        <strong class="title" id="block-<?= /* @escapeNotVerified */ $class ?>-heading" role="heading" aria-level="2"><?= /* @escapeNotVerified */ $title ?></strong>
    </div>
    <div class="block-content content" aria-labelledby="block-<?= /* @escapeNotVerified */ $class ?>-heading">
        <div class="products wrapper products-list products-<?= /* @escapeNotVerified */ $type ?>">
			<?php $_collectionSize = count($items); ?>
            <div id="product-slider-<?php echo $type ?>" class="products list items product-items  <?php if($useSlider): ?> mgs-owl-carousel owl-carousel <?php endif; ?> list-products">
                <?php $iterator = 1; ?>
                <?php $i=0; foreach ($items as $_item): ?>
                    <?php if ($i++ % $perRow == 0): ?>
                        <div class="list-item">
                    <?php endif ?>
                        <div class="list-content">
                            <div class="product-item-info">
                                <div class="product-image">
                                    <a href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
                                        <?= $block->getImage($_item, $image)->toHtml() ?>
                                    </a>
                                </div>
                                <div class="product details product-item-details">
                                    <h5 class="product name product-item-name"><a class="product-item-link" title="<?= $block->escapeHtml($_item->getName()) ?>" href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>">
                                        <?= $block->escapeHtml($_item->getName()) ?></a>
                                    </h5>

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

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

                                    <?php if ($_item->isSaleable()): ?>
                                        <?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
                                            <button class="action tocart btn-cart primary" data-mage-init='{"redirectUrl": {"url": "<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_item) ?>"}}' type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
                                                <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                            </button>
                                        <?php else: ?>
                                            <?php $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
                                            $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
                                            ?>
                                            <button class="action tocart btn-cart primary"
                                                    data-post='<?php /* @escapeNotVerified */ echo $postData; ?>'
                                                    type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
                                                <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                            </button>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?= ($iterator == count($items)+1) ? '</div>' : '' ?>
                    <?php if ($i % $perRow == 0 || $i == $_collectionSize): ?>
                        </div>
                    <?php endif ?>
                <?php endforeach ?>
            </div>
            <?php if($useSlider): ?>
			    <script type="text/javascript">
					require([
						'jquery',
						'mgsowlcarousel'
					], function ($) {
						$('#product-slider-<?php echo $type ?>').owlCarousel({
							items: 1,
							autoplay: false,
                            autoplayHoverPause: false,
							nav: false,
							dots: true,
							loop: false,
							lazyLoad: true,
                            autoHeight: true,
							responsive:{
								0:{
									items: 1
								},
								768:{
									items: 2
								},
								992:{
									items: 1
								}
							}
						});
					});
				</script>
            <?php endif; ?>
		</div>
    </div>
</div>
<?php endif;?>
