<?php
session_start();
include("config.php");
include("functions.php");

$login = urldecode(filter_input(INPUT_SERVER, 'QUERY_STRING'));
$uid = isset($_SESSION['uid']) ? $_SESSION['uid'] : NULL;
$user = db::c()->query('SELECT `id`,`login`,`married`,`win`,borndate,`align`,`admin`,`realname`,`info`,`prof1`,`prof2`,`email`,`ip`,`exp`,`stats`,`money`,`room`,`doblest` 
                        FROM `users` 
                        WHERE `login` = "?s" OR `id` = "?s"', $login, $login)->fetch_assoc();
$own = db::c()->query('SELECT `align`,`admin` FROM `users` WHERE `id` = "?s"', $uid)->fetch_assoc();

$presents = db::c()->query('SELECT `img`,`present` FROM `inventory` WHERE `owner` = ?i',$user['id']);

if (empty($user['id'])): ?>
    <html>
    <head><meta charset="utf-8"><title>Ошибка</title></head>
    <body style="color: #666; background-color: #d5d5d5; text-align: center; font-family: Consolas,monospace;">
    Ошибка: персонаж<?= ($login ? "&nbsp;<em>" . $login . "</em>" : ""); ?> не найден...
    <p><a style="color: #99f" href="javascript:window.history.go(-1);">←назад</a></p>
    </body>
    </html>
    <? exit(); endif; ?>

<html>
<head>
    <title>Информация о <?= $user['login'] ?></title>
    <meta charset=UTF-8"/>
    <link href="css/main.css" rel="stylesheet">
    <link href="css/tooltip.css" rel="stylesheet">
</head>
<BODY onload="<?= topsethp() ?>">
<table width=100%>
    <tr>
        <td style="width: 250px; vertical-align: top;">
            <?= showinf_pers($user['id'], 1);

            if (!empty($user['married']))
                echo '<br><a href="inf.php?' . $user['married'] . '" target=_blank><img src="i/married.gif" class="tooltip" title="В браке с ' . $user['married'] . '"></a>';
            ?>
        </td>
        <td style="vertical-align: top; width: 100px; text-align: center;">
            <img src="i/<?= star_sign(substr($user['borndate'], 3, 2), substr($user['borndate'], 0, 2)); ?>.gif"
                 width="100"/>
        </td>
    </tr>
</table>
<hr>
<!-- Нижняя часть -->
<?php
$effect = db::c()->query('SELECT `time` FROM `effects` WHERE `owner` = ?i AND `type` = ?i ', $user['id'], 5)->fetch_assoc();
if ($effect['time'] AND ($own['admin'] != 1)) die('<h3>Персонаж обезличен!</h3>');
if (!empty($user['realname'])) echo "Имя: " . $user['realname'] . "<br>";
?>
<p style="font-family: Consolas,monospace;"><?= nl2br($user['info']) ?></p>
<hr>
<div id="Content">
    <fieldset>
        <legend>Подарки</legend>
        <?php
        if ($presents->getNumRows())
            while ($row = $presents->fetch_assoc())
                echo "<img src=i/sh/{$row['img']} class='tooltip' title='Подарок от {$row['present']}'>";
        ?>
    </fieldset>
    <fieldset>
        <legend>Лицензии</legend>
        <?php
        if ($user['prof2'] == 21) echo ' <img src="i/prof21.png" class="tooltip" title="<b>Лицензия наемника</b>" /> ';
        if ($user['prof2'] == 22) echo ' <img src="i/prof22.png" class="tooltip" title="<b>Лицензия лекаря</b>" /> ';
        if ($user['prof1'] == 1) echo ' <img src="i/prof1.png" class="tooltip" title="<b>Лицензия кузнеца</b>" /> ';
        if ($user['prof1'] == 2) echo ' <img src="i/prof1.png" class="tooltip" title="<b>Лицензия наставника</b>" /> ';
        ?>
    </fieldset>
</div>

<?php
# Секретыный раздел
$okld = 0;
$okdop = 0;

# Если я любой паладин, и смотрю не админа, или я сам админ.
if ((((int)$own['align'] == 1) AND (!$user['admin'])) OR (!empty($own['admin']))) $okld = 1;
# Если я любой паладин выше 1.2, и смотрю не админа, или я сам админ.
if ((((int)$own['align'] == 1) AND (!$user['admin']) AND ($own['align'] > 1.2)) OR (!empty($own['admin']))) $okdop = 1;

if ($okld == 1) {
    echo "<H4>Личное дело</H4>";
    $ld = db::c()->query('SELECT * FROM `lichka` WHERE `pers` = ?i ORDER BY `id` ASC', $user['id']);
    while ($row_ld = $ld->fetch_assoc()) {
        $dat = date("d.m.Y H:i", $row_ld['date']);
        echo "<code>$dat {$row_ld['text']} </code><br />";
    }
}

if ($okdop == 1) {
    $iplog = db::c()->query('SELECT idperslater FROM delo_multi WHERE idpersnow = ?i GROUP BY idperslater ASC', $user['id']);
    if ($iplog->getNumRows()) {
        echo "<H4>Возможные мульты</H4>";
        while ($row_iplog = $iplog->fetch_assoc()) echo nick::id($row_iplog['idperslater'])->full() . " ";
    }

    echo "<H4>Дополнительные сведения</H4>";

    if (!empty($user['id'])) echo "ID: " . $user['id'] . "<br>";
    if (!empty($user['email'])) echo "E-mail: " . $user['email'] . "<br>";
    if (!empty($user['borndate'])) echo "День рождения: " . $user['borndate'] . "<br>";
    if (!empty($user['ip'])) echo "IP при регистрации: " . $user['ip'] . "<br>";
}

if ($own['admin'] == 1) {

    if (!empty($user['exp'])) echo "Oпыт: " . $user['exp'] . "<br>";
    if (!empty($user['stats'])) echo "Число неиспользованных UP-ов: " . $user['stats'] . "<br>";
    if (!empty($user['money'])) echo "Кредитов: " . $user['money'] . "<br>";
    if (!empty($user['room'])) echo "Комната: " . $user['room'] . "<br>";
    if (!empty($user['reputation'])) echo "Всего Репутации: " . $user['reputation'] . "<br>";
    if (!empty($user['doblest'])) echo "Репутации: " . $user['doblest'] . "<br>";

    if ($user_bank = db::c()->query('SELECT `id`,`cr`,`ekr` FROM `bank` WHERE `id` = ?i', $user['id'])) {
        echo "<H4>Банковские счета</H4>";
        while ($row_user_bank = $user_bank->fetch_assoc())
            echo "№" . $row_user_bank['id'] . ": " . $row_user_bank['cr'] . " кр; " . $row_user_bank['ekr'] . " екр.";
    }

    //TODO Вынести управляющие элементы во внешнюю админку, подальше от прецедентов. А то были случаи.
    if (isset($_POST['del'])) {
        $itemdel = db::c()->query('SELECT `type`,`dressed` FROM `inventory` WHERE `id`=?i', $del)->fetch_assoc();
        if ($itemdel['dressed'] == 1) {
            dropitem($itemdel['type']);
            if ($itemdel['type'] == 5) {
                dropitem(6);
                dropitem(7);
            }
        }
        db::c()->query('DELETE FROM `inventory` WHERE `id` = ?i', $del);
    }

    echo "<br><H4><u>Вещи в инвентаре:</u></H4>";
    echo "<table class='adm'>
            <tr>
                <th>ID</th>
                <th>Название</th>
                <th>Количество</th>
                <th>Картинка</th>
                <th></th>
          	</tr>
          ";
    $inv = db::c()->query('SELECT `id`,`name`,`koll`,`img` FROM `inventory` WHERE `owner` = ?i ORDER BY `id` DESC', $user['id']);
    while ($row_inv = $inv->fetch_assoc()) {
        echo "
                <tr>
                    <td>{$row_inv['id']}</td>
                    <td>{$row_inv['name']}</td>
              		<td>{$row_inv['koll']}</td>
              		<td><img src='i/sh/{$row_inv['img']}'></td>
              		<td><form method='post'><input name='del' type='hidden' value='{$row_inv['id']}'>
      		        <input name='ok' type='submit' value='Удалить'></form></td>
                </tr>
            ";
    }
    echo "<th colspan='6'><form method='post'><input name='undress_char' type='submit' value='Стриптиз'></form></th></table>";
    if (isset($_POST['undress_char'])) undressall($user['id']);
}
?>

<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/showthing.js"></script>
<script src="js/tooltip.js"></script>
<script>
    $(function () {
        $(".tooltip").tipTip({maxWidth: "auto", edgeOffset: 0, fadeIn: 300, fadeOut: 500});
    });
</script>
</body>
</html>