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

$helper = $this->helper('Webkul\AdvancedLayeredNavigation\Helper\Data');
?>
<div class="wk_layer_loader_bg"><span></span></div>
<?php $_filters = $block->getActiveFilters() ?>
<?php if (!empty($_filters)) : ?>
<div class="filter-current">
    <strong class="block-subtitle filter-current-subtitle"
            role="heading"
            aria-level="2"
            data-count="<?= /* @noEscape */ count($_filters); ?>">
            <?= /* @noEscape */   $block->escapeHtml(__('Now Shopping by')) ?></strong>
    <ol class="items">
        <?php foreach ($_filters as $_filter) :
                $value = $_filter->getValue();
                $currenturl = $block->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
            if ($_filter->getName() != 'Price' && strpos($currenturl, '_'.$value.'_') !== false) {
                $url = str_replace('_'.$value.'_', "_", $currenturl);
            } elseif ($_filter->getName() != 'Price' && strpos($currenturl, '_'.$value) !== false) {
                $url = str_replace('_'.$value, "", $currenturl);
            } elseif ($_filter->getName() != 'Price' && strpos($currenturl, $value.'_') !== false) {
                $url = str_replace($value.'_', "", $currenturl);
            } elseif ($_filter->getName() == 'Category') {
                $url = str_replace('cat='.$value, "", $currenturl);
            } else {
                $url = $_filter->getRemoveUrl();
            }
            if ($_filter->getName() == 'Category') {
                $categoryDetail = $helper->getCategory($value);
                ?>
            <li class="item">
                <span class="filter-label"><?= /* @noEscape */ $block->escapeHtml(__($_filter->getName())); ?></span>
                <span class="filter-value">
                <?= /* @noEscape */   $block->escapeHtml($block->stripTags($categoryDetail->getName())) ?>
                </span>
                <?php
                $clearLinkUrl = $_filter->getClearLinkUrl();
                $currentFilterName = $block->escapeHtml(__($_filter->getName())) . " " .
                $block->stripTags($_filter->getLabel());
                if ($clearLinkUrl) :
                    ?>
                    <span class="wk-filter-action action previous" 
                    data-url="<?= /* @noEscape */   $block->escapeHtml($url) ?>"
                       title="<?= /* @noEscape */   $block->escapeHtml(__('Previous')) ?>">
                        <span><?= /* @noEscape */   $block->escapeHtml(__('Previous')) ?></span>
                    </span>
                    <span class="wk-filter-action action remove"
                       title="<?= /* @noEscape */ $block->escapeHtml($_filter->getFilter()->getClearLinkText()) ?>"
                       data-url="<?= /* @noEscape */   $block->escapeHtml($clearLinkUrl) ?>">
                        <span><?= /* @noEscape */ $block->escapeHtml($_filter->getFilter()->getClearLinkText()) ?></span>
                    </span>
                <?php else : ?>
                    <span class="wk-filter-action action remove" 
                    data-url="<?= /* @noEscape */   $block->escapeHtml($url) ?>"
                       title="<?= /* @noEscape */   $block->escapeHtml(__('Remove')) . " " . $block->escapeHtml($currentFilterName);?>">
                        <span><?= /* @noEscape */   $block->escapeHtml(__('Remove This Item')) ?></span>
                    </span>
                <?php endif; ?>
            </li>
            <?php } else { ?>
                    <li class="item">
                <span class="filter-label"><?= /* @noEscape */ $block->escapeHtml(__($_filter->getName())); ?></span>
                <span class="filter-value">
                    <?= /* @noEscape */   $block->escapeHtml($block->stripTags($_filter->getLabel())) ?>
                </span>
                <?php
                $clearLinkUrl = $_filter->getClearLinkUrl();
                $currentFilterName = $block->escapeHtml(__($_filter->getName())) . " " .
                $block->stripTags($_filter->getLabel());
                if ($clearLinkUrl) :
                    ?>
                    <span class="wk-filter-action action previous" 
                    data-url="<?= /* @noEscape */   $block->escapeHtml($url) ?>"
                       title="<?= /* @noEscape */   $block->escapeHtml(__('Previous')) ?>">
                        <span><?= /* @noEscape */   $block->escapeHtml(__('Previous')) ?></span>
                    </span>
                    <span class="wk-filter-action action remove"
                       title="<?= /* @noEscape */ $block->escapeHtml($_filter->getFilter()->getClearLinkText()) ?>"
                       data-url="<?= /* @noEscape */   $block->escapeHtml($clearLinkUrl) ?>">
                        <span><?= /* @noEscape */ $block->escapeHtml($_filter->getFilter()->getClearLinkText()) ?></span>
                    </span>
                <?php else : ?>
                    <span class="wk-filter-action action remove" 
                    data-url="<?= /* @noEscape */   $block->escapeHtml($url) ?>"
                       title="<?= /* @noEscape */   $block->escapeHtml(__('Remove')) . " " . $block->escapeHtml($currentFilterName);?>">
                        <span><?= /* @noEscape */   $block->escapeHtml(__('Remove This Item')) ?></span>
                    </span>
                <?php endif; ?>
            </li>
            <?php  } ?>
        <?php endforeach; ?>
    </ol>
</div>
<?php endif; ?>
<?php
    $params["_use_rewrite"] = true;
    $data = json_encode(["url" => $block->getUrl('advancedlayerednavigation/index/setvalues'),
    "clearUrl" => $block->getUrl('*/*/*', $params)]);
    ?>
<script type="text/x-magento-init">
{
    "body": {
        "Webkul_AdvancedLayeredNavigation/js/layerednavigation": <?= /* @noEscape */   $data ?>
    }
}
</script>
