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

<!-- Container -->
<div class="container">

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

<!-- Advanced search form -->
<div class="row justify-content-center">
  <div class="col-md-4">
    <form class="form-horizontal" action="" method="post" role="form" accept-charset="UTF-8">

      <!-- Search terms -->
      <div class="form-group input-group">
        <input type="text" class="form-control" 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="form-group input-group">
        <select class="form-control custom-select" name="searchType" id="searchType">
          <option <?php if (isset($type) && $type === "AND") echo "selected"; ?> value="AND"><?php echo TFISH_SEARCH_ALL; ?></option>
          <option <?php if (isset($type) && $type === "OR") echo "selected"; ?> value="OR"><?php echo TFISH_SEARCH_ANY; ?></option>
          <option <?php if (isset($type) && $type === "exact") echo "selected"; ?> value="exact"><?php echo TFISH_SEARCH_EXACT; ?></option>
          </select>
      </div>

      <!-- Submit -->
      <div class="form-group clearfix">
          <input type="hidden" name="action" value="search" />
          <button type="submit" class="btn btn-primary float-start"><?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 else: ?>
        <h2><?php echo TFISH_SEARCH_NO_RESULTS; ?></h2><hr>
        <?php endif; ?>
    </div>
  </div>
</div>

<?php if ($searchResults): ?>

<?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; ?>

<div class="row">
  <div class="col main-content clearfix">
  <table class="table table-bordered table-striped table-hover text-left">
    <thead>
      <tr>
        <th class="d-none d-md-table-cell d-lg-table-cell d-xl-table-cell"><?php echo TFISH_ID; ?></th>
        <th><?php echo TFISH_TYPE; ?></th>
        <th><?php echo TFISH_TITLE; ?></th>
        <th class="d-none d-md-table-cell d-lg-table-cell d-xl-table-cell"><?php echo TFISH_SUBMISSION_TIME; ?></th>
        <th class="d-none d-md-table-cell d-lg-table-cell d-xl-table-cell"><?php echo TFISH_EXPIRES; ?></th>
        <th class="d-none d-md-table-cell d-lg-table-cell d-xl-table-cell"><?php echo TFISH_COUNTER; ?></th>
        <th class="text-center"><?php echo TFISH_ONLINE; ?></th>
        <th colspan="2" class="text-center"><?php echo TFISH_ACTION; ?></th>
      </tr>
      </thead>
    <tbody>
    <?php foreach($viewModel->searchResults() as &$content): ?>
      <tr>
        <td class="d-none d-md-table-cell d-lg-table-cell d-xl-table-cell"><?php echo xss($content->id()); ?></td>
        <td><?php echo xss($viewModel->listTypes()[$content->type()]); ?></td>
        <td><a href="<?php echo TFISH_URL . '?id=' . xss($content->id()); ?>"><?php echo xss($content->title()); ?></a></td>
        <td class="d-none d-md-table-cell d-lg-table-cell d-xl-table-cell"><?php echo xss(\date($viewModel->dateFormat(), (int) $content->submissionTime())); ?></td>
        <td class="d-none d-md-table-cell d-lg-table-call d=xl-table-cell">
            <?php if (!empty($content->expiresOn())):
                echo xss($content->expiresOn()->format('j M Y'));
              else:
                echo '-';
              endif;
            ?>
        </td>
        <td class="d-none d-md-table-cell d-lg-table-cell d-xl-table-cell"><?php echo xss($content->counter()); ?></td>
        <?php if ($content->onlineStatus()): ?>
        <td class="text-center">
          <a class="text-success" hx-post="<?php echo TFISH_ADMIN_URL; ?>?action=toggle" target="closest td"
            hx-vals='{"id": "<?php echo xss($content->id()); ?>", "status": "1"}' hx-swap="outerHTML">
            <i class="fas fa-check"></i></a>
          </td>
        <?php else: ?>
        <td class="text-center">
          <a class="text-danger" hx-post="<?php echo TFISH_ADMIN_URL; ?>?action=toggle" target="closest td"
            hx-vals='{"id": "<?php echo xss($content->id()); ?>", "status": "0"}' hx-swap="outerHTML">
            <i class="fas fa-times"></i></a>
        </td>
        <?php endif; ?>
        <td class="text-center"><a href="<?php echo TFISH_ADMIN_URL . 'content/'; ?>?action=edit&amp;id=<?php echo xss($content->id()); ?>"><i class="fas fa-edit"></i></a></td>
        <td class="text-center"><a href="<?php echo TFISH_ADMIN_URL; ?>?action=confirmDelete&amp;id=<?php echo xss($content->id()); ?>"><i class="fas fa-trash-alt"></i></a></td>
        <?php unset($content); ?>
      </tr>
    <?php endforeach; ?>
    </tbody>
  </table>
  </div>
</div>

<?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 endif; ?>

<?php endif; ?>

</div>