<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_BannerSlider
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

use Mageplaza\BannerSlider\Model\Config\Source\Type;

$banners = $block->getBannerCollection();
$check   = is_array($banners) ? !empty($banners) : $banners->getSize();
if ($check) : ?>
    <?php
    $sliderId      = $block->getSliderId() . uniqid('-', false);
    $sliderOptions = $block->getBannerOptions(); ?>
    <div class="carousel-container">
        <div id="banner-slider-carousel">
            <?php $iterator = 1; ?>
            <div id="bannerslider-list-items-<?= /* @noEscape */ $sliderId ?>"
                 class="owl-carousel owl-theme">
                <?php foreach ($banners as $banner) : ?>
                    <?php
                    $bnType  = $banner->getType();
                    $bnTitle = $banner->getTitle();
                    $bnUrl   = $banner->getUrlBanner();
                    $bnImg   = $banner->getImageUrl();
                    ?>
                    <?php if (($bnType === Type::CONTENT && empty($banner->getContent())) || ($bnType === Type::IMAGE && empty($banner->getImage()))) : continue; ?>
                    <?php endif; ?>
                    <?= ($iterator++ === 1) ? '<div class="banner-item banner-slider">' : '</div><div class="banner-item banner-slider">' ?>
                    <?php if ($bnType === Type::CONTENT) : ?>
                        <div class="item-content">
                            <?= /* @noEscape */ $block->getPageFilter($banner->getContent()) ?>
                        </div>
                    <?php else : #image type?>
                        <div class="item-image">
                            <?php if (!empty($bnUrl)) : ?>
                                <a class="owl-image"
                                   href="<?= $block->escapeUrl($bnUrl) ?>" <?= /* @noEscape */ ($banner->getNewtab() === '1') ? 'target="_blank"' : '' ?>>
                                    <?php if (!empty($bnTitle)) : ?>
                                        <div class="banner-title"><?= $block->escapeHtml($bnTitle) ?></div>
                                    <?php endif; ?>
                                    <img class="img-responsive" src="<?= $block->escapeUrl($bnImg) ?>" alt="<?= $block->escapeHtml($bnTitle) ?>">
                                </a>
                            <?php else : ?>
                                <img class="owl-image img-responsive" src="<?= $block->escapeUrl($bnImg) ?>"
                                     title="<?= $block->escapeHtml($bnTitle) ?>" alt="<?= $block->escapeHtml($bnTitle) ?>">
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                    <?= ($iterator === count($banners) + 1) ? '</div>' : '' ?>
                <?php endforeach; ?>
            </div>
        </div>
        <?= $block->getPagerHtml() ?>
    </div>

    <script>
        require(['jquery', 'mageplaza/core/owl.carousel'], function ($) {
            $('#bannerslider-list-items-<?= /* @noEscape */ $sliderId ?>').owlCarousel(<?= /* @noEscape */ $sliderOptions ?>);
        });
    </script>
<?php endif; ?>
