<?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();

if ($b == false){
header('location:/');
die();
}
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>';
$id = isset($_GET['id']) ? abs((int)$_GET['id']) : false;

// Переимен
if (isset($_GET['red'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
$id = isset($_GET['red']) ? abs((int)$_GET['red']) : false;
if (empty($id)) {
header('location: /');
die();
}
$inf = DB::$dbs->queryFetch("SELECT name FROM `smiles_r` 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 smiles_r 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" name="name" class="form-control" value="'.$inf['name'].'"/><br/>
<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 smiles_r WHERE id = ?", [abs((int)$_GET['del'])]);
header('location: smiles');
die();
}

$looks = DB::$dbs->querySingle("SELECT COUNT(id) FROM smiles_r");
if ($looks ==0) {
err('Категорий нет!');
} else {
echo'<div class="tile-body"><div class="count-box bg-blue"><div class="row text-center">';

$array = DB::$dbs->query("SELECT * FROM smiles_r ORDER BY id ASC");
while($a = $array->fetch()) {
$D = ($b['level'] > 2 ? '<a href="?del='.$a['id'].'">[Удл]</a> <a href="?red='.$a['id'].'">[Изм]</a>':NULL);
$_new = DB::$dbs->querySingle("SELECT COUNT(id) FROM smiles WHERE cid = ? AND time > ?", [$a['id'],time()-86400]);
echo'<a href="smile.php?id='.$a['id'].'"style="text-decoration:none; color:white;"><div class="col-sm-3 col-xs-6"><div class="cb-item"><h3 style="text-decoration:none; color:white;">'.DB::$dbs->querySingle("SELECT COUNT(id) FROM smiles WHERE cid = ?", [$a['id']]).(!empty($_new) ? '<font color="red">+'.$_new.'</font>':NULL).'</h3><small>'.$a['name'].'</small></div></div></a>';

//echo '<div class="list-group-item media"><a href="smile.php?id='.$a['id'].'"><i class="zmdi zmdi-shuffle"></i>&nbsp;  '.$a['name'].' <span class= "count">('.DB::$dbs->querySingle("SELECT COUNT(id) FROM smiles WHERE cid = ?", [$a['id']]).(!empty($_new) ? '<font color="red">+'.$_new.'</font>':NULL).')</span></a> '.$D.'</div>';
}
echo'</div></div></br></div>';

}
if ($b['level']>2) {
if (isset($_POST['add'])) {
if (empty($_POST['name'])) {
err('Введите название!');
} else {
DB::$dbs->query("INSERT INTO smiles_r SET name = ?", [check($_POST['name'])]);
header('location: ?');
}
}
echo '<div class="list-group-item media"><form action="?" method="POST">Новая категория:<br/> <input type="text" class="form-control" name="name"/> <input type="submit" class="btn btn-primary" name="add" value="Добавить"/></form></div>';
}

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

?>