<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Agorae
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

// @codingStandardsIgnoreFile

/**
 * Display some products of seller at product view page
 *
 * @var $block \Webkul\Agorae\Block\Sellerblock
 */
$viewModel = $block->getData("viewModel");
$moduleManager = $viewModel->getModuleManager();
if ($moduleManager->isOutputEnabled('Webkul_Marketplace')) {
$helper = $this->helper('Webkul\Agorae\Helper\Data');
$mpHelper = $this->helper('Webkul\Marketplace\Helper\Data');
$marketplaceModuleEnabled = $helper->isModuleEnabled('Webkul_Marketplace');
$marketplaceModuleOutputEnabled = $helper->isOutputEnabled('Webkul_Marketplace');
$showWishlist = true;
$showCompare = true;

if($marketplaceModuleEnabled && $marketplaceModuleOutputEnabled && $mpHelper->getSellerProfileDisplayFlag()) {
    $_product = $block->getProduct();
    $productId = $_product->getId();

    if($block->isSeller($productId)) {
        $sellerId = $helper->getSellerIdByProductId($productId);
        $products = $block->getSellerProducts($sellerId);
        $proCount = $mpHelper->getSellerProCount($sellerId);
        $showPro=6;
        $maxProducts = $proCount - $showPro;
        $i=0;
?>
        <div class="block block-list block-viewed">
            <div class="wk-agorae-sp-block-container">
                <div class="block-title">
                    <div class="wk-agorae-sp-block-head">
                        <strong>
                            <span><?php echo __('From Sellers Shop') ?></span>
                        </strong>
                    </div>
                </div>
                <div class="block-content">
                    <div class="wk-agorae-sp-block-body">
                        <div class="wk-agorae-sp-image-cont">
                            <?php
                            if($products->getSize()){
                                foreach($products as $product) {
                                    $i++;
                                    $productUrl = $product->getProductUrl();
                                    $smallImageUrl = $block->getLayout()->createBlock(
                                        'Webkul\Agorae\Block\Homepage'
                                    )->getProductImage($product, 'product_small_image');

                                    if($product->getVisibility()==4){ ?>
                                        <div class="wk-agorae-sp-image">
                                            <div class="seller-product-image">
                                                <?php if($smallImageUrl): ?>
                                                    <img src="<?php echo $smallImageUrl; ?>">
                                                <?php endif; ?>
                                            </div>
                                            <div class="wk-agorae-sp-image-foot">
                                                <div class="seller-product-name">
                                                    <a href="<?php echo $productUrl ?>"
                                                        title="<?php echo $product->getName() ?>">
                                                        <?php if(strlen($product->getName()) <= 20) {
                                                            echo $product->getName();
                                                        } else {
                                                            echo substr($product->getName(),0,20).'...';
                                                        }?>
                                                    </a>
                                                </div>
                                                <div class="seller-product-price">
                                                    <?php
                                                        echo $this->getLayout()
                                                        ->getBlock(
                                                            'product.price.render.default'
                                                        )->render(
                                                            'final_price',
                                                            $helper->getProduct($product->getId()),
                                                            ['display_label','include_container']
                                                        );
                                                    ?>
                                                    <?php if ($showWishlist || $showCompare):
                                                            $abstractProductBlock = $block->getLayout()->createBlock(
                                                                'Magento\Catalog\Block\Product\AbstractProduct'
                                                            ); ?>
                                                        <div class="secondary-addto-links actions-secondary" data-role="add-to-links">
                                                            <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()
                                                                    && $showWishlist
                                                                ): ?>
                                                                <a href="#"
                                                                    data-post='<?php echo $abstractProductBlock->getAddToWishlistParams($product); ?>'
                                                                    class="action towishlist"
                                                                    data-action="add-to-wishlist"
                                                                    title="<?php echo __('Add to Wish List') ?>">
                                                                    <span><?php echo __('Add to Wish List') ?></span>
                                                                </a>
                                                            <?php endif; ?>
                                                            <?php if ($abstractProductBlock->getAddToCompareUrl()
                                                                    && $showCompare
                                                                ):
                                                                $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare'); ?>
                                                                <a href="#" class="action tocompare"
                                                                   data-post='<?php echo $compareHelper->getPostDataParams($product);?>'
                                                                   data-role="add-to-links"
                                                                   title="<?php echo __('Add to Compare'); ?>">
                                                                    <span><?php echo __('Add to Compare') ?></span>
                                                                </a>
                                                            <?php endif; ?>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                        </div>
                                        <?php if($i >= $showPro) {break;}
                                    }
                                }
                            }?>
                        </div>
                        <?php if($maxProducts > 0)  { ?>
                            <div class="seller-products-more">
                                <div>+</div>
                                <?php if($maxProducts == 1) {?>
                                    <div><?php echo $maxProducts; echo __(' More Product'); ?></div>
                                <?php } ?>
                                <?php if($maxProducts > 1) {?>
                                    <div><?php echo $maxProducts; echo __(' More Products'); ?></div>
                                <?php } ?>
                            </div>
                        <?php } ?>
                    </div>
                    <div class="wk-agorae-sp-block-foot">
                        <?php $sellerData = $block->getSellerDetailsByProductId($productId);?>
                        <a href="<?php echo $mpHelper->getRewriteUrl(
                                    'marketplace/seller/collection/shop/'
                                    .$sellerData->getShopUrl()); ?>"
                            title="Go to seller's shop">
                            <div class="gotosellercollection">
                                <?php echo __("VIEW ALL AT SELLER'S SHOP") ?>
                            </div>
                        </a>
                    </div>
                </div>
            </div>
        </div>
<?php
    }
}
} ?>
