<?
if (isset($ID)) {
  $ank = $sql -> fetch("SELECT * FROM `users` WHERE `id` = '$ID' LIMIT 1");
} elseif ($level > 0) {
  $ank = $user;
}

if (!isset($ank['id'])){
  die('К сожалению пользователь не найден или он был удален');
}

if (isset($_GET['delete'])) {
  $delete = (int) $_GET['delete'];
  $delete = $sql -> fetch("SELECT * FROM `user_votes` WHERE `id_ank` = '$ank[id]' AND `id_user` = '$delete' LIMIT 1");
  $type = $delete['type'];
  
  if ($level > 2 || $delete['id_user'] == $user['id']) {
      
    //if ($delete['id_user'] != $user['id']) {
      admin_log($user['id'], 'Пользователи|Арбитраж', 'Удаление '.($type == 1 ? 'положительного отзыва: [green]\"' . $delete['msg'] . '\"[/green]' : 'отрицательного отзыва: [red]\"' . $delete['msg'] . '\"[/red]').'\ О пользователе [url=?func=user.profile&id=' . $delete['id_ank'] . ']' . login($delete['id_ank']) . '[/url]');
    //}
    /*
    if ($delete['type'] == 1) {
      $sql -> update("UPDATE `users` SET `rating` = `rating` + '1' WHERE `id` = '$delete[id_ank]' LIMIT 1");  
    } else {
      $sql -> update("UPDATE `users` SET `rating` = `rating` - '1' WHERE `id` = '$delete[id_ank]' LIMIT 1");    
    }
    */
    $sql -> delete("DELETE FROM `user_votes` WHERE `id_user` = '$delete[id_user]' AND `id_ank` = '$ank[id]' LIMIT 1");
    $messages[] = 'Отзыв успешно удален';    
  }
}


if (isset($_POST['msg']) && $level > 0 && ($sql -> selectCount("SELECT COUNT(*) FROM `user_votes` WHERE `id_ank` = '$ank[id]' AND `id_user` = '$user[id]'") == 0)) {
  $msg = $sql->esc($_POST['msg']);
  $type = (int) $_POST['type'];
/// Если нет покупок то проверяем заполнен ли вмид и если нет не даём писать
$pokupka = $sql -> selectCount("SELECT COUNT(*) FROM `shop_pays` WHERE `id_user` = '".$user['id']."' AND `pays` = '1'");
if ($pokupka == 0) {
if ($user['wmid']==0)$err[] = 'Не заполнены реквизиты в анкете !'; /// by_Mstivoj
}
  if (strlen2($msg) > 512) {
    $err[] = 'Сообщение слишком длинное';
  } elseif (strlen2($msg) < 2) {
    $err[] = 'Сообщение слишком короткое';
  }
  
  if (!isset($err)) {
    journal($ank['id'], 'arbitrage', "Пользователь [b]$user[login] [/b], оставил".($user['pol']==0?'a':null)." ".($type == 1 ? '[green]положительный[/green]' : '[red]отрицательный[/red]')." [url=?func=user.votes&id=" . $ank['id'] . "]отзыв[/url] о Вас!");
    
    if ($type == 1) {
      admin_journal($user['id'], 'arbitrage', " оставил".($user['pol']==0?'a':null)." [green]положительный[/green] о [url=?func=user.votes&id=" . $ank['id'] . "]" . login($ank['id'], 0) . "[/url]");
    } elseif ($type == 0) {
      admin_journal($user['id'], 'arbitrage', " оставил".($user['pol']==0?'a':null)." [red]отрицательный[/red] о [url=?func=user.votes&id=" . $ank['id'] . "]" . login($ank['id'], 0) . "[/url]");
    }
    
    $sql -> insert("INSERT INTO `user_votes` (`id_user`, `id_ank`, `time`, `msg`, `type`) VALUES ('" . $user['id'] . "', '" . $ank['id'] . "', '$time', '$msg', '$type')");
    
    $_SESSION['message'] = 'Отзыв успешно отправлен';
    header('Location: /?func=' . $func . '&id=' . $ank['id']);
    exit;
  }
}

if (isset($_GET['view']) && $level > 2) {
  $id_user = (int) $_GET['view'];
  $id_us = $sql -> fetch("SELECT * FROM `users` WHERE `id` = '$id_user' LIMIT 1");
  $vote = $sql -> fetch("SELECT * FROM `user_votes` WHERE `id_ank` = '$ank[id]' AND `id_user` = '$id_us[id]' LIMIT 1");
  
  if ($vote['view'] == 0 && isset($id_us['id'])) {
    /*
    if ($vote['type'] == 1) {
      $sql -> update("UPDATE `users` SET `rating` = `rating` + '1' WHERE `id` = '$ank[id]' LIMIT 1");
    } elseif ($vote['type'] == 0) {
      $sql -> update("UPDATE `users` SET `rating` = `rating` - '1' WHERE `id` = '$ank[id]' LIMIT 1");
    }
    */
    $sql -> update("UPDATE `user_votes` SET `view` = '1' WHERE `id_ank` = '$ank[id]' AND `id_user` = '$id_us[id]' LIMIT 1");
    
    $_SESSION['message'] = 'Отзыв успешно одобрен';
    header('Location: /?func=' . $func . '&id=' . $ank['id']);
    exit;
  }
}

if ($level > 2 || isset($user) && $ank['id'] == $user['id']) {
  $where = "";
} elseif ($level > 0) {
  $where = "AND (`view` = '1' OR `id_user` = '$user[id]')";
} else {
  $where = "AND `view` = '1'";
}

$system['title'] = 'Отзывы о ' . login($ank['id'], 0);
require SYS . 'header.php';

if ($level > 0 && $sql -> selectCount("SELECT COUNT(*) FROM `user_votes` WHERE `id_ank` = '$ank[id]' AND `id_user` = '$user[id]'") == 0 && $user['id'] != $ank['id']) {
?>
<form class="form_t" action="/?func=user.votes&amp;id=<?= $ank['id']?>" method="POST" style="margin: 2px;">
  <? if (is_file(THEME . 'php/form.textarea.php')) {
    require THEME . 'php/form.textarea.php';
  } else {
    ?><textarea style="width: 75%;" name="msg" id="msg" placeholder="Cообщение..."><?= $insert?></textarea><br /><?
  }
  ?>
  <select name="type" id="type" class="span2">
   <option value='1' selected='selected'>Положительный</option>
   <option value='0'>Отрицательный</option>
	</select><br />
  
  <button type="submit" class="btn">Отправить отзыв</button>
</form>
<?  
}

$k_post = $sql -> selectCount("SELECT COUNT(*) FROM `user_votes` WHERE `id_ank` = '$ank[id]' $where");

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 `user_votes` WHERE `id_ank` = '$ank[id]' $where ORDER BY `time` DESC LIMIT $start, $system[page_str]");

  foreach ($array AS $post) {
    $num++;
    ?>
    <div class="<?= ($num % 2 ? "nav1" : "nav2")?>">
      <? if ($level > 2 ) { ?>
      <a class="close" href="/?func=user.votes&amp;id=<?= $ank['id']?>&amp;delete=<?= $post['id_user']?>"><i class="icon-remove"></i></a>
      <? } ?>
      
      <?= login($post['id_user'], 1)?> (<?= vremja($post['time'])?>)<br />
      <span class="<?= ($post['type'] == 0 ? 'off' : 'on')?>"><?= text_out($post['msg'])?></span><br />
      
      <? if ($level > 2 && $post['view'] == 0) { ?>
        [<a href="/?func=user.votes&amp;id=<?= $ank['id']?>&amp;view=<?= $post['id_user']?>">одобрить</a>]
      <? } ?>
    </div>
    <?
  }

  if ($k_page > 1) { 
    str('/?func=' . $func . '&amp;id=' . $ank['id'] . '&amp;', $k_page, $page);
  }  
}  
?>
<div class="foot">
  &laquo; <a href="/wm/<?= $ank['my_links']?>">На страницу пользователя</a><br />
</div>