<?php declare(strict_types=1); ?>

<div class="row">
  <div class="col">
    <h1 class="page-title"><?php echo xss($viewModel->pageTitle()); ?></h1>
  </div>
</div>

<!-- Advanced search form -->
<div class="row">
  <div class="col">
    <form class="form-horizontal" method="post" role="form" accept-charset="UTF-8">
    
      <!-- Search terms -->
      <div class="col-md-6 offset-md-3 form-group input-group">
        <input type="text" class="form-control is-valid" name="searchTerms" id="searchTerms" 
          <?php if ($searchTerms = $viewModel->searchTermsForForm()): ?>
            value="<?php echo htmlspecialchars($searchTerms, ENT_QUOTES, "UTF-8", false); ?>"
          <?php else: ?>
            placeholder="<?php echo TFISH_SEARCH_ENTER_TERMS; ?>" 
          <?php endif; ?>				   
        required>
      </div>

      <!-- Search type -->
      <div class="col-md-6 offset-md-3 form-group input-group">
        <select class="form-control custom-select is-valid" name="searchType" id="searchType">
          <option <?php if ($viewModel->searchType() === "AND") echo "selected"; ?> value="AND"><?php echo TFISH_SEARCH_ALL; ?></option>
          <option <?php if ($viewModel->searchType() === "OR") echo "selected"; ?> value="OR"><?php echo TFISH_SEARCH_ANY; ?></option>
          <option <?php if ($viewModel->searchType() === "exact") echo "selected"; ?> value="exact"><?php echo TFISH_SEARCH_EXACT; ?></option>	
          </select>
      </div>
      
      <!-- Submit -->
      <div class="form-group col-md-6 offset-md-3 clearfix">
          <input type="hidden" name="action" value="search" />
          <button type="submit" class="btn btn-primary float-left"><?php echo TFISH_SEARCH ?></button>
      </div>
    
    </form>
  </div>
</div>

<?php if (($_REQUEST['action'] ?? false)): ?>
<!-- Search results -->
<div class="row">
  <div class="col">
    <hr>
    <div class="text-left">
        <?php if ($searchResults = $viewModel->searchResults()): ?>
        
        <h2><?php echo xss($viewModel->contentCount()) . ' ' . TFISH_SEARCH_RESULTS . ':'; ?></h2><hr>

        <?php if ($pagination ?? null): ?>
        <div class="row">
          <div class="col d-flex justify-content-end pagination-top clearfix">
            <?php echo $pagination; ?>
          </div>
        </div>
        <?php endif; ?>

        <?php foreach ($searchResults as $key => $content): ?>
        <div class="clearfix">
            <h3><a href="<?php echo xss($content->url()); ?>"><?php echo xss($content->title()); ?></a></h3>
            <?php if ($content->image()): ?>
            <div class="float-left">
                <figure class="figure-left">
                    <a href="<?php echo xss($content->url()); ?>">
                        <img class="img-responsive" src="<?php echo $content->cachedImage(100); ?>" 
                            <?php if ($content->caption()): ?>title="<?php echo $content->caption(); ?>" 
                            alt="<?php echo xss($content->caption()); ?>"<?php endif; ?> />
                    </a>
                </figure>
            </div>
            <?php endif; ?>
            <?php echo $content->teaserForDisplay(); ?>
        </div>
        <?php endforeach; ?>

        <?php if ($pagination ?? null): ?>
        <div class="row">
          <div class="col d-flex justify-content-end pagination-bottom clearfix">
            <?php echo $pagination; ?>
          </div>
        </div>
        <?php endif; ?>

        <?php else: ?>
        <h2><?php echo TFISH_SEARCH_NO_RESULTS; ?></h2><hr>
        <?php endif; ?>
    </div>
  </div>
</div>
<?php endif; ?>
