<?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> :: <a href="/office"style="text-decoration:none; color:white;"> Кабинет</a> :: <a href="/notebook" style="text-decoration:none; color:white;">Блокнот</a> :: Добавить запись</div></div>';
if (isset($_GET['add'])){
$name = check($_POST['name']);
$msg = check($_POST['msg']);
if (empty($name) OR empty($msg)) {
err('Заполните все поля.');
} else {
$key = pass(5);
DB::$dbs->query("INSERT INTO `notebook` (`name`, `msg`, `key`, `time`, `id_user`) VALUES (?,?,?,?,?)", [$name,$msg,$key,time(),$b['id']]);
header('Location: note.php?key=' . $key);
exit;
}
}

if (isset($_GET['edit'])){
$notebook = DB::$dbs->queryFetch("SELECT * FROM `notebook` WHERE `id` = ? LIMIT 1", array(intval($_GET['edit'])));
if ($notebook['id_user'] != $b['id']){
header('Location: /');
exit;
}
if (isset($_POST['edit'])){
$name = check($_POST['name']);
$msg = check($_POST['msg']);
if (empty($name) OR empty($msg)) {
err('Заполните все поля.');
} else {
DB::$dbs->query("UPDATE `notebook` SET `name` = ?, `msg` = ? WHERE `id` = ? LIMIT 1", [$name,$msg,$notebook['id']]);
header('Location: note.php?key=' . $notebook['key']);
exit;
}
}
echo '<div class="list-group-item media"><form method="post" action="?edit=' . $notebook['id'] . '">Название:<br /><input name="name" class="form-control" value="' . $notebook['name'] . '" type="text" /><br />Текст:<br /></br>';
quickpaste('text');
quickpanel();
echo'<br/><br/><textarea name="msg" class="form-control" rows="8"/>' .$notebook['msg'] . '</textarea><br /><input value="Cохранить" class="btn btn-primary btn-block" name="edit" type="submit" /></div></form>';
$title->GetMenu();
$title->GetFooter();
exit;
}

echo '<div class="list-group-item media"><form method="post" name="message" action="?add">Название:<br /><input name="name" class="form-control" value="" type="text" placeholder="Название записи.."  /><br />Текст:<br /></br>';
quickpaste('text');
quickpanel();
echo'<br/><br/><textarea name="msg" class="form-control" rows="8"/></textarea><br /><input value="Записать" type="submit" class="btn btn-primary btn-block"/></form></div>';

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