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

// @codingStandardsIgnoreFile

/* @var $block \Magento\Catalog\Block\Product\AbstractProduct */
?>

<?php
switch ($type = $block->getType()) {

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

            $image = 'related_products_list';
            $title = __('Related Products');
            $items = $block->getAllItems();
            $limit = $block->getPositionLimit();
            $shuffle = (int) $block->isShuffled();
            $canItemsAddToCart = $block->canItemsAddToCart();

            $showWishlist = true;
            $showCompare = true;
            $showCart = true;
            $templateType = null;
            $description = false;
        }
    break;

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

            $image = 'related_products_list';
            $title = __('Related Products');
            $items = $block->getItems();
            $limit = 0;
            $shuffle = 0;
            $canItemsAddToCart = $block->canItemsAddToCart();

            $showWishlist = true;
            $showCompare = true;
            $showCart = true;
            $templateType = null;
            $description = false;
        }
    break;

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

            $image = 'upsell_products_list';
            $title = __('upsell products');
            $items = $block->getAllItems();
            $limit = $block->getPositionLimit();
            $shuffle = (int) $block->isShuffled();

            $showWishlist = true;
            $showCompare = true;
            $showCart = true;
            $templateType = null;
            $description = false;
            $canItemsAddToCart = false;
        }
    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';
            $title = __('upsell products');
            $items = $block->getItemCollection()->getItems();
            $limit = $block->getItemLimit('upsell');
            $shuffle = 0;

            $showWishlist = true;
            $showCompare = true;
            $showCart = true;
            $templateType = null;
            $description = false;
            $canItemsAddToCart = false;
        }
    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();

            $showWishlist = true;
            $showCompare = true;
            $showCart = true;
            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
            $description = false;
            $canItemsAddToCart = false;
        }
    break;

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

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

            $showWishlist = true;
            $showCompare = true;
            $showCart = true;
            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
            $description = false;
            $canItemsAddToCart = false;
        }
    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;

            $showWishlist = true;
            $showCompare = true;
            $showCart = true;
            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
            $description = ($mode == 'list') ? true : false;
            $canItemsAddToCart = false;
        }
    break;

    case 'other':
    break;
}
?>

<?php if ($exist):?>

    <?php if ($type == 'related' || $type == 'upsell'): ?>
        <?php if ($type == 'related'): ?>
            <div class="block <?php /* @escapeNotVerified */ echo $class; ?>" data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox"}}' data-limit="<?php /* @escapeNotVerified */ echo $limit; ?>" data-shuffle="<?php /* @escapeNotVerified */ echo $shuffle; ?>">
        <?php else: ?>
            <div class="block <?php /* @escapeNotVerified */ echo $class; ?>" data-mage-init='{"upsellProducts":{}}' data-limit="<?php /* @escapeNotVerified */ echo $limit; ?>" data-shuffle="<?php /* @escapeNotVerified */ echo $shuffle; ?>">
        <?php endif; ?>
    <?php else: ?>
        <div class="block <?php /* @escapeNotVerified */ echo $class; ?>">
    <?php endif; ?>
    <div class="ma-title title">
        <span id="block-<?php /* @escapeNotVerified */ echo $class?>-heading" role="heading" aria-level="2"><?php /* @escapeNotVerified */ echo $title; ?></span>
    </div>
    <div class="block-content content" aria-labelledby="block-<?php /* @escapeNotVerified */ echo $class?>-heading">
        <?php if ($type == 'related' && $canItemsAddToCart): ?>
        <div class="block-actions">
            <?php /* @escapeNotVerified */ echo __('Check items to add to the cart or') ?>
            <button type="button" class="action select" role="select-all"><span><?php /* @escapeNotVerified */ echo __('select all') ?></span></button>
        </div>
        <?php endif; ?>
        <div class="products-slider products wrapper grid products-grid products-<?php /* @escapeNotVerified */ echo $type; ?>">
			<div class="row">
				<ol class="products list items">
					<?php $iterator = 1; ?>
					<?php foreach ($items as $_item): ?>
					<?php $available = ''; ?>
					<?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
						<?php if (!$_item->getRequiredOptions()): ?>
							<?php $available = 'related-available'; ?>
						<?php endif; ?>
					<?php endif; ?>
					<?php if ($type == 'related' || $type == 'upsell'): ?>
						<?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="item product" style="display: none;">' : '</li><li class="item product" style="display: none;">' ?>
					<?php else: ?>
						<?php /* @escapeNotVerified */ echo($iterator++ == 1) ? '<li class="item product">' : '</li><li class="item product ">' ?>
					<?php endif; ?>
					
						<div class="item-inner">
							<div class="product-item-info product-info <?php /* @escapeNotVerified */ echo 	$available; ?>">
								<div class="image-container">
									<?php /* @escapeNotVerified */ echo '<!-- ' . $image . '-->' ?>
									<a href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
										<?php echo $block->getImage($_item, $image)->toHtml(); ?>
									</a>
									<div class="product-action">
										<?php if ($showCart): ?>
											<div class="actions-primary">
												<?php if ($_item->isSaleable()): ?>
													<?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
														<button class="action tocart 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 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 else: ?>
													<?php if ($_item->getIsSalable()): ?>
														<div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
													<?php else: ?>
														<div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
													<?php endif; ?>
												<?php endif; ?>
											</div>
										<?php endif; ?>
									</div>
									<?php if ($showWishlist || $showCompare): ?>
										<div class="add-to-links" data-role="add-to-links">
											<?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow() && $showWishlist): ?>
												<a href="#" data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_item); ?>'  class="action towishlist" data-action="add-to-wishlist" title="<?php /* @escapeNotVerified */ echo __('Add to Wish List') ?>">
													<span><?php /* @escapeNotVerified */ echo __('Add to Wish List') ?></span>
												</a>
											<?php endif; ?>
											<?php if ($block->getAddToCompareUrl() && $showCompare): ?>
												<?php
												$compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');
												?>
												<!--<a href="#" class="action compare"
												   data-post='<?php /* @escapeNotVerified */ //echo $compareHelper->getPostDataParams($_item);?>'
												   data-role="add-to-links"
												   title="<?php /* @escapeNotVerified */ //echo __('Add to Compare'); ?>">
													<span><?php /* @escapeNotVerified */ // echo __('Add to Compare') ?></span>
												</a>-->
											<?php endif; ?>
										</div>
									<?php endif; ?>
								</div>	
								<div class="box-info">
									<div class="product-name">
										<strong class="product name product-item-name"><a class="product-item-link" title="<?php echo $block->escapeHtml($_item->getName()) ?>" href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>">
											<?php echo $block->escapeHtml($_item->getName()) ?></a>
										</strong>

										<?php /* @escapeNotVerified */ echo $block->getProductPrice($_item); ?>

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

										<?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
											<?php if (!$_item->getRequiredOptions()): ?>
												<div class="field choice related">
													<input type="checkbox" class="checkbox related" id="related-checkbox<?php /* @escapeNotVerified */ echo $_item->getId() ?>" name="related_products[]" value="<?php /* @escapeNotVerified */ echo $_item->getId() ?>" />
													<label class="label" for="related-checkbox<?php /* @escapeNotVerified */ echo $_item->getId() ?>"><span><?php /* @escapeNotVerified */ echo __('+ Add to Cart') ?></span></label>
												</div>
											<?php endif; ?>
										<?php endif; ?>
									</div>	
								</div>
							</div>
						</div>
						<?php echo($iterator == count($items)+1) ? '</li>' : '' ?>
					<?php endforeach ?>
				</ol>
			</div>	
        </div>
    </div>
</div>
<?php endif;?>

<script type="text/javascript">
require([
  "jquery",
  "owlcarousel/minjs"
 ], function($,owlCarousel){
   	
      $(".products-slider ol").owlCarousel({
   		items : 4,
		itemsDesktop : [1200,4],
   		itemsDesktopSmall : [992,3],
   		itemsTablet: [767,2],
   		itemsMobile : [400,1],
   		autoPlay : false,
   		slideSpeed : 5000,
   		navigation : false,
   		stopOnHover : true,
   		pagination : false
      });
      
   });
</script>
