<?
if ($level == 0){
  header('Location: /index.php');
  exit;
}

if (isset($_GET['delete'])) {
  $delete = (int) $_GET['delete'];
  
  if ($sql -> selectCount("SELECT COUNT(*) FROM `contacts` WHERE `id_user` = '$user[id]' AND `id_contact` = '$delete' AND `id_contact` != '0'") != 0) {
    $sql -> delete("DELETE FROM `contacts` WHERE `id_user` = '$user[id]' AND `id_contact` = '$delete' AND `id_contact` != '0'");
    $messages[] = 'Контакт успешно удален';
  }
}

if (isset($_GET['type']) && $_GET['type'] != '1') {
  $type = (int) $_GET['type'];
  $order = "AND `type` = '$type'";
} else {
  $type = '1';
  $order = "AND `type` != '0'";
}

$system['title'] = 'Cписок контактов';
require SYS . 'header.php';

?>
<div class="pagination pagination-centered pagination-small" style="margin: 4px;"><ul>
<li><?= ($type != 1 ? '<a href="?func=' . $func . '&amp;type=1">Общие</a>' : '<a style="color: black;">Общие</a>')?></li>
<li><?= ($type != 2 ? '<a href="?func=' . $func . '&amp;type=2">Избранные</a>' : '<a style="color: black;">Избранные</a>')?></li>
<li><?= ($type != 0 ? '<a href="?func=' . $func . '&amp;type=0">Игнор</a>' : '<a style="color: black;">Игнор</a>')?></li>
</ul></div>
<?

$k_post = $sql -> selectCount("SELECT COUNT(*) FROM `contacts` WHERE `id_user` = '$user[id]' $order");

if ($k_post == 0) {
  ?>
  <div class="alert" style="margin: 2px;">Список контактов пуст</div>
  <?
} else {
  $k_page = k_page($k_post, $system['page_str']);
  $page = page($k_page);
  $start =  $system['page_str'] * $page -  $system['page_str'];

  $array = $sql -> select("SELECT * FROM `contacts` WHERE `id_user` = '$user[id]' $order ORDER BY `time_last` DESC LIMIT $start, $system[page_str]");

  foreach ($array AS $contact) {
    $count = $sql -> fetch("SELECT 
      SUM(IF(`read` = '0' AND `id_contact` = '$user[id]' AND `id_user` = '$contact[id_contact]'  AND `unlink` != '$user[id]', 1, 0)) AS 'new',
      SUM(IF(`id_contact` = '$user[id]' AND `id_user` = '$contact[id_contact]'  AND `unlink` != '$user[id]', 1, 0)) AS 'contact',
      SUM(IF(`id_contact` = '$contact[id_contact]' AND `id_user` = '$user[id]'  AND `unlink` != '$user[id]', 1, 0)) AS 'user'
      FROM `mail`");
    $count = array('new' => (int) $count['new'], 'contact' => (int) $count['contact'], 'user' => (int) $count['user']);
    $num++;
    ?>
    <div class="<?= ($num % 2 ? "nav1" : "nav2")?>">
      <a href="?func=user.contacts&amp;delete=<?= $contact['id_contact']?>&amp;page=<?= $page?>&amp;type=<?= $type?>" class="close">&times;</a>
      <?= avatar($contact['id_contact'], 4)?> <?= login($contact['id_contact'], 1)?> <a href="?func=user.mail&amp;id=<?= $contact['id_contact']?>">[переписка]</a> 
      [<?= $count['contact']?>/<?= $count['user']?>]<?= ($count['new'] > 0 ? '<span class="off">+' . $count['new'] . ' (' . vremja($contact['time_last']) . ')</span>' : ' (' . vremja($contact['time_last']) . ')')?> 
    </div>
    <?
  }

  if ($k_page > 1) { 
    str('?func=' . $func . '&amp;', $k_page, $page);
  }
}
?>