<?php
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');
$title= new Body;
$id = isset($_GET['id']) ? abs((int)$_GET['id']) : false;
$array = DB::$dbs->query("SELECT * FROM codes_code WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
DB::$dbs->query("UPDATE `codes_code` SET `chit` = `chit` + ? WHERE id = ?", [1,$id]);
$cat = DB::$dbs->queryFetch("SELECT id,name FROM codes_cat WHERE id = ? LIMIT 1", [$arr['cid']]);
$title->SetTitle('Полезные куски кодов - '.$arr['name'].'');
$title->GetHeader();
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="/"style="text-decoration:none; color:white;"> Главная</a> :: <a href="/codes"style="text-decoration:none; color:white;">Полезные коды</a> :: <a href="codlist.html?id='.$cat['id'].'"style="text-decoration:none; color:white;">'.$cat['name'].'</a> :: '.$arr['name'].'</div></div></div>';
if ($b['level'] < 2) {
if ($arr['check'] == 0) {
err('Код на модерации!'); 
$title->GetMenu();
$title->GetFooter();
exit;
}
}
echo '<div class="list-group-item media"><b>Описание: </b> ';
echo text($arr['text']); echo'</div><div class="list-group-item media"><div class="citat" style="word-wrap:break-word;"><b>Код:</b></br>';
echo text($arr['code']);
echo '</div></div>';
echo '<div class="list-group-item media"><i class="zmdi zmdi-account"></i> Добавил: '.Nick($arr['user_id']).' ('.vr($arr['time']).')</div>';
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-comment"></i> Обсуждение кода ('.DB::$dbs->querySingle("SELECT COUNT(id) FROM codes_comm WHERE cid = ?", [$id]).')</div></div></div>';
$array = DB::$dbs->query("SELECT * FROM codes_code WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}

$arr = $array -> fetch();
if (isset($_GET['del'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
DB::$dbs->query("DELETE FROM codes_comm WHERE id = ?", [abs((int)$_GET['del'])]);
header('location: code.php?id='.$id);
die();
}
if (isset($_GET['otv'])) {
$t = DB::$dbs->queryFetch("SELECT * FROM blogger WHERE id = ?", [abs((int)$_GET['otv'])]);
if (empty($t['id'])) {
header('location: /');
}
$reply = $t['nick'].', ';
} else {
$reply = '';
}
$otv = isset($_GET['otv']) ? abs((int)$_GET['otv']) : false;
$k_mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM codes_comm WHERE cid = ?", [$id]);
$num = 10;
$k_page = k_page($k_mess, $num);
$page = page($k_page);
$start = $num*$page-$num;
$sql = DB::$dbs->query("SELECT * FROM codes_comm WHERE cid = ? ORDER BY time DESC LIMIT $start,$num", [$id]);
if ($sql->rowCount() == 0) {
err('Комментариев нет!');
} else {
while($a = $sql -> fetch()) {
$D = ($b['level'] > 1 ? '[<a href=code.php?id='.$id.'&del='.$a['id'].'>Удл</a>] ':NULL);
echo '<div class="list-group-item media">'.$D.Nick($a['user_id']).' ('.vr($a['time']).') '.(($a['user_id'] != $b['id']) ? ' <a href="code.php?id='.$id.'&otv='.$a['user_id'].'">[отв]</a>':NULL).'</br>'.text($a['text']).'</div>';
}
}
if ($k_page>1) {
str('code.php?id='.$id.'&', $k_page,$page);
}
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-comment"></i> Добавить комментарий</div></div></div>';
if ($b == true){
if (isset($_POST['add'])) {
$text = check($_POST['text']);
if (empty($text)) {
err('Введите текст!');
} elseif (DB::$dbs->querySingle("SELECT COUNT(id) FROM codes_comm WHERE text = ? AND cid = ?", [$text,$id]) > 0) {
err('Такой коммент уже есть :)');
} else {
DB::$dbs->query("INSERT INTO codes_comm SET text = ?, time = ?, user_id = ?, cid= ?", [$text,time(),$b['id'],$id]);
header('location: code.php?id='.$id);
die();
}
}
$o = (!empty($otv) ? '&otv='.$otv : NULL);
echo '<div class="list-group-item media"><form name="form" method="post" action="code.php?id='.$id.$o.'"><br/>';
quickpaste('text');
quickpanel();
echo'<br/><br/><textarea name="text" class="form-control" rows="8">'.$reply.'</textarea> </br><input type="submit" name="add"class="btn btn-primary btn-block" value="Добавить"/></div></form>';
} else {echo'<div class="list-group-item media">Только зарегистрированные пользователи могут оставлять комментарии. <a href= "/login" >Войдите</a>, пожалуйста.</div>';}


$title->GetMenu();
$title->GetFooter();

?>