<?

$title = $l = "".language::text('gifton', $language,'gifts')."";
$lBack = "/gifts?id={$ank['id']}&amp;cat=1";
require(CORE . "head.php");
$gift_id = (int)abs($_GET['gift']);
$gift = DB:: $pdo->queryFetch("SELECT * FROM `gifts` WHERE `id` = ? LIMIT 1;", array($gift_id));

if (!isset($gift) || $gift['id'] <= 0 || ($gift['do'] > 0 && $gift['do'] < time())) {
    ?>
    <div class="list _gray _font-s _center">Подарок не найден!</div>
    <?
} else {
    $cat = DB:: $pdo->queryFetch("SELECT * FROM `gifts_kat` WHERE `id` = ? LIMIT 1;", array($gift['id_kat']));


    if ($cat['admin'] == 0 || ($cat['admin'] == 1 && admin:: getAccess($user, array('a' => 3, 'm' => 1)))) {


        if (isset($_POST['cfms'])) {
            if ($user['line'] < $gift['cena']) {
                $errMsg = "Недостаточно лайн на счете!";
            }
            if (!isset($errMsg)) {
                if (isset($_POST['komment']) && $_POST['komment'] != NULL) {
                    $komment = system:: check($_POST['komment']);
                } else {
                    $komment = '';
                }
                if (isset($_POST['privat']) && $_POST['privat'] == 1) {
                    $privat = 1;
                } else {
                    $privat = 0;
                }
                if (DB:: $pdo->query("INSERT INTO `user_gifts` (`id_user`, `id_ank`, `id_gift`, `id_kat`, `privat`, `komment`, `time`) VALUES (?, ?, ?, ?, ?, ?, ?);", array($user['id'], $ank['id'], $gift['id'], $cat['id'], $privat, $komment, time()))) {
                    $lastIdGift = DB :: $dbh -> lastInsertId();
                    $count = DB:: $pdo->querySingle("SELECT COUNT(*) FROM `user_gifts` WHERE `id_ank` = ?;", [$ank['id']]);
                    DB:: $pdo->query("UPDATE `users` SET `count_gifts` = '{$count}', `count_new_gifts`=`count_new_gifts`+1 WHERE `id` = '" . $ank['id'] . "' LIMIT 1;");
                    DB:: $pdo->query("UPDATE `users` SET `line` = `line`-'" . $gift['cena'] . "' WHERE `id` = '" . $user['id'] . "' LIMIT 1");
                    DB:: $pdo->query("INSERT INTO `money` (`user`, `money`, `mp`, `usl`, `time`) values('" . $user['id'] . "', '" . $gift['cena'] . "', '0', 'Отправка подарка для " . $ank['login'] . "', '$time')");

                  //  DB:: $pdo->query("INSERT INTO `mail` (`id_user`, `id_kont`, `gifts`, `time`) values(:id_user, :id_kont, :gifts, :time)", array(':id_user' => $user['id'], ':id_kont' => $ank['id'], ':gifts' => $lastIdGift, ':time' => time()));
                 
                    notif::init()->gift(['id_user' => $ank['id'],   'id_last' => $user['id'], 'id_obj' => $gift['id'], 'notices_text' => $komment]);
                    DB:: $pdo->query("UPDATE `users` SET `count_new_notice` = `count_new_notice`+1 WHERE `id` = '".$ank['id']."' LIMIT 1;");
                    system:: redirect("Подарок успешно отправлен.", "/gifts?id=" . $ank['id'] . "");
                } else {
                    $errMsg = "Неизвестная ошибка.!";
                }
            }
        }
        if (isset($errMsg)) {
            system:: show($errMsg);
        }
        ?>
        <div class="app-block">
            <div class="list">
                <div class="_center">
                    <img src="<?= (FILES_SERVER . "gifts/" . $gift['id'] . ".jpg") ?>"><br>
                    <? if ($gift['gifts_text'] != "") {
                        ?>
                        <span style="color: #8b91a2;background: #e5edff;padding: 5px;border-radius: 3px;"><?= $gift['gifts_text'] ?></span>
                    <? } ?>

                </div>
                <div class="_padd-y"></div>
                <div>
                    <span class="_gray"><?=language::text('nick', $language,'gifts');?>:</span> <span class="_bold"><?= profile:: goPage($ank, false) ?></span>
                </div>
                <div>
                    <span class="_gray"><?=language::text('line', $language,'gifts');?>:</span> <?= system:: coins($gift['cena']) ?>
                </div>
                <div>
                    <span class="_gray"><?=language::text('lineme', $language,'gifts');?>:</span> <?= system:: coins($user['line']) ?>
                </div>
            </div>
            <div class="list _bg-gray">
                <form method="post" data-type="arbour"
                      action="/gifts?id=<? echo $ank['id']; ?>&gift=<? echo $gift['id']; ?>">
                    <div>
                        <textarea name="komment"></textarea>
                    </div>
                    <div class="_padd-y">
                        <label class="_dbl _gray"><input name="privat" value="1" type="checkbox"/><?=language::text('me', $language,'gifts');?> </label>
                    </div>
                    <div style="padding-top: 4px;">

                        <input type="submit" class="main-btn" name="cfms" value="<?=language::text('ok', $language,'gifts');?>"/>
                    </div>
 
                </form>
            </div>
        </div>
        <?
    } else {
        ?>
        <div class="list _gray _font-s _center">Подарок не найден!</div>
        <?
    }
}

require(CORE . "foot.php");