<?php
require_once('../../classes/body.class.php');
require_once('../../classes/db.class.php');
require_once('../../classes/system.class.php');
$title= new Body;
$title->SetTitle('Биллинг - Смена ника');
$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="/billing"style="text-decoration:none; color:white;">Биллинг</a> :: <a href="/billing/usl"style="text-decoration:none; color:white;">Услуги</a> :: Смена ника</div></div></div>';
$bill = DB::$dbs->queryFetch("SELECT * FROM billing_set WHERE id = ? LIMIT 1",[1]);

if (isset($_GET['ok'])) echo '<div class="alert alert-success"role="alert">Ник изменен!</div>';

if (isset($_POST['add'])) {
$nick = check($_POST['nick']);
if (empty($nick)) {
err('Введите ник!');
} elseif ($bill['icon'] > $b['rur']) {
err('Недостаточно средств!');
}
elseif (DB::$dbs->querySingle("SELECT COUNT(id) FROM `blogger` WHERE `nick` = ?", [$nick]) > 0) {
err('Такой ник уже занят!');
} else {
DB::$dbs->query("UPDATE `blogger` SET `nick` = ? WHERE `id` = ? LIMIT 1", [$nick,$b['id']]);
DB::$dbs->query("UPDATE `blogger` SET `rur` = `rur` - ? WHERE `id` = ? LIMIT 1", [$bill['nick'],$b['id']]);
$text = 'Изменен ник за '.$bill['nick'].' руб.';
DB::$dbs->query("INSERT INTO `billing_hist` SET `user_id` = ?, `text` = ?, `time` = ?", [$b['id'],$text,time()]);
header('Location: ?ok');
die();
}
}
echo '<div class="list-group-item media"><form method="post" action="?">* Стоимость смены ника <b>'.$bill['nick'].' руб.</b><br/>Новый ник: (*max.:15)<br/><input name="nick" class="form-control" type="text"/><br/><input type="submit" name="add" class="btn btn-primary"
 value="Изменить"/></form></div>';

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