<?php
    // Set default value if necessary:
    if (!isset($this->searchClassId)) {
        $this->searchClassId = 'SolrAuth';
    }

    // Load search actions and settings (if any):
    $options = $this->searchOptions($this->searchClassId);
    $handlers = $this->searchbox()->getHandlers(
        $this->searchClassId,
        isset($this->searchIndex) ? $this->searchIndex : null
    );
    $handlerCount = count($handlers);
    $basicSearch = $this->searchbox()->combinedHandlersActive() ? 'combined-searchbox' : $options->getSearchAction();
    $searchHome = $options->getSearchHomeAction();
    $advSearch = $options->getAdvancedSearchAction();
    $lastSort = $this->searchMemory()->getLastSort($this->searchClassId);
    $lastLimit = $this->searchMemory()->getLastLimit($this->searchClassId);
    $ignoreHiddenFilterMemory = isset($this->ignoreHiddenFilterMemory) && $this->ignoreHiddenFilterMemory;
    $ignoreHiddenFiltersInRequest = isset($this->ignoreHiddenFiltersInRequest) && $this->ignoreHiddenFiltersInRequest;
    $hiddenFilters = $this->searchtabs()->getHiddenFilters($this->searchClassId, $ignoreHiddenFilterMemory, $ignoreHiddenFiltersInRequest);
    if (empty($hiddenFilters) && !$ignoreHiddenFilterMemory) {
        $hiddenFilters = $this->searchMemory()->getLastHiddenFilters($this->searchClassId);
        if (empty($hiddenFilters)) {
            $hiddenFilters = $this->searchtabs()->getHiddenFilters($this->searchClassId);
        }
    }
    $hiddenFilterParams = $this->searchtabs()->getCurrentHiddenFilterParams($this->searchClassId, $ignoreHiddenFilterMemory, '?');
?>
<?php $searchTabs = $this->searchtabs()->getTabConfig($this->searchClassId, $this->lookfor, $this->searchIndex, $this->searchType, $hiddenFilters); ?>
<?php if ($this->searchType == 'advanced'): ?>
  <div class="navbar-form navbar-left flip">
    <?php $tabs = $this->context($this)->renderInContext('search/searchTabsAuthorities', ['searchTabs' => $searchTabs['tabs']]); ?>
    <?php if (!empty($tabs)): ?><?=$tabs ?><div class="tab-content clearfix"><?php endif; ?>
      <p class="adv_search_terms"><?=$this->transEsc("Your search terms")?> : "<strong><?=$this->escapeHtml($this->lookfor)?></strong>"</p>
      <p class="adv_search_links">
        <a href="<?=$this->url($advSearch)?>?edit=<?=$this->escapeHtmlAttr($this->searchId)?>"><?=$this->transEsc("Edit this Advanced Search")?></a> |
        <a href="<?=$this->url($advSearch) . $hiddenFilterParams?>"><?=$this->transEsc("Start a new Advanced Search")?></a> |
        <a href="<?=$this->url($searchHome) . $hiddenFilterParams?>"><?=$this->transEsc("Start a new Basic Search")?></a>
      </p>
    <?php if (!empty($tabs)): ?></div><?php endif; ?>
  </div>
<?php else: ?>
  <form id="searchForm" class="searchForm navbar-form navbar-left flip" style="padding-top:0px;" method="get" action="<?=$this->url($basicSearch)?>" name="searchForm" autocomplete="off">
    <?php $placeholder = $this->searchbox()->getPlaceholderText(isset($searchTabs['selected']['id']) ? $searchTabs['selected']['id'] : null); ?>
    <input id="searchForm_lookfor" class="searchForm_lookfor form-control search-query<?php if($this->searchbox()->autocompleteEnabled($this->searchClassId)):?> autocomplete searcher:<?=$this->escapeHtmlAttr($this->searchClassId) ?><?php endif ?>" type="text" name="lookfor" value="<?=$this->escapeHtmlAttr($this->lookfor)?>"<?php if ($placeholder): ?> placeholder="<?=$this->transEsc($placeholder) ?>"<?php endif ?> />
    <?php if ($handlerCount > 1): ?>
      <select id="searchForm_type" class="searchForm_type form-control" name="type" data-native-menu="false">
        <?php foreach ($handlers as $handler): ?>
          <option value="<?=$this->escapeHtmlAttr($handler['value'])?>"<?=$handler['selected'] ? ' selected="selected"' : ''?>><?=$handler['indent'] ? '-- ' : ''?><?=$this->transEsc($handler['label'])?></option>
        <?php endforeach; ?>
      </select>
    <?php elseif ($handlerCount == 1): ?>
      <input type="hidden" name="type" value="<?=$this->escapeHtmlAttr($handlers[0]['value'])?>" />
    <?php endif; ?>

    <?php if (count($searchTabs) > 0): ?>
        <?php foreach ($searchTabs as $tab): ?>
            <?php // if ($this->permission()->allowDisplay($tab['permission'])): ?>
		<?php if (isset($tab['label'])) : ?>
	       		 <?php if (strcmp($tab['label'],"Gesamtbestand") == 0 || strcmp($tab['label'],"VET Repository") == 0)  : ?>
				<input type="checkbox" name='filter' value='(format:"Electronic resource" OR  format:"eBook")'  /> <?=$this->transEsc('Filter_electronic_resource')?>
		    		<!-- <input type="checkbox" name='filter' value='format:"Electronic resource"'  /> <?=$this->transEsc('Filter_electronic_resource')?> -->
	        	<?php endif; ?>
		<?php endif; ?>
            <?php // endif; ?>
        <?php endforeach; ?>
    <?php endif; ?>
    <?php  //END  RKE: search Field "Fulltext only ?>

    <button type="submit" class="btn btn-primary"><i class="fa fa-search" aria-hidden="true"></i></button>

    <?php if ($advSearch): ?>
      <a href="<?=$this->url($advSearch) . ((isset($this->searchId) && $this->searchId) ? '?edit=' . $this->escapeHtmlAttr($this->searchId) : $hiddenFilterParams) ?>" class="btn btn-link advancedSearch" rel="nofollow"><?=$this->transEsc("Advanced Search")?></a>
    <?php endif; ?>

 <?= $this->context($this)->renderInContext('search/searchTabsAuthorities', ['searchTabs' => $searchTabs['tabs']]); ?>



    <?php if ($geoUrl = $this->geocoords()->getSearchUrl($options)) : ?>
        <a href="<?=$geoUrl ?>" class="btn btn-link"><?=$this->transEsc('Geographic Search')?></a>
    <?php endif; ?>

    <?php $shards = $options->getShards(); if ($options->showShardCheckboxes() && !empty($shards)): ?>
      <?
      $selectedShards = isset($this->selectedShards)
          ? $this->selectedShards : $options->getDefaultSelectedShards();
      ?>
      <br />
      <?php foreach ($shards as $shard => $val): ?>
        <?php $isSelected = empty($selectedShards) || in_array($shard, $selectedShards); ?>
          <input type="checkbox" <?=$isSelected ? 'checked="checked" ' : ''?>name="shard[]" value='<?=$this->escapeHtmlAttr($shard)?>' /> <?=$this->transEsc($shard)?>
      <?php endforeach; ?>
    <?php endif; ?>
    <?
      $filterDetails = $this->searchbox()->getFilterDetails(
          isset($this->filterList) && is_array($this->filterList) ? $this->filterList : [],
          isset($this->checkboxFilters) && is_array($this->checkboxFilters) ? $this->checkboxFilters : []
      );
    ?>
    <?php if ((isset($hasDefaultsApplied) && $hasDefaultsApplied) || !empty($filterDetails)): ?>
      <?php $defaultFilterState = $options->getRetainFilterSetting() ? ' checked="checked"' : ''; ?>
      <div class="checkbox">
        <label>
          <input onChange="$('.applied-filter').click()" type="checkbox"<?=$defaultFilterState?> class="searchFormKeepFilters"/>
          <?=$this->transEsc("basic_search_keep_filters")?>
        </label>
      </div>
      <div class="hidden">
        <?php foreach ($filterDetails as $current): ?>
          <input class="applied-filter" id="<?=$this->escapeHtmlAttr($current['id'])?>" type="checkbox"<?=$defaultFilterState?> name="filter[]" value="<?=$this->escapeHtmlAttr($current['value'])?>" />
          <label for="<?=$this->escapeHtmlAttr($current['id'])?>"><?=$this->escapeHtml($current['value'])?></label>
        <?php endforeach; ?>
        <?php if (isset($hasDefaultsApplied) && $hasDefaultsApplied): ?>
          <!-- this is a hidden element that flags whether or not default filters have been applied;
               it is intentionally unlabeled, as users are not meant to manipulate it directly. -->
          <input class="applied-filter" id="dfApplied" type="checkbox" name="dfApplied" value="1"<?=$defaultFilterState?> />
        <?php endif; ?>
      </div>
    <?php endif; ?>
    <?php foreach ($hiddenFilters as $key => $filter): ?>
      <?php foreach ($filter as $value): ?>
        <input type="hidden" name="hiddenFilters[]" value="<?=$this->escapeHtmlAttr($key) . ':' . $this->escapeHtmlAttr($value)?>" />
      <?php endforeach; ?>
    <?php endforeach; ?>
    <?
      /* Show hidden field for active search class when in combined handler mode. */
      if ($this->searchbox()->combinedHandlersActive()) {
        echo '<input type="hidden" name="activeSearchClassId" value="' . $this->escapeHtmlAttr($this->searchClassId) . '" />';
      }
      /* Load hidden limit preference from Session */
      if (!empty($lastLimit)) {
        echo '<input type="hidden" name="limit" value="' . $this->escapeHtmlAttr($lastLimit) . '" />';
      }
      if (!empty($lastSort)) {
        echo '<input type="hidden" name="sort" value="' . $this->escapeHtmlAttr($lastSort) . '" />';
      }
    ?>
  </form>
<?php endif; ?>
