<?php
/**
 * Copyright © 2017 Codazon, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
use Magento\Framework\App\Action\Action;

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Product list template
 *
 * @var $block \Magento\Catalog\Block\Product\ListProduct
 */
?>
<?php
$_productCollection = $block->getProductCollection();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
?>
<?php if (!$_productCollection->count()): ?>
    <div class="message info empty"><div><?php /* @escapeNotVerified */ echo __('We can\'t find products matching the selection.') ?></div></div>
<?php else: ?>
    <?php
    $viewMode = 'grid';
    $image = 'category_page_grid';
    $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');
    $wishlistHelper = $this->helper('Magento\Wishlist\Helper\Data');
    $imageHelper = $this->helper('Magento\Catalog\Helper\Image');
    $quickShopHelper = $this->helper('Codazon\QuickShop\Helper\Data');
    $labelHelper = $this->helper('Codazon\ProductLabel\Helper\Data');
    $showDescription = $this->isShow('description');
    $showSku = $this->isShow('sku');
    $showWishlist = $this->isShow('wishlist') && $wishlistHelper->isAllow();
    $showCompare = $this->isShow('compare') && $block->getAddToCompareUrl();
    $showCart = $this->isShow('addtocart');
    $showQuickShop = $this->isShow('quickshop');
    $showLabel = $this->isShow('label');
    $showThumb = $this->isShow('thumb');
    $showName = $this->isShow('name');
    $showReview = $this->isShow('review');
    $showPrice = $this->isShow('price');
    $showSlider = $this->getData('show_slider');
    $width = $block->getData('thumb_width')?:200;
    $height = $block->getData('thumb_height')?:200;
    $largeWidth = 700;
    $largeHeight = $height*$largeWidth/$width;
    $paddingBottom = $height*100/$width;
    $totalRows = (int)$this->getData('total_rows')?:2;
    $totalCols = (int)$this->getData('total_cols');
    $widget = ['themewidgets' => [
        'codazon.isogrid' => [
            'sliderConfig' => $this->getSliderData(),
            'colWidth'     => [1 => '46%', 2 => '20%', 3 => '20%']
        ]
    ]];
    $cartWidget = $block->isRedirectToCartEnabled()?'':'data-mage-init=\'{"catalogAddToCart":{}}\'';
    $wlLabel = $block->escapeHtml(__('Add to Wish List'));
    $cpLabel = $block->escapeHtml(__('Add to Compare'));
    $cartLabel = $block->escapeHtml(__('Add to Cart'));
    $formKey = $block->getBlockHtml('formkey');
    $gallery = $block->getLayout()->createBlock('Magento\Framework\View\Element\Template')
        ->setTemplate('Codazon_ProductFilter::elements/gallery.phtml')
        ->addData([
            'image_width'       => $largeWidth,
            'image_height'      => $largeHeight,
            'small_width'       => 200,
            'moreview_settings' => [
                'items'     => 4,
                'nav'       => 1,
                'dots'      => 0,
                'margin'    => 10
            ] 
        ]);
    $qtyValidator = $block->escapeHtml(json_encode($block->getQuantityValidators()));
    ?>
    <div class="grid-style-09 products wrapper <?= $viewMode; ?> products-<?= $viewMode; ?>">
        <div class="products list items product-items hidden" data-mage-init='<?= json_encode($widget) ?>'>
            <?php $i = 0; foreach ($_productCollection as $_product): ?>
            <div class="item product product-item cdz-transparent">
                <div class="product-item-info" data-container="product-grid" data-hasoptions='<?= $_product->getHasOptions()? '1':'0' ?>'>
                    <?php if ($showThumb) : ?>
                    <?php
                        $mainImage = $imageHelper->init($_product, 'category_page_grid')->setImageFile($_product->getData('small_image'));
                        $mainImageLabel = $block->escapeHtmlAttr($mainImage->getLabel());
                        $mainImageUrl = $mainImage->resize($width, $height)->getUrl();
                        $largeMainImageUrl = $imageHelper->init($_product, 'category_page_grid')->setImageFile($_product->getData('small_image'))->resize($largeWidth, $largeHeight)->getUrl();
                        $hoveredImage = $imageHelper->init($_product, 'category_page_grid')->setImageFile($_product->getData('thumbnail'))->resize($width, $height);
                        $hoveredImageUrl = $hoveredImage->getUrl();
                        $largeHoveredImageUrl = $imageHelper->init($_product, 'category_page_grid')->setImageFile($_product->getData('thumbnail'))->resize($largeWidth, $largeHeight)->getUrl();
                    ?>
                    <div class="product-item-top">
                        <a href="<?= $_product->getProductUrl() ?>" class="product photo product-item-photo">
                            <span class="product-image-container">
                                <span class="product-image-wrapper" style="padding-bottom: <?= $paddingBottom ?>%;">
                                    <img data-largeimg="<?= $largeMainImageUrl ?>" data-smallimage="<?= $mainImageUrl ?>" class="product-image-photo main-img" alt="<?= $mainImageLabel ?>" /><img data-largeimg="<?= $largeHoveredImageUrl ?>" data-smallimage="<?= $hoveredImageUrl ?>" class="product-image-photo hovered-img" alt="<?= $mainImageLabel ?>" />
                                </span>
                            </span>
                        </a>
                        <?= $showQuickShop?$quickShopHelper->getQuickShopButton($_product):''; ?>
                        <?= $showLabel?$labelHelper->showLabel($_product):''; ?>						
                        <?php if ($showWishlist): ?>
                            <a href="#" data-post='<?= $block->getAddToWishlistParams($_product); ?>'
                               class="action towishlist" data-action="add-to-wishlist"
                               title="<?= $wlLabel ?>"><span><?= $wlLabel ?></span></a>
                        <?php endif; ?>   
                    </div>
                    <?php endif ?>
                    <div class="product details product-item-details">
                        <?= $gallery->setProduct($_product)->toHtml(); ?>
                        <div class="detail-items-wrap">
                            <div class="product-box">
                                <?php if ($showName): ?>
                                <strong class="product name product-item-name">
                                    <a class="product-item-link" href="<?= $_product->getProductUrl(); ?>">
                                        <?= $_helper->productAttribute($_product, $_product->getName(), 'name'); ?>
                                    </a>
                                </strong>
                                <?php endif ?>
                                <?php if ($showSku) : ?>
                                <div class="product-item-sku"><span class="label"><?= __('SKU: ') ?></span><span class="value"><?= $_product->getData('sku') ?></span></div>
                                <?php endif ?>
                                <?php if ($showDescription):?>
                                <div class="product description product-item-description">
                                    <?= $this->subString($_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description'), 30) ?>
                                </div>
                                <?php endif; ?>    
                                <?= $showReview?$block->getReviewsSummaryHtml($_product, $templateType):''; ?>
                            </div>
                            <?php echo $block->getProductDetailsHtml($_product); ?>
                            <?php if ($showCart) : ?>
							 <div class="product-hovered-section">
								<div class="button-hover-wrap">
									<?= $showPrice?$block->getProductPrice($_product):''; ?>
									<div class="cart-qty">
									<?php if ($_product->isSaleable()): ?>
										<?php $postParams = $block->getAddToCartPostParams($_product); ?>
										<form <?= $cartWidget ?> data-role="tocart-form" action="<?= $postParams['action']; ?>" method="post">
											<input type="hidden" name="product" value="<?= $postParams['data']['product']; ?>">
											<input type="hidden" name="<?= Action::PARAM_NAME_URL_ENCODED; ?>" value="<?= $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
											<?= $formKey ?>
											<div class="field qty">
												<label class="label" for="qty"><span><?php echo __('Qty') ?></span></label>
												<div class="control">
													<div class="qty-ctl">
														<button aria-label="<?= __('Decrease') ?>" type="button" data-role="change_cart_qty" class="decrease" data-qty="-1" class="decrease"></button>
													</div>
													<input type="number" class="qty" name="qty" maxlength="12" value="<?= $block->getProductDefaultQty($_product) * 1 ?>" title="<?php echo __('Qty') ?>" class="input-text qty" data-validate="<?= $qtyValidator ?>" />
													<div class="qty-ctl">
														<button aria-label="<?= __('Increase') ?>" type="button" data-role="change_cart_qty" data-qty="1" class="increase"></button>
													</div>
												</div>
											</div>
											<button type="submit" title="<?= $cartLabel ?>" class="action tocart primary"><span><?= $cartLabel ?></span></button>
											<?php if ($showCompare): ?>
											<a href="#" class="action tocompare" data-post='<?= $compareHelper->getPostDataParams($_product);?>' title="<?= $cpLabel ?>"><span><?= $cpLabel ?></span></a>
                                            <?php endif; ?>
										</form>
									<?php else: ?>
										<?php if ($_product->getIsSalable()): ?>
											<button onclick="document.location.href = '<?= $_product->getProductUrl(); ?>'" class="action tocart primary stock available"><span><?= __('In stock') ?></span></button>
										<?php else: ?>
											<button class="action tocart primary disabled stock unavailable" disabled><span><?= __('Out of stock') ?></span></button>
										<?php endif; ?>
										<?php if ($showCompare): ?>
										<a href="#" class="action tocompare" data-post='<?= $compareHelper->getPostDataParams($_product);?>' title="<?= $cpLabel ?>"><span><?= $cpLabel ?></span></a>
										<?php endif; ?>
									<?php endif; ?>									
									</div>
								</div>
							</div>
                            <?php endif ?>
                        </div>
					</div>
                </div>
            </div>
            <?php $i++; endforeach; ?>
        </div>
    </div>
<?php endif; ?>
