<?php
  $this->headScript()->appendFile('facets.js');

  // Save results/options to $this so they are available to sub-templates:
  $this->results = $results = $this->recommend->getResults();
  $this->options = $options = $this->searchOptions($this->searchClassId);
?>
<?php if ($results->getResultTotal() > 0): ?>
  <div class="narrow-search"><h4><?=$this->transEsc(isset($this->overrideSideFacetCaption) ? $this->overrideSideFacetCaption : 'Narrow Search')?></h4></div>
<?php endif; ?>
<?php $checkboxFilters = $results->getParams()->getCheckboxFacets(); ?>
<?php $checkboxesShown = false; ?>
<?php if (count($checkboxFilters) > 0):
    foreach ($checkboxFilters as $current) {
      if ($results->getResultTotal() > 0 || $current['selected'] || $current['alwaysVisible']) {
        $checkboxesShown = true;
        break;
      }
    }
  ?>
  <?php if ($checkboxesShown):?>
    <div class="checkboxFilter">
      <?=$this->context($this)->renderInContext('Recommend/SideFacets/checkbox-filters.phtml', ['checkboxFilters' => $checkboxFilters, 'results' => $results]); ?>
    </div>
  <?php endif; ?>
<?php endif; ?>
<?php $extraFilters = isset($this->extraSideFacetFilters) ? $this->extraSideFacetFilters : []; ?>
<?php $collapsedFacets = $this->recommend->getCollapsedFacets() ?>
<?php $filterList = array_merge($results->getParams()->getFilterList(true), $extraFilters); ?>
<?php if (!empty($filterList)): ?>
  <?=$this->context($this)->renderInContext('Recommend/SideFacets/filter-list.phtml', [
    'collapsedFacets' => $collapsedFacets,
    'extraFilters' => $extraFilters,
    'filterList' => $filterList,
  ]); ?>
<?php endif; ?>
<?= isset($this->sideFacetExtraControls) ? $this->sideFacetExtraControls : '' ?>
<?php $sideFacetSet = $this->recommend->getFacetSet(); $rangeFacets = $this->recommend->getAllRangeFacets(); ?>
<?php $hierarchicalFacets = $this->recommend->getHierarchicalFacets() ?>
<?php $hierarchicalFacetSortOptions = $this->recommend->getHierarchicalFacetSortOptions() ?>
<?php if (!empty($sideFacetSet) && $results->getResultTotal() > 0): ?>
  <?php foreach ($sideFacetSet as $title => $cluster): ?>
    <div class="facet-group" id="side-panel-<?=$this->escapeHtmlAttr($title) ?>">
      <div class="title<?php if(in_array($title, $collapsedFacets)): ?> collapsed<?php endif ?>" data-toggle="collapse" href="#side-collapse-<?=$this->escapeHtmlAttr($title) ?>" >
        <?=$this->transEsc($cluster['label'])?>
      </div>
      <div id="side-collapse-<?=$this->escapeHtmlAttr($title) ?>" class="collapse<?php if(!in_array($title, $collapsedFacets)): ?> in<?php endif ?>">
        <?php if (isset($rangeFacets[$title])): ?>
          <?=$this->context($this)->renderInContext('Recommend/SideFacets/range-slider.phtml', ['title' => $title, 'facet' => $rangeFacets[$title]]); ?>
        <?php else: ?>
          <?php $contextVars = [
            'allowExclude' => $this->recommend->excludeAllowed($title),
            'title' => $title,
            'sortOptions' => isset($hierarchicalFacetSortOptions[$title]) ? $hierarchicalFacetSortOptions[$title] : '',
            'collapsedFacets' => $collapsedFacets
          ]; ?>
          <?php if (in_array($title, $hierarchicalFacets)): ?>
            <?=$this->context($this)->renderInContext('Recommend/SideFacets/hierarchical-facet.phtml', $contextVars); ?>
            <noscript>
          <?php endif; ?>
          <?php $contextVars = [
            'options' => $options,
            'allowExclude' => $this->recommend->excludeAllowed($title),
            'facets_before_more' => $this->recommend->getShowMoreSetting($title),
            'showMoreInLightbox' => $this->recommend->getShowInLightboxSetting($title)
          ]; ?>
          <?=$this->context($this)->renderInContext('Recommend/SideFacets/cluster-list.phtml', array_merge($contextVars, ['title' => $title, 'cluster' => $cluster])); ?>
          <?php if (in_array($title, $hierarchicalFacets)): ?>
            </noscript>
          <?php endif; ?>
        <?php endif; ?>
      </div>
    </div>
  <?php endforeach; ?>
<?php endif; ?>
