<?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;
    $imageHelper = $this->helper('Magento\Catalog\Helper\Image');
    $quickShopHelper = $this->helper('Codazon\QuickShop\Helper\Data');
    $labelHelper = $this->helper('Codazon\ProductLabel\Helper\Data');
    $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');
    $wishlistHelper = $this->helper('Magento\Wishlist\Helper\Data');
    $showDescription = $this->isShow('description');
    $showSku = $this->isShow('sku');
    $showWishlist = $wishlistHelper->isAllow() && $this->isShow('wishlist');
    $showCompare = $block->getAddToCompareUrl() && $this->isShow('compare');
    $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');
    $width = $block->getData('thumb_width')?:150;
    $height = $block->getData('thumb_height')?:150;
    $paddingBottom = $height*100/$width;
    $totalRows = (int)$this->getData('total_rows')?:2;
    $totalCols = (int)$this->getData('total_cols');
    $cartWidget = $block->isRedirectToCartEnabled()?'':'data-mage-init=\'{"catalogAddToCart":{}}\'';
    $swatchesRender = $this->getSwatchesBlock();
    $wlLabel = $block->escapeHtml(__('Add to Wish List'));
    $cpLabel = $block->escapeHtml(__('Add to Compare'));
    $cartLabel = $block->escapeHtml(__('Add to Cart'));
    $formKey = $block->getBlockHtml('formkey');
    $qtyValidator = $block->escapeHtml(json_encode($block->getQuantityValidators()));
    ?>
    <div class="grid-style-10 products wrapper <?= $viewMode; ?> products-<?= $viewMode; ?>">
        <div class="products list items product-items">
            <?php $i = 0; foreach ($_productCollection as $_product): ?>
            <div class="item product product-item">
                <div class="product-item-info" data-container="product-grid">
                    <?php if ($showThumb) : ?>
                    <div class="product-line-grid-left col-lg-8 col-md-8 col-sm-6 col-xs-6">
                        <a href="<?= $_product->getProductUrl() ?>" class="product photo product-item-photo">
                            <span class="product-image-container">
                                <span class="product-image-wrapper" style="padding-bottom: <?= $paddingBottom ?>%;"><?= $block->getImageHtml($_product, $width, $height, $imageHelper) ?></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 product-line-grid-body col-lg-16 col-md-16 col-sm-18 col-xs-18">
                        <div class="detail-items-wrap">
                            <div class="product-box">
                                <?php if ($showName): ?><?php $_productNameStripped = $block->stripTags($_product->getName(), null, true); ?>
                                <strong class="product name product-item-name">
                                    <a title="<?= $_productNameStripped ?>" class="product-item-link" href="<?= $_product->getProductUrl(); ?>"><?= $this->subString($_productNameStripped, 30); ?></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; ?>
                                <?= $showPrice?$block->getProductPrice($_product):''; ?>
                                <?= $showReview?$block->getReviewsSummaryHtml($_product, $templateType):''; ?>
                            </div>
                            <?= ($_product->getTypeId() === 'configurable')?$swatchesRender->setProduct($_product)->toHtml():''; ?>
                            <?php echo $block->getProductDetailsHtml($_product); ?>
                        </div>
                        <?php if ($showCart) : ?>
                        <div class="product-hovered-section">
                            <div class="button-hover-wrap">
                                <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><?= __('Qty') ?></span></label>
                                            <div class="control">
                                                <div class="qty-ctl">
                                                    <button title="<?= __('Decrease') ?>" type="button" data-role="change_cart_qty" class="decrease" data-qty="-1"></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 title="<?= __('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 if ($showCompare): ?>
                                        <a href="#" class="action tocompare" data-post='<?= $compareHelper->getPostDataParams($_product);?>' title="<?= $cpLabel ?>"><span><?= $cpLabel ?></span></a>
                                        <?php endif; ?>
                                    <?php else: ?>
                                        <button class="action tocart primary disabled stock unavailable" disabled><span><?= __('Out of stock') ?></span></button>
                                        <?php if ($showCompare): ?>
                                        <a href="#" class="action tocompare" data-post='<?= $compareHelper->getPostDataParams($_product);?>' title="<?= $cpLabel ?>"><span><?= $cpLabel ?></span></a>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        <?php endif ?>
					</div>
                </div>
            </div>
            <?php $i++; endforeach; ?>
        </div>
    </div>
<?php endif; ?>
