<?php
require __DIR__ . '/inc/head.php';

$email_filter = function ($email) { return str_replace( [ '@', '.' ], [ ' &lt;at&gt; ', ' &lt;dot&gt; ' ], $email ); };
$h2 = function ($text) {$t = substr(trim($text),0,50); return substr($t, 0,strrpos($t,' ')).'&hellip;'; };
$preview = function ($text) {$t = substr( trim( str_replace(["\n"],['<br>'], preg_replace('/<br>(\s*)?<br>/','<p>',preg_replace("/[\t\ ]{2,}/",' ',$text))) ),0,1000); return substr($t, 0,strrpos($t,' ')); };
ob_start();

$one_href = '/date/desc/';
$two_href = '/user/asc/';
$three_href = '/mail/asc/';
$one_arr = '&uarr; ';
$two_arr = '&ensp;&nbsp;';
$three_arr = '&ensp;&nbsp;';
if ($this->sort_by == 'date' && $this->sort_order == 'desc') {
  $one_href = '/';
  $one_arr = '&darr; ';
}
if ($this->sort_by == 'user') {
  $one_href = '/';
  $one_arr = '&ensp;&nbsp;';
  if ($this->sort_order == 'asc') {
    $two_href = '/user/desc/';
    $two_arr = '&uarr; ';
  } else {
    $two_href = '/user/asc/';
    $two_arr = '&darr; ';
  }
}
if ($this->sort_by == 'mail') {
  $one_href = '/';
  $one_arr = '&ensp;&nbsp;';
  if ($this->sort_order == 'asc') {
    $three_href = '/mail/desc/';
    $three_arr = '&uarr; ';
  } else {
    $three_href = '/mail/asc/';
    $three_arr = '&darr; ';
  }
}
?>
  <h1 class="page-title"><?php echo $this->page_title ?></h1>
  <div class="add-link"><a href="/new/" class="add-link__a">Добавить объявление</a></div>
  <div class="sort">
    <div class="sort__cell">Сортировка:</div>
    <div class="sort__cell"><a href="<?php echo $one_href ?>"><?php echo $one_arr ?>Дата</a></div>
    <div class="sort__cell"><a href="<?php echo $two_href ?>"><?php echo $two_arr ?>Пользователь</a></div>
    <div class="sort__cell"><a href="<?php echo $three_href ?>"><?php echo $three_arr ?>e-mail</a></div>
  </div>
<?php
foreach ($this->ads as $item){
  ?>
  <article>
    <header>
      <div>
        <h2><?php echo $h2( $item->getText() ); ?></h2>
        <span><i class="i-date"></i><?php echo $item->getDate(); ?></span>
        <span><i class="i-user"></i><?php echo $item->getUserName(); ?></span><br>
        <span><i class="i-email"></i><?php echo $email_filter($item->getEmail()); ?></span>
        <?php
        if ( ! empty( $item->getHomepage() ) ) {
          echo '<br><span><i class="i-homepage"></i><a href="' . $item->getHomepage() . '" target="_blank">' . $item->getHomepage() . '</a>';
        }
        ?>
      </div>
    </header>
    <p><?php echo $preview( $item->getText() ); ?>
    <footer>
      <a href="/full/<?php echo $item->getId(); ?>/">Подробнее</a>
    </footer>
  </article>
  <?php
}

if ($this->pages_count>1) {
  $pages = [];
  $prefix_url = ($this->sort_by == 'date' && $this->sort_order == 'asc')?'':'/'.$this->sort_by.'/'.$this->sort_order;

  $start = $this->current_page - 2;
  $start = ($start<=0)?1:$start;

  $end = $this->current_page + 2;
  $end = ($end<=$this->pages_count)?$end:$this->pages_count;
  if ((($end - $start) < 4) && ($this->pages_count > 4)) {
    $end++;
    if ((($end - $start) < 4) && ($this->pages_count > 4)) {
      $end++;
    }
  };
  $end = ($end<=$this->pages_count)?$end:$this->pages_count;

  if ($this->current_page > 3) {
    $pages[]=['В&nbsp;начало' , $prefix_url . '/'];
  }
  for ($i=$start;$i<=$end;$i++){
    $pages[]=[ $i, $prefix_url . '/page/' . $i . '/'];
  }
  if ($this->current_page != $this->pages_count) {
    $pages[]=['дальше' , $prefix_url . '/page/' . ( $this->current_page + 1 ). '/'];
  }

  ?>
  <footer>
    <div class="pager"><?php
      foreach ($pages as $value) {
        if ((int)$this->current_page != (int)$value[0] ) {
        ?>
        <div class="pager__page<?php if ((int)$value[0]==0)echo " pager__page--string";?>"><a href="<?php echo $value[1];?>"><?php echo $value[0];?></a></div>
        <?php
      } else { ?>
          <div class="pager__page pager__page--current"><span><?php echo $value[0];?></span></div>
          <?php
        }
      }
      ?>
    </div>

  </footer>
  <?php
}

$this->content = ob_get_contents();
ob_end_clean();

require __DIR__ . '/inc/body.php';