<?php
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');
$title= new Body;
$title->SetTitle(homeurl.' - Полезные куски кодов');
$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> ::  Полезные коды</div></div></div>';
$id = isset($_GET['red']) ? abs((int)$_GET['red']) : false;

// Переимен
if (isset($_GET['red'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
$inf = DB::$dbs->queryFetch("SELECT name FROM `codes_cat` WHERE `id` = ? LIMIT 1", [$id]);
if (isset($_POST['save'])) {
$err = [];
$name = check($_POST['name']);
if (empty($name)) {
$err[] = 'Заполните название!';
}
if (empty($err)) {
DB::$dbs->query("UPDATE codes_cat SET name = ? WHERE id = ? LIMIT 1", [$name,$id]);
echo '<div class="alert alert-success"role="alert"">Успешно сохранено!</div>'; } else {
echo '<div class="alert alert-danger" role="alert"">';
foreach($err as $error) {
echo $error.'<br/>';
}
echo '</div>';
}
}
echo '<div class="list-group-item media"><form method="post" action="?red='.$id.'">Название:<br/> 
<input type="text" class="form-control m-b-10" name="name" value="'.$inf['name'].'"/> 
<input type="submit" name="save" class="btn btn-primary" value="Сохранить"/></div></form>'; 
}
// Удаление категории
if (isset($_GET['del'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
DB::$dbs->query("DELETE FROM codes_cat WHERE id = ?", [abs((int)$_GET['del'])]);
header('location: ?');
die();
}
echo'<div class="list-group-item media"><a href="search.html"><i class="zmdi zmdi-search"></i> Поиск кодов</a></div>';
echo'<div class="t-body"><div class="list-group lg-alt">';
$looks = DB::$dbs->querySingle("SELECT COUNT(id) FROM codes_cat");
if ($looks ==0) {
err('Категорий нет!');
} else {
$array = DB::$dbs->query("SELECT * FROM codes_cat ORDER BY id ASC");
while($a = $array->fetch()) {
$D = ($b['level'] > 4 ? '<a href="?red='.$a['id'].'">[Изм]</a> <a href="?del='.$a['id'].'">[Удл]</a> ':NULL);
$_new = DB::$dbs->querySingle("SELECT COUNT(id) FROM codes_code WHERE cid = ? AND time > ?", [$a['id'],time()-86400]);
echo '<div class="list-group-item media"><i class="zmdi zmdi-folder"></i> <a href="codlist.php?id='.$a['id'].'"> '.$a['name'].' ('.DB::$dbs->querySingle("SELECT COUNT(id) FROM codes_code WHERE cid = ?", [$a['id']]).(!empty($_new) ? '<font color="red">+'.$_new.'</font>':NULL).')</a> '.$D.' </div>';
}
}
if ($b['level'] > 3) {
if (isset($_POST['add'])) {
if (empty($_POST['name'])) {
err('Введите название!');
} else {
DB::$dbs->query("INSERT INTO codes_cat SET name = ?", [check($_POST['name'])]);
header('location: ?');
}
}
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i> Новая категория</div></div></div>';
echo '<div class="list-group-item media"><form action="?" method="POST"> <input type="text" class="form-control" name="name"/> <input type="submit" class="btn btn-primary btn-block" name="add" value="Добавить"/></form></div>';
}

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