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