<?php
    use Magento\Framework\App\Action\Action;

    $viewMode = 'grid';
    $image = 'category_page_grid';
    $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    $_product = $this->getLoadProduct($this->getProductId());
    $_productNameStripped = $block->stripTags($_product->getName(), null, true);
    $productImage = $block->getImage($_product, $image);
    $_helper = $this->helper('Magento\Catalog\Helper\Output');
    $builderHelper = $this->helper('MGS\Fbuilder\Helper\Data');
    $hideName = $this->getBlockData('hide_name');
    $hideReview = $this->getBlockData('hide_review');
    $hidePrice = $this->getBlockData('hide_price');
    $hideAddCart = $this->getBlockData('hide_addcart');
    $hideAddWishlist = $this->getBlockData('hide_addwishlist');
    $hideAddCompare = $this->getBlockData('hide_addcompare');
    $hideTime = $this->getBlockData('hide_time');
    $hideDiscount = $this->getBlockData('hide_discount');
    $hideSavedPrice = $this->getBlockData('hide_saved_price');
    $themeSettingConfig = $this->helper('MGS\ThemeSettings\Helper\Config');
    $price = $_product->getOrigData('price');
    $finalPrice = $_product->getFinalPrice();
    $savedPrice = $price - $finalPrice;
    $_imagehelper = $this->helper('MGS\ThemeSettings\Helper\Image');
    $htmlStructure = $builderHelper->getStoreConfig('fbuilder/html_structure/deal_product_grid');
?>
<div class="product-item-info" data-container="product-grid">
        <div class="product-top">
            <?php // Product Image ?>
            <?php $size = $themeSettingConfig->getImageSize();?>
            <?php $padding = $themeSettingConfig->getImagePadding(); ?>

            <?php
            $productImage = $_imagehelper->init($_product, $image)->setImageFile($_product->getData('small_image'))->resize($size['width'], $size['height'])->getUrl();
            $productThumbnail = $_imagehelper->init($_product, $image)->setImageFile($_product->getData('thumbnail'))->resize($size['width'], $size['height'])->getUrl();
            $productImageHover = $_imagehelper->init($_product, 'product_thumbnail_image')->setImageFile($_product->getData('thumbnail'))->resize($size['width'], $size['height'])->getUrl();
            ?>

            <!-- Product Image-->
            <a href="<?php  echo $_product->getProductUrl() ?>" class="product photo product-item-photo <?php if((basename($_product->getData('thumbnail') ?? '') !=  'no_selection') && (basename($_product->getData('thumbnail') ?? '') != basename($_product->getData('small_image') ?? ''))): ?> has-hover-image<?php endif ?>" style="padding-bottom: <?php echo $padding ?>;"  tabindex="-1">

                <img src="<?php echo $productImage ?>" alt="<?php echo $_productNameStripped ?>" class="img-responsive product-image-photo img-thumbnail"  data-src="<?php echo $productImage ?>"/>

                <?php if(basename($_product->getData('thumbnail') ?? '') !=  'no_selection'): ?>
                    <?php if(basename($_product->getData('thumbnail') ?? '') != basename($_product->getData('small_image') ?? '')): ?>
                        <?php
                        $productThumbnail = $_imagehelper->init($_product, $image)->setImageFile($_product->getData('thumbnail'))->resize($size['width'], $size['height'])->getUrl();
                        ?>
                        <img src="<?php echo $productThumbnail ?>" alt="<?php echo $_productNameStripped ?>" class="img-responsive img-hover-show"  data-src="<?php echo $productThumbnail ?>"/>
                    <?php endif ?>
                <?php endif ?>
            </a>

            <?php if ($hideDiscount!=1): ?>
                <?php
                if (($finalPrice<$price)) {
                    $percent = round(($savedPrice * 100) / $price);
                    echo '<span class="product-ribbon sale-ribbon"><span>-'.$percent.'%</span></span>';
                }
                ?>
            <?php endif ?>
            <ul class="actions-link actions-secondary">
                <?php if($hideAddCompare!=1): ?>
                    <?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare'); ?>
                    <li>
                        <button class="action tocompare"
                                title="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                data-title="<?php echo __('Add to Compare') ?>"
                                aria-label="<?php echo $block->escapeHtml(__('Add to Compare')); ?>"
                                data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataParams($_product); ?>'
                                role="button">
                            <i class="pe-7s-graph3"></i>
                        </button>
                    </li>
                <?php endif ?>
                <?php if(($hideAddWishlist!=1)): ?>
                    <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow() && ($hideAddWishlist!=1)): ?>
                        <li>
                            <button class="action towishlactions-secondarist"
                                    title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
                                    data-title="<?php echo __('Add to Wish List') ?>"
                                    aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
                                    data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_product); ?>'
                                    data-action="add-to-wishlist"
                                    role="button">
                                <i class="pe-7s-like"></i>
                            </button>
                        </li>
                    <?php endif ?>
                <?php endif ?>

            </ul>
            <div class="action-mobile">
                <?php if($hideAddCart!=1): ?>
                    <?php if ($_product->isSaleable()): ?>
                        <?php $postParams = $block->getAddToCartPostParams($_product); ?>
                        <form data-role="tocart-form" data-product-sku="<?=  /* @NoEscape */ $_product->getSku() ?>" action="<?= /* @NoEscape */ $postParams['action'] ?>" method="post">
                            <input type="hidden" name="product" value="<?= /* @escapeNotVerified */ $postParams['data']['product'] ?>">
                            <input type="hidden" name="<?= /* @escapeNotVerified */ Action::PARAM_NAME_URL_ENCODED ?>" value="<?= /* @escapeNotVerified */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
                            <?= $block->getBlockHtml('formkey') ?>
                            <?php if($_product->getTypeId() == 'simple'): ?>
                                <button type="submit"
                                        title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"
                                        class="action tocart btn-cart primary">
                                    <span class="icon pe-7s-shopbag"></span>
                                    <span class="text"><?= /* @escapeNotVerified */
                                        __('Add to Cart') ?></span>
                                </button>
                            <?php else: ?>
                                <button type="submit"
                                        title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"
                                        class="action tocart btn-cart primary"
                                        onclick="window.location.href = '<?php echo $block->getProductUrl($_product)?>'">
                                    <span class="icon pe-7s-shopbag"></span>
                                    <span class="text"><?= /* @escapeNotVerified */
                                        __('Add to Cart') ?></span>
                                </button>
                            <?php endif; ?>
                        </form>
                    <?php endif; ?>
                <?php endif ?>
            </div>
            <div class="product details product-item-details">
                <?php if($hideTime!=1): ?>
                    <?php echo $this->getLayout()->createBlock('MGS\Fbuilder\Block\Products\Deals')->setProduct($_product)->setTemplate('MGS_Fbuilder::products/deals/time.phtml')->setBlockData($this->getBlockData())->toHtml() ?>
                <?php endif ?>

                <?php if($hideName!=1): ?>
                    <h5 class="product name product-item-name">
                        <a class="product-item-link"
                           href="<?php echo $_product->getProductUrl() ?>">
                            <?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
                        </a>
                    </h5>
                <?php endif ?>
                <?php if($hideReview!=1): ?>
                    <?php echo $block->getReviewsSummaryHtml($_product, $templateType) ?>
                <?php endif ?>

                <?php if($hidePrice!=1): ?>
                    <?php echo $block->getProductPrice($_product) ?>
                <?php endif ?>

                <?php if($hideSavedPrice!=1): ?>
                    <?php
                    $savedText = '<span class="saved-price">'.$builderHelper->decodeHtmlTag($this->getBlockData('fbuilder_saved_text')).' %1'.'</span>';
                    $formattedPrice = $this->helper('Magento\Framework\Pricing\Helper\Data')->currency($savedPrice, true, true);
                    echo __($savedText, $formattedPrice);
                    ?>
                <?php endif ?>

                <?php echo $this->getLayout()->createBlock('Magento\Swatches\Block\Product\Renderer\Listing\Configurable')->setProduct($_product)->setTemplate('Magento_Swatches::product/listing/renderer.phtml')->toHtml() ?>

            </div>
        </div>
    </div>

