<?php defined('ACCESS') OR die('No direct script access...');
/**
 * Author - koder_alex
 * ISQ - 669906617
 * VK - https://vk.com/koder_alex
 * It is forbidden to give, sell, modify.
 */
$data = array();
if (isset($_POST['peer']) && is_numeric($_POST['peer']) && DB::$pdo->querySingle("SELECT COUNT(id) FROM `users` WHERE `id` = ?", array((int)$_POST['peer'])))
    $peer = d_user((int)$_POST['peer'], false);
else
    $peer = d_user(0, false);
if (($peer['last_time'] > (time() - $config['TIME_ONLINE']) || $peer['set_online_hide'] == "1" ) && $user['id'] != $peer['id']) {
    $data['read'] = true;
    if (DB::$pdo->querySingle("SELECT COUNT(id) FROM `users` WHERE `vkTypingId` = '$user[id]' AND `vkTypingLastTime` > '" . (time() - 5) . "' AND `id` = '" . $peer['id'] . "'"))
        $data['typing'] = true;
    if (DB::$pdo->querySingle("SELECT COUNT(id) FROM `mail` WHERE `read` = '0' AND `id_kont` = '$peer[id]' AND `id_user` = '$user[id]'"))
        $data['read'] = false;
    $q = DB::$pdo->query("SELECT * FROM `mail` AS m WHERE m.`read` = '0' AND (m.`id_user` = '" . $peer['id'] . "' and m.`id_kont` = '" . $user['id'] . "') AND m.unlink != '$peer[id]' ORDER BY m.`id` ASC");
    $qF = DB::$pdo->prepare("SELECT id,folder, name, tmp_name, extension, type FROM mail_files_vk WHERE `action` = 'sent' AND `id_mail` = ?");
    $data['message'] = array();
    while ($post = $q->fetch()) {
        ob_start();
        $d = d_user($post['id_user'], true, 'mi_img');
        ?>
        <div class="msg_item<?= ($user['id'] == $d['id'] ? ' message_outbox' : ' message_inbox') ?>">
        <div class="mi_iwrap"><a href="/id<?= $d['id'] ?>"><?= $d['avatar'] ?></a></div>
        <div class="mi_cont">
            <div class="mi_head">
                <span class="mi_date"><?= system::times($post['time']) ?></span>
                <a class="mi_author"
                   href="/id<?= $d['id'] ?>"><?= $d['online'] . $d['group'] . $d['login'] . $d['Medals'] ?></a>
            </div>
            <div class="mi_body">
                <?php if($post['attachments']) {
                    $qF->execute([$post['id']]);
                    ?>
                    <div class="cp_attached_wrap">
                        <div class="pi_medias">
                            <?php while($file = $qF->fetch()) { ?>
                                <a class="medias_thumb thumb_item mr_x_wrap" href="/mail/file/<?= $file['folder'] ?>/<?= $file['tmp_name'] ?>" target="_blank">
                                    <img class="ph_img" src="/mail/file/<?= $file['folder'] ?>/<?= $file['tmp_name'] ?>"></a>
                            <?php } $qF->closeCursor();?>
                        </div>
                    </div><div class="mi_text">
                        <?= system::BBcode(system::textOut($post['msg'])) ?>
                    </div>

                <?php } else if($post['gifts'] > 0) {
                    $gifts = DB :: $pdo -> queryFetch("SELECT * FROM `user_gifts` WHERE `id` = '".$post['gifts']."' LIMIT 1");
                    ?><div class="mi_text"><div class="mi_medias_plain">
                    <?= ($user['id'] == $d['id'] ? "Вы отправили" : "Вам прислали") ?> подарок
                    </div>
                    </div>
                    <a href="/gifts?id=<?= $user['id'] ?>">
                        <img src="<?=(FILES_SERVER."gifts/{$gifts['id_gift']}.jpg")?>">
                    </a>
                    <div class="mi_text"><?= $gifts['komment'] ?></div>
                <?php } else { ?>
                    <div class="mi_text">
                        <?= system::BBcode(system::textOut($post['msg'])) ?>
                    </div>
                <?php } ?>
                <span style="float: right;margin-top: -10px;text-decoration: underline;">
                                        <a href="/mail/?act=delete&amp;id=<?= $post['id'] ?>"><span
                                                    class="ico ico-dell"></span></a>
                                    </span>
            </div>
        </div></div><?
        $data['message'][] = ob_get_contents();
        ob_end_clean();
    }
    DB::$pdo->query("UPDATE `mail` SET `read` = '1' WHERE `id_kont` = '$user[id]' AND `id_user` = '$peer[id]'");
}
setData($data);