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

// @codingStandardsIgnoreFile

/* @var $block \Magento\Catalog\Block\Product\AbstractProduct */
?>
<?php
use Magento\Framework\App\Action\Action;
?>
<?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();

            $showAddTo = true;
            $showCart = false;
            $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();

            $showAddTo = true;
            $showCart = true;
            $templateType = null;
            $description = false;
        }
    break;

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

            $image = 'upsell_products_list';
            $title = __('We found other products you might like!');
            $items = $block->getAllItems();
            $limit = $block->getPositionLimit();
            $shuffle = (int) $block->isShuffled();

            $showAddTo = false;
            $showCart = false;
            $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 = __('We found other products you might like!');
            $items = $block->getItemCollection()->getItems();
            $limit = $block->getItemLimit('upsell');
            $shuffle = 0;

            $showAddTo = 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();

            $showAddTo = 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();

            $showAddTo = 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;

            $showAddTo = 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
        $imageHelper = $this->helper('Magento\Catalog\Helper\Image');
        $themeHelper = $this->helper('Codazon\ThemeLayoutPro\Helper\Data');
        $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');
        $wishlistHelper = $this->helper('Magento\Wishlist\Helper\Data');
        
        $width = $themeHelper->getProductImageSize('width');
        $height = $themeHelper->getProductImageSize('height');
        $paddingBottom = $height*100/$width;
        $cartWidget = $block->isRedirectToCartEnabled()?'':'data-mage-init=\'{"catalogAddToCart":{}}\'';
        $showWishlist = $showAddTo && (bool)($themeHelper->isDisplayOnList('wishlist')) && $wishlistHelper->isAllow();
        $showCompare = $showAddTo && (bool)($themeHelper->isDisplayOnList('compare')) && $block->getAddToCompareUrl();
        $showCart = $showCart && (bool)($themeHelper->isDisplayOnList('addtocart'));
        $count1 = count($items)+1;
    ?>
    <?php if ($type == 'related' || $type == 'upsell'): ?>
        <?php if ($type == 'related'): ?>
            <div class="block <?php echo $class; ?>" data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox", "selectAllLink": "[role=select-all-related]"}}' data-limit="<?php echo $limit; ?>" data-shuffle="<?php echo $shuffle; ?>">
        <?php else: ?>
            <div class="block <?php echo $class; ?>" data-mage-init='{"upsellProducts":{}}' data-limit="<?php echo $limit; ?>" data-shuffle="<?php echo $shuffle; ?>">
        <?php endif; ?>
    <?php else: ?>
        <div class="block <?php echo $class; ?>">
    <?php endif; ?>
    <div class="block-title title">
        <strong id="block-<?php echo $class?>-heading" role="heading" aria-level="2"><?php echo $title; ?></strong>
    </div>
    <div class="block-content content" aria-labelledby="block-<?php echo $class?>-heading">
        <?php if ($type == 'related' && $canItemsAddToCart): ?>
        <div class="block-actions">
            <?php echo __('Check items to add to the cart or') ?>
            <button type="button" class="action select" role="select-all-related"><?php echo __('select all') ?></button>
        </div>
        <?php endif; ?>
        <div class="products wrapper base-small-list products-list products-<?php echo $type; ?>">
            <div class="products list items product-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 echo($iterator++ == 1) ? '<div class="item product product-item" style="display: none;">' : '</div><div class="item product product-item" style="display: none;">' ?>
                <?php else: ?>
                    <?php echo($iterator++ == 1) ? '<div class="item product product-item">' : '</div><div class="item product product-item">' ?>
                <?php endif; ?>
                <div class="product-item-info <?php echo $available; ?>">
                    <div class="product-item-top">
                        <a href="<?php echo $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
                            <span class="product-image-container">
                                <span class="product-image-wrapper" style="padding-bottom: <?= $paddingBottom ?>%;"><?= $themeHelper->getProductImageHtml($_item, $width, $height, $imageHelper, $block) ?></span>
                            </span>
                        </a>
                        <?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
                            <?php if (!$_item->getRequiredOptions()): ?>
                                <div class="field choice related"><input
                                        type="checkbox"
                                        class="checkbox related js-cdz-checkbox"
                                        id="related-checkbox<?= $block->escapeHtmlAttr($_item->getId()) ?>"
                                        name="related_products[]"
                                        value="<?= $block->escapeHtmlAttr($_item->getId()) ?>" />
                                    <label
                                        class="label"
                                        for="related-checkbox<?= $block->escapeHtmlAttr(
                                            $_item->getId()
                                        ) ?>"><span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
                                    </label>
                                </div>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                    <div class="product details product-item-details">
                        <strong class="product name product-item-name"><a class="product-item-link" title="<?php echo $block->escapeHtml($_item->getName()) ?>" href="<?php echo $block->getProductUrl($_item) ?>">
                            <?php echo $block->escapeHtml($_item->getName()) ?></a>
                        </strong>
                        <?php echo $block->getProductPrice($_item); ?>
                            <?php if ($templateType): ?>
                                <?php echo $block->getReviewsSummaryHtml($_item, $templateType) ?>
                            <?php endif; ?>
                            
                            <?php if ($showAddTo || $showCart): ?>
                            <div class="product-item-inner">
                                <div class="product actions product-item-actions">
                                    <?php if ($showCart): ?>
                                    <?php if ($_item->isSaleable()): ?>
                                        <form <?= $cartWidget ?> data-role="tocart-form" action="<?= $block->getAddToCartUrl($_item); ?>" method="post">
                                            <?php echo $block->getBlockHtml('formkey')?>
                                            <input type="hidden" name="product" value="<?= $_item->getId() ?>">
                                            <button type="submit"
                                                    title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>"
                                                    class="action tocart primary">
                                                <span><?= __('Add to Cart') ?></span>
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <?php if ($_item->getIsSalable()): ?>
                                            <button type="button"
                                                    class="action tocart primary stock available">
                                                <span><<?php echo __('In stock') ?></span>
                                            </button>
                                        <?php else: ?>
                                            <button type="button"
                                                    class="action tocart primary stock unavailable disabled" disabled >
                                                <span><?php echo __('Out of stock') ?></span>
                                            </button>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                    <?php endif; ?>
                                    <div class="secondary-addto-links actions-secondary" data-role="add-to-links">
                                        <?php if ($showWishlist): ?>
                                        <a href="#"
                                           data-post='<?= $block->getAddToWishlistParams($_item); ?>'
                                           class="action towishlist" data-action="add-to-wishlist"
                                           title="<?= __('Add to Wish List') ?>">
                                            <span><?= __('Add to Wish List') ?></span>
                                        </a>
                                        <?php endif; ?>
                                        <?php if ($showCompare): ?>
                                        <a href="#" class="action tocompare" data-post='<?= $compareHelper->getPostDataParams($_item);?>' title="<?= __('Add to Compare') ?>">
                                            <span><?= __('Add to Compare') ?></span>
                                        </a>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php echo($iterator == $count1) ? '</div>' : '' ?>
                <?php endforeach ?>
            </div>
        </div>
    </div>
</div>
<?php endif;?>
